In tableau, it is relatively straight forward to create tables/heatmaps with labels from two columns. Consider the following data
library(tidyverse)
set_cols <- as_mapper(
~set_names(.x, c("brand", "model"))
)
data <- mtcars[-6,] %>%
rownames() %>%
str_split(" ", n=2) %>%
map(set_cols) %>%
map_df(bind_rows) %>%
bind_cols(mtcars[-6,]) %>%
mutate_if(is.numeric, scale)
data_long <- data %>%
gather(key, value,
-(1:2))
#> Warning: attributes are not identical across measure variables;
#> they will be dropped
The desired output with tableau looks like this:
I find this much more readable than combining the two factors, e.g. by using interaction()
.
Now, I can get pretty close using static ggplot2:
p <- ggplot(data_long)+
aes(y = model, x = key, fill = value) +
geom_tile()+
facet_grid(brand~1, scale = "free_y", switch = "y", space = "free_y") +
scale_fill_continuous(guide =FALSE)+
theme_minimal()+
theme(strip.placement = "outside",
strip.text.y = element_text(angle = 180))
p
Created on 2019-08-09 by the reprex package (v0.3.0)
But if I try to pass this to ggplotly
all the styling gets butchered again: The arguments switch
and space
in facet_grid()
seem to be ignored.
library(plotly)
q <- ggplotly(p)
q
Question: How can I generate such plots?
I am thinking to write it with plotly directly, but after skimming the docs, I couldn't find an obvious way to do it. Any other solution is welcome (rhandsontable?), as long as it would allow for interactivity, e.g. respond to the selection of a cell in a shiny app.