Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I force identical colors for all plots in a subplot in plotly?

Tags:

r

plotly

r-plotly

In the following example, I have four box plots within a plotly subplot. Each of the four box plots in this example has 3 variables: stocks, bonds and cash. In each of the box plots, I’d like stocks to show in the same color (e.g., blue), bonds to show in the same color (e.g., red), and cash to show as a third color. The code I have below causes me to have 12 colors, not 3. I’ve made a simple example. In my actual problem, the number of variables will be determined at runtime, so I can’t hard-code the colors easily. I’d like to make a call to a palette.

library(RColorBrewer)
library(plotly)
set.seed(101)
tbl1y <- data.frame(stocks = rnorm(1000,10,15),
                   bonds = rnorm(1000, 7, 8),
                   cash = rnorm(1000,3,1))
tbl3y <- data.frame(stocks = rnorm(1000,10,15*0.75),
                    bonds = rnorm(1000, 7, 8*0.75),
                    cash = rnorm(1000,3,1*0.75))
tbl5y <- data.frame(stocks = rnorm(1000,10,15*0.5),
                    bonds = rnorm(1000, 7, 8*0.5),
                    cash = rnorm(1000,3,1*0.5))
tbl10y <- data.frame(stocks = rnorm(1000,10,15*0.25),
                    bonds = rnorm(1000, 7, 8*0.25),
                    cash = rnorm(1000,3,1*0.25))

create_1boxplot <- function(tbl, n, vnames){
    mypalette <- brewer.pal(length(vnames), "Dark2")
    p <- plot_ly(data = tbl, type="box")
    for(i in vnames){
        p <- p %>% 
            add_trace(y = tbl[,i], name = i)
    }
    a<-list(text=paste("Boxplot of", n, "Year Returns"),
            xref = "paper",
            yref = "paper",
            yanchor = "bottom",
            xanchor = "center",
            color = vnames,
            colors = mypalette,
            align = "left",
            valign = "top",
            x = 0.5,
            y = 1,
            showarrow = FALSE)
    p <- p %>% layout(annotations=a)
    return(p)
}

vnames <- c("stocks", "bonds", "cash")
p1 <- create_1boxplot(tbl1y, 1, vnames = vnames)
p3 <- create_1boxplot(tbl3y, 3, vnames = vnames)
p5 <- create_1boxplot(tbl5y, 5, vnames = vnames)
p10 <- create_1boxplot(tbl10y, 10, vnames = vnames)

subplot(p1, p3, p5, p10, titleX=FALSE, titleY=FALSE, nrows=2, margin=0.05) %>%
    layout(showlegend = FALSE,
           yaxis = list(title = ""), 
           xaxis = list(title = ""))

enter image description here

like image 876
RMacey Avatar asked Nov 07 '22 23:11

RMacey


1 Answers

A solution can be found here. In brief, add 1 to the first parameter (n) of the brewer.pal function call; and, add colorway=mypalette to the call to layout in the create_1boxplot function.

like image 82
RMacey Avatar answered Nov 14 '22 03:11

RMacey