Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: How to change the color scheme (14 colors needed) in ggplot

I used the following code to create a plot using ggplot:

m = ggplot(derv, aes(x=Date, y=derv, colour = Season)) + geom_point() 
m2 = m+geom_abline(intercept = 0, slope = 0)
m3 = m2 + geom_abline(intercept = 2.578269274, slope = 0)
m3 = m3 + geom_abline(intercept = -1.4242559021, slope = 0) 

enter image description here

This plot looks beautiful but for some intervals such as 2010sp and 2010au, it is hard for me to tell when the color changed. So I want to change the color scheme of this plot.

and I have tried the following code:

 m3+scale_color_brewer(palette="Dark2")

but I am getting a warning message:

enter image description here

2: In RColorBrewer::brewer.pal(n, pal) :
n too large, allowed maximum for palette Dark2 is 8
Returning the palette you asked for with that many colors

and I have checked the palettes available, the biggest one contains 12 colors but I need 14, so I am wondering if there is way to resolve this issue.

like image 370
lll Avatar asked Dec 08 '15 19:12

lll


1 Answers

If you really want/need more colors than are given in a palette, you can concatenate two different palettes and then use scale_color_manual. I couldn't reproduce your example, but hopefully this communicates the general point:

mycolors = c(brewer.pal(name="Dark2", n = 8), brewer.pal(name="Paired", n = 6))

ggplot(derv, aes(x=Date, y=derv, colour = Season)) + 
  geom_point() +
  geom_abline(intercept = 0, slope = 0) + 
  geom_abline(intercept = 2.578269274, slope = 0) + 
  geom_abline(intercept = -1.4242559021, slope = 0) +
  scale_color_manual(values = mycolors)

As an aside, it would be great if you could provide a minimal and reproducible example next time. Perhaps just take a subset of your data and dput() it into the question, or use one of the data sets you'll find by running data() that most closely resembles the format of your data.

like image 143
Nancy Avatar answered Nov 09 '22 20:11

Nancy