Subplot no longer uses different colors

One or two months ago, when creating subplots, Plotly automatically assigned different colors to the different plots. Now it doesn’t do that any more. Every plot has the same color. See the first example in the tutorial about subplots:

library(plotly)
p1 <- plot_ly(economics, x = ~date, y = ~unemploy) %>%
add_lines(name = ~“unemploy”)
p2 <- plot_ly(economics, x = ~date, y = ~uempmed) %>%
add_lines(name = ~“uempmed”)
subplot(p1, p2)

Both graphs are printed in blue, which is not ideal. Before, and in the tutorial example, one graph was printed in blue, the other in orange.

Is that due to a new release? How can I make sure that different colors are assigned automatically, just as they were before?

***** Update: ****
Note also the difference between the help center and the tutorial “plotly for r”:

Is this a bug or do I make a mistake? If it’s a feature: how can I create the subplots in different colors?

Hey @frb,

They’re blue and orange for me on plotly_4.7. Perhaps try updating to the dev version devtools::install_github("ropensci/plotly") and see if that works for you.

Thanks a lot, that helped! The new version 4.7 can even be updated on CRAN.

Hi,
In my case, I have the opposite problem…
I would like plotly to keep using the colors I have specified when creating the original plots, but somehow when using subplotly(), the 2 plots divert to use a different set of colors on both plots (instead of the same ones as I intended).

Do you guys know how to make subplotly not override the colors so that simply the original colors are passed on to subplotly() ?

Thanks

Hey @Yorye2013,

Do you have an example you could share?

Hi @bcd,
Sure, see the below example:

data.toPlot = mtcars

# First Plot
xform <- list(categoryorder = "array",
              categoryarray = rownames(data.toPlot))

nodes.Vector = colnames(data.toPlot)
for (i in 1:length(nodes.Vector) ) {
    if (i==1) {
        p = plot_ly(data = data.toPlot, 
                    x =  rownames(data.toPlot) , 
                    y = data.toPlot[, i],
                    type = 'bar', 
                    name = nodes.Vector[i],
                    legendgroup = nodes.Vector[i],
                    alpha = 0.8
        )  %>%
            layout( title = paste0('Example 1'),
                    xaxis = xform,
                    yaxis = list(title = 'values'), 
                    barmode = 'relative',
                    margin = list(b = 160)
            )
    } else {
        p = p %>% add_trace(y = data.toPlot[, i], 
                            name = nodes.Vector[i],
                            legendgroup = nodes.Vector[i],
                            alpha = 0.8
        )
    }
}

# Second Plot
data.Sums = as.data.frame(t(apply(X = data.toPlot, MARGIN = 2, FUN = sum, na.rm = TRUE)))
xform <- list(categoryorder = "array",
              categoryarray = rownames(data.Sums))

nodes.Vector = colnames(data.Sums)
for (i in 1:length(nodes.Vector) ) {
    if (i==1) {
        p2 = plot_ly(data = data.Sums, 
                         x =  rownames(data.Sums) , 
                         y = data.Sums[, i],
                         type = 'bar', 
                         name = nodes.Vector[i],
                         legendgroup = nodes.Vector[i],
                         alpha = 0.8,
                         showlegend = F)
        # Add layout
        p2 = p2  %>%
            layout( title = paste0('Example 2'),
                    xaxis = xform,
                    yaxis = list(title = 'value'), 
                    barmode = 'relative',
                    margin = list(b = 160)
            )
    } else {
            p2 = p2 %>% add_trace(y = data.Sums[, i], 
                                  name = nodes.Vector[i],
                                  legendgroup = nodes.Vector[i],
                                  alpha = 0.8  )
    }
}

# Both combined
p.both = subplot(p, p2, shareY = FALSE, nrows = 1, widths =c(0.9, 0.1), titleX = TRUE, titleY = TRUE)

The above code gives 3 plots:
p, p2, and p.both

Basically, p and p2 have the same colors for the same column names (e.g. mgp, cyl, hp) and that’s they way I wanted them to be.

But p.both does something to the colors and changes the colors of the subplot ‘p2’ when displayed together with p.


Combined:

It looks like it is the way you’re handling these data. For example, below resolves the issue of the 2nd plot being out by 1 color step.

library(plotly)
library(dplyr)
library(reshape2)
library(RColorBrewer)

data.toPlot <- tibble::rownames_to_column(mtcars, "cars")
data.toPlot <- melt(data.toPlot)

mypalette<-brewer.pal(12,"Set3")

# First Plot
xform <- ~list(categoryorder = "array",
              categoryarray = cars)

p = plot_ly(
  data = data.toPlot, 
  x =  ~cars, 
  y = ~value,
  type = 'bar',
  color = ~variable,
  colors = mypalette,
  name = ~variable,
  legendgroup = ~variable,
  alpha = 0.8
  )  %>%
  layout( 
    title = paste0('Example 1'),
    xaxis = xform,
    yaxis = list(title = 'values'), 
    barmode = 'relative',
    margin = list(b = 160)
  )

# Second Plot
data.Sums = as.data.frame(t(apply(X = mtcars, MARGIN = 2, FUN = sum, na.rm = TRUE)))
data.Sums <- tibble::rownames_to_column(data.Sums, "id")
data.Sums <- melt(data.Sums)


p2 = plot_ly(
  data = data.Sums, 
  x =  ~id, 
  y = ~value,
  type = 'bar',
  color = ~variable,
  colors = mypalette,
  name = ~variable,
  legendgroup = ~variable,
  alpha = 0.8,
  showlegend = F
)  %>%
  layout( 
    title = paste0('Example 2'),
    yaxis = list(title = 'values'), 
    barmode = 'relative',
    margin = list(b = 160)
  )

# Both combined
p.both = subplot(p, p2, shareY = FALSE, nrows = 1, widths =c(0.9, 0.1), titleX = TRUE, titleY = TRUE)

1 Like

wow that’s fantastic! thanks a lot for that!