Search code examples
rplotly

Plotly Warning: RColorBrewer::brewer.pal(N, "Set2") :


I am working to replicate the plotly density plot example here in R. The code used on the example page is

library(plotly)
dens <- with(diamonds, tapply(price, INDEX = cut, density))
df <- data.frame(
  x = unlist(lapply(dens, "[[", "x")),
  y = unlist(lapply(dens, "[[", "y")),
  cut = rep(names(dens), each = length(dens[[1]]$x))
)
fig <- plot_ly(df, x = ~x, y = ~y, color = ~cut) 
fig <- fig %>% add_lines()
fig

Of course this works just fine and a quick preview of the data is

> str(df)
'data.frame':   2560 obs. of  3 variables:
 $ x  : num  -1115 -1073 -1032 -991 -949 ...
 $ y  : num  6.74e-08 9.02e-08 1.20e-07 1.57e-07 2.06e-07 ...
 $ cut: chr  "Fair" "Fair" "Fair" "Fair" ...

Now, with my own data (see dput() below for reproducing), the plot is created, but yields a warning

fig <- plot_ly(df2, x = ~theta, y = ~values, color = ~ind)
fig <- fig %>% add_lines()
fig
Warning messages:
1: In RColorBrewer::brewer.pal(N, "Set2") :
  minimal value for n is 3, returning requested palette with 3 different levels

2: In RColorBrewer::brewer.pal(N, "Set2") :
  minimal value for n is 3, returning requested palette with 3 different levels

The structure of my data are

> str(df2)
'data.frame':   34 obs. of  3 variables:
 $ values: num  0.0585 0.1003 0.1672 0.2672 0.4018 ...
 $ ind   : chr  "X1" "X1" "X1" "X1" ...
 $ theta : num  -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0.5 ...

The documentation on page 3 does specifically state that "...a warning if a palette you asked for exists but not with as many different levels as you asked for." My data in this case have fewer than 3 in ind.

However, what I cannot find now is if there is a palette I can set for < 3. I'd rather not supress or just ignore the warning. Any advice for resolution?

Thanks. Data for reproducing are below.

> dput(df2)
structure(list(values = c(0.0584990041577079, 0.100254537023576, 
0.167189618781583, 0.267205773945374, 0.401760675391809, 0.55864009227321, 
0.712171241231567, 0.836160108570042, 0.918694081341489, 0.964460695673611, 
0.986034362720271, 0.994936330930512, 0.998262836587041, 0.999424735793067, 
0.999813543824513, 0.999940322471119, 0.999981037752802, 0.000454891425316739, 
0.00138118245593599, 0.0040618318346699, 0.0113553955978209, 
0.0294536830165663, 0.0690401859334859, 0.143049939374408, 0.258712591235403, 
0.407910135546857, 0.566494475339215, 0.707280909084666, 0.81478103878707, 
0.888097594376448, 0.934397771015761, 0.962244935056505, 0.978507400496616, 
0.987842047415269), ind = c("X1", "X1", "X1", "X1", "X1", "X1", 
"X1", "X1", "X1", "X1", "X1", "X1", "X1", "X1", "X1", "X1", "X1", 
"X2", "X2", "X2", "X2", "X2", "X2", "X2", "X2", "X2", "X2", "X2", 
"X2", "X2", "X2", "X2", "X2", "X2"), theta = c(-4, -3.5, -3, 
-2.5, -2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 
-4, -3.5, -3, -2.5, -2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2, 2.5, 
3, 3.5, 4)), row.names = c(401L, 451L, 501L, 551L, 601L, 651L, 
701L, 751L, 801L, 851L, 901L, 951L, 1001L, 1051L, 1101L, 1151L, 
1201L, 2002L, 2052L, 2102L, 2152L, 2202L, 2252L, 2302L, 2352L, 
2402L, 2452L, 2502L, 2552L, 2602L, 2652L, 2702L, 2752L, 2802L
), class = "data.frame")

Solution

  • Maybe it is good idea to define colors directly by args.
    Below is an example;

    fig <- plot_ly(df2, x = ~theta, y = ~values, color = ~ind, colors = c("red", "blue"))
    

    [ADDITION]

    I noticed when I give palette name of RColorBrewer, warning doesn't occur.

    Maybe
    plot_ly(df2, x = ~theta, y = ~values, color = ~ind, colors = "Set2")
    is a better solution.

    NOTE: this is equivalent to
    plot_ly(df2, x = ~theta, y = ~values, color = ~ind, colors = RColorBrewer::brewer.pal(3, "Set2")[1:2])