2

I have used geom_rect to highlight 4 separate sections of my point plot graph, ggplot2 has used a default pink, green, blue and purple to shade in the rectangles, does anyone know of a way to change these default colours?

I didn't bother with a workable example but please let me know if you want one and I can post in comments

shilovk
  • 11,718
  • 17
  • 75
  • 74
Ningman
  • 89
  • 1
  • 8
  • Not bothering with a workable example is a good way to attract negative votes. Always include one – Michael Harper Apr 04 '18 at 10:38
  • Possible duplicate of [changing ggplot factor colors?](https://stackoverflow.com/questions/15130497/changing-ggplot-factor-colors) – Michael Harper Apr 04 '18 at 10:40
  • @MikeyHarper thanks for the advice, I've only been using R for about a week or so, after searching the internet for hours for a answer to this question even If I had of come across this question I wouldn't have recognised it as the answer I needed, as I don't know what I'm looking for, hence why I asked my own question – Ningman Apr 04 '18 at 11:05

1 Answers1

3

Using scale_fill_manual. An example:

library('ggplot2')  #v 0.9.0
library('scales')
(unemp <- qplot(date, unemploy, data=economics, geom="line", 
                 xlab = "", ylab = "No. unemployed (1000s)"))

presidential <- presidential[-(1:3), ]

yrng <- range(economics$unemploy)
xrng <- range(economics$date)
unemp + geom_vline(aes(xintercept = start), data = presidential)
unemp + geom_rect(aes(NULL, NULL, xmin = start, xmax = end, fill = party),
                   ymin = yrng[1], ymax = yrng[2],
                   data = presidential) + 
         scale_fill_manual(values = alpha(c("blue", "red"), 0.2))

More here: How can I have two different scale_fill_manual active in a ggplot command

Amar
  • 1,340
  • 1
  • 8
  • 20