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

16,533

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.

Share:
16,533
lll
Author by

lll

Updated on June 12, 2022

Comments

  • lll
    lll almost 2 years

    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.

  • rawr
    rawr over 8 years
    colorRampPalette(brewer.pal(name="Dark2", n = 8))(14)
  • Nancy
    Nancy over 8 years
    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.
  • Sid
    Sid about 7 years
    Shouldn't the scale_color_brewer provide a way to do this. Seems odd to use or learn 2 different apis and its burdensome...
  • untill
    untill over 4 years
    could not find function "brewer.pal"
  • stephanmg
    stephanmg over 4 years
    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.