0

I need to plot a line chart that has more than 50 different lines. I am using RColorBrewer to generate 50 unique colors to plot it. However some of the generated colors are very hard to read (e.g. bright yellow). Is there anyway to generate a set of colors that are easier to read?

library(tidyverse)
library(RColorBrewer)
library(plotly)

colors_ = colorRampPalette(brewer.pal(10,"Spectral"))(50)

grps =  sort(rep(c(1:50),2)) %>%  as.character() %>%  unique
dat = data.frame(x = rep(c(1,2),50)  ,  y = sort(rep(c(1:50),2)) , grp =as.character(sort(rep(c(1:50),2)))  )

p  = plot_ly(dat ,type = 'scatter', mode = 'lines')         

for (i in 1:50){
  p <- p %>% add_trace(data = filter(dat , grp ==grps[i] ) ,  x = ~x, y=~y,
                       line = list(color = colors_[i]),
                       text = grps[i]
  )
}
p

enter image description here

SG_
  • 69
  • 1
  • 6
  • 8
    *50 colors* shall never be easy to view. – Rui Barradas Jul 09 '22 at 06:38
  • 1
    Indeed, 50 colours is hard to view. Another issue is that when only 3 colours is needed, colorRampPalette(brewer.pal(10,"Spectral"))(3) still generate the bright yellow which is hard to see – SG_ Jul 09 '22 at 06:44
  • 2
    Here are some relevant similar questions. Once you get more than 10-20 colors, distinguishing them gets harder and harder. https://stackoverflow.com/questions/10014271/generate-random-color-distinguishable-to-humans. https://stackoverflow.com/questions/61049305/generate-a-color-palette-of-n-colors-as-distinctive-and-far-apart-as-possible – Jon Spring Jul 09 '22 at 07:24
  • 1
    Plotting 50 lines is almost certainly not the way to go. Before trying to find 50 colours, consider fct_lump() functions from forcats, or a different plot altogether! – jpenzer Jul 09 '22 at 08:29
  • 1
    Obviously RuiBarradas, JonSpring, SG_ and jpenzer are right, this is an unrealistic goal, but if you have no alternative (e.g. your boss says "get me 50 colors or you're fired!") you can join all of the qualitative palettes from RColorBrewer and a normal person can potentially differentiate between 50 of them, per https://stackoverflow.com/a/33144808/12957340 – jared_mamrot Jul 09 '22 at 11:43
  • 1
    You also might find this discussion helpful: https://stats.stackexchange.com/questions/118033/best-series-of-colors-to-use-for-differentiating-series-in-publication-quality –  Jul 09 '22 at 13:04

0 Answers0