6

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.

lll
  • 1,049
  • 2
  • 13
  • 39
  • 1
    Create a manusal palette? 14 colours is a lot to diferentiate though. Something like [this](http://stackoverflow.com/questions/9968975/using-ggplot2-in-r-how-do-i-make-the-background-of-a-graph-different-colours-in) might help. – Heroka Dec 08 '15 at 19:50
  • 1
    What are you trying to convey? Who is your audience? What do they care about? Have you considered facets? I would be very surprised if 14 different colors is the best way to differentiate the information being presented. – JasonAizkalns Dec 08 '15 at 19:55
  • I used facets but did not see how that can be super helpful. This graphs is mainly by myself and I need to be able to distinguish between the different levels – lll Dec 08 '15 at 20:03
  • If I look at your data, you seem to have only two seasons (spring and autumn). You could thus consider using only two colors, as the year is easily distinguishable from the graph. – Heroka Dec 08 '15 at 21:33
  • but if I only use 2 colors, what code should i write to make them alternating. It seems that if I just use m3+scale_color_brewer(palette="Dark2") I will get an error. – lll Dec 09 '15 at 02:05

1 Answers1

22

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.

Nancy
  • 3,989
  • 5
  • 31
  • 49
  • 7
    `colorRampPalette(brewer.pal(name="Dark2", n = 8))(14)` – rawr Dec 08 '15 at 20:16
  • Oh nice, that works too. I was also hoping that my answer would provide an "aha!" of how one can work with color palettes in general. ```colorRampPalette``` is quick, but combining two palettes might allow OP to hand-pick colors that are more easily distinguishable. – Nancy Dec 08 '15 at 20:20
  • 1
    Shouldn't the scale_color_brewer provide a way to do this. Seems odd to use or learn 2 different apis and its burdensome... – Sid Apr 28 '17 at 23:01
  • `could not find function "brewer.pal"` – untill Aug 21 '19 at 09:11
  • untill: Did you load RColorBrewer package via the library(...) load function? If not installed you need to install it. Maybe add: `if (!require("RColorBrewer")) { install.packages("RColorBrewer") library(RColorBrewer) }` at top of your code. – stephanmg Aug 21 '19 at 10:37