Search code examples
rplotlylegendsubplot

Keep colours the same between facetted plots / subplots


Following the R plotly documentation, facetting happens by splitting a dataset on 1 variable, and then applying the same plot function on each split, and finally combining the plots using subplot.

If I facet a dataframe on 1 variable, but color on another, I might end up having a different subset of variables to color by in each subplot, causing the same value to have different colors between subplot.

library(plotly)
library(dplyr)
iris%>%
  mutate(name = unlist(c(rep(c("A", "B"), 30), rep(c("A", "B", "C"), 30)))) %>% 
  group_by(Sepal.Length > 5) %>%
  do(p=plot_ly(., x = ~Sepal.Length, y = ~Sepal.Width, color = ~name, type = "scatter")) %>%
  subplot(nrows = 1, shareX = TRUE, shareY = TRUE)

enter image description here

On the left subplot, datapoints with the name "B" are colored blue, while on the right subplot, they are colored red.

How can I ensure that the colors are stable across Subplots?

Keeping duplicates out of the legend would also be a plus!


Solution

  • You need to have your names as factor and only show the legend for one of the subplots.

    Here, I am setting showlegend to Sepal.Length > 5 because that group has all the names/levels. If you have cases where no single group covers all the names, then we need to do a little more data preparation beforehand.

    iris %>%
      mutate(name = factor(unlist(c(rep(c("A", "B"), 30), 
                                    rep(c("A", "B", "C"), 30))))) %>% 
      group_by(Sepal.Length > 5) %>%
      do(p=plot_ly(., x = ~Sepal.Length, y = ~Sepal.Width, 
                   color = ~name, type = "scatter", mode = "markers",
                   showlegend = ~unique(Sepal.Length > 5))) %>%
      subplot(nrows = 1, shareX = TRUE, shareY = TRUE)
    

    Created on 2024-03-26 with reprex v2.0.2