简体   繁体   中英

How to fill box plots with a user-defined color in R plotly?

This question is obviously a replicate from this thread .

However I could not apply the solution given in the comments, hence my question.

I want to use user-defined colors to fill box plots with plotly.

In the following code I would expect the boxes to be filled respectively with red and blue, but they keep the default colors.

library(plotly)

d1 <- data.frame(val = rnorm(n=1000), type=1)
d2 <- data.frame(val = rnorm(n=1000), type=2)
col1 <- "red"
col2 <- "blue"

plot_ly() %>%
  add_trace(data = d1, y = ~val, type="box", colors=col1) %>%
  add_trace(data = d2, y = ~val, type="box", colors=col2) 

I have managed to change the lines' and markers' colors, but not the fill.

Use the parameter fillcolor

plot_ly() %>%
    add_trace(data = d1, y = ~val, type = "box", fillcolor = col1) %>%
    add_trace(data = d2, y = ~val, type = "box", fillcolor = col2)

在此处输入图像描述

How about this

plot_ly(colors=c(col1, col2)) %>%
  add_trace(data = d1, y = ~val, type="box", color=I(col1)) %>%
  add_trace(data = d2, y = ~val, type="box", color=I(col2)) 

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM