0

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: tableau example

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 switchand space in facet_grid() seem to be ignored.

  library(plotly)
  q <- ggplotly(p)
  q

enter image description here

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.

Benjamin Schwetz
  • 624
  • 5
  • 17
  • 1
    If you add `panel.spacing = unit(0, "lines")` to your theme, you can get rid of the whitespace between your strips in your ggplot2-solution and then it should look quite similar to your tableau-solution (maybe increase the height of the plot as well when saving). – Gilean0709 Aug 09 '19 at 08:02
  • thanks! I only made a rough draft. As the ggplot2 object will still be static. – Benjamin Schwetz Aug 09 '19 at 08:04
  • so I found https://github.com/plotly/plotly.js/pull/3300 for multicategorical axis in plotly.js and https://plot.ly/r/axes/#subcategory-axes which does it completely manual... – Benjamin Schwetz Aug 09 '19 at 11:46

0 Answers0