0

I have a list of lists that I want to plot each list's distribution. Each distribution should be in a different color. Is there any predefined list of colors in R so that I wouldn't need to write the list of colors myself? (The list might get pretty long and I don't want to check everytime, if the length of list is the same with the data!)

Another issue is with the x- and y-axis limits. How can I put them in a way that covers all the values showing in the plot? (If you run the code bellow, the red line is cut on the right)

What I have written so far is:

dput(degree.l)
list(c(schwab = 0, pnc = 0.0344827586206897, jpm = 0.0862068965517241, 
amex = 0.0862068965517241, gs = 0.103448275862069, ms = 0.103448275862069, 
bofa = 0.103448275862069, citi = 0.103448275862069, wf = 0.120689655172414, 
spgl = 0.120689655172414, brk = 0.137931034482759), c(schwab = 0.0166666666666667, 
pnc = 0.05, ms = 0.0666666666666667, spgl = 0.0833333333333333, 
jpm = 0.1, bofa = 0.1, wf = 0.1, amex = 0.1, gs = 0.116666666666667, 
brk = 0.116666666666667, citi = 0.15), c(schwab = 0.0428571428571429, 
gs = 0.0714285714285714, pnc = 0.0714285714285714, citi = 0.0857142857142857, 
amex = 0.0857142857142857, spgl = 0.0857142857142857, jpm = 0.1, 
brk = 0.1, ms = 0.114285714285714, wf = 0.114285714285714, bofa = 0.128571428571429
))

colors <- c("red","blue")
density.plot <- function(cent.l){
  plot(density(cent.l[[1]]), col="orangered")
  for(i in 2:length(cent.l)){
    lines(density(cent.l[[i]]), col= colors[i-1])
  }

}
density.plot(degree.l)
statwoman
  • 380
  • 1
  • 9
  • 1
    For your first question, ```RColorBrewer``` has pre defined palettes that you can index. Idk though if there are palettes that have many (>100) colors. – Harry Smith Jul 22 '21 at 17:17
  • I actually tried that but it has some really light colors in it, which makes the plot difficult to read. Thanks though. @HarrySmith – statwoman Jul 22 '21 at 17:33
  • 1
    https://stackoverflow.com/questions/15282580/how-to-generate-a-number-of-most-distinctive-colors-in-r may be useful – user20650 Jul 22 '21 at 19:55

1 Answers1

0

You want to select a color palette that is qualitative to distinguish different lines. To get the axes right you have to compute all of the densities before plotting:

degree.d <- lapply(degree.l, density)
rng <- sapply(degree.d, function(y) range(y$x))
xrng <-c(min(rng[1]), max(rng[2]))
yrng <- c(0, max(sapply(degree.d, function(z) max(z$y))))

This computes the x-axis and y-axis ranges for your plot by getting the range over all of the lines. Now get your colors:

hcl.pals("qualitative")
# [1] "Pastel 1" "Dark 2"   "Dark 3"   "Set 2"    "Set 3"    "Warm"     "Cold"     "Harmonic" "Dynamic" 
clrs <- hcl.colors(3, "Dark 3")

We list the qualitative palettes and choose three colors from palette "Dark 3". Now we can plot:

plot(NA, xlim=xrng, ylim=yrng)
invisible(lapply(1:3, function(z) lines(degree.d[[z]]$x, degree.d[[z]]$y, col=clrs[z])))

Density Plot

dcarlson
  • 10,936
  • 2
  • 15
  • 18