Coloured Box Plot With Precomputed Quartiles

Viewed 248

I'm trying to colour a boxplot with custom values, but it remains in the default blue colour.

For example - see the code in the official tutorial: https://plotly.com/r/box-plots/#box-plot-with-precomputed-quartiles. When I add the code for coloring, nothing happens with the colours:

fig <- plot_ly(y = list(1,2,3,4,5,6,7,8,9), type = "box", q1=list(1, 2, 3), median=list(4, 5, 6),
               q3=list(7, 8, 9 ), lowerfence=list(-1, 0, 1),
               upperfence=list(5, 6, 7), mean=list(2.2, 2.8, 3.2 ),
               sd=list(0.2, 0.4, 0.6), notchspan=list(0.2, 0.4, 0.6),
               color = list(1,1,2),
               colors = list("red2", "grey"))
fig

What am I doing wrong?

1 Answers

The example has several issues, so I don't know if I understood the aim of the OP correctly. In the following, I removed the dummy ydata and replaced it with ID characters, used vectors instead of lists (except for y) and a formula with ~ for the color mapping:

library("plotly")
fig <- plot_ly(
               y = c("A", "B", "C"),
               type = "box", 
               q1 = c(1, 2, 3), 
               median = c(4, 5, 6),
               q3 = c(7, 8, 9 ), 
               lowerfence = c(-1, 0, 1),
               upperfence = c(5, 6, 7), 
               mean = c(2.2, 2.8, 3.2 ),
               sd = c(0.2, 0.4, 0.6), 
               notchspan = c(0.2, 0.4, 0.6),
               color = ~ c("a", "a", "c"),
               colors = c("red2", "grey")
)
fig

Colored plotly boxplots

For the mean and sd values, I left the original dummy values as in the original example.

Related