Home > Back-end >  Set subplot colors plotly R
Set subplot colors plotly R

Time:11-16

I have this code for subplots, i want to set de colors by a custom palette, but seems not to take any color.

data.frame(x = sample(letters[1:3],30,replace = TRUE)) %>%
  mutate(y = rnorm(30),
         z = runif(30,10,20)) %>%
  group_by(x) %>%
  do(p=plot_ly(.,x = ~y, y=~z, type = "scatter", mode = "lines", line = list(color = "blue","red","green")))) %>%
  subplot(nrows = 1,
                  shareX = FALSE,
                  shareY = TRUE,
                  margin = 0.0001)

CodePudding user response:

One option to achieve your desired result would be to map the grouping variable on the color "aesthetic" and to set the custom palette via the colors argument where personally I prefer to make use of a named vector to assign colors to categories:

library(plotly)

set.seed(123)

data.frame(x = sample(letters[1:3], 30, replace = TRUE)) %>%
  mutate(
    y = rnorm(30),
    z = runif(30, 10, 20)
  ) %>%
  group_by(x) %>%
  do(p = plot_ly(., x = ~y, y = ~z, color = ~x, type = "scatter", mode = "lines", colors = c(a = "blue", b = "red", c = "green"))) %>%
  subplot(
    nrows = 1,
    shareX = FALSE,
    shareY = TRUE,
    margin = 0.0001
  )

  • Related