Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set multiple colours in a ggplot2 stat_summary plot?

Tags:

r

ggplot2

How do I use ggplot2 with stat_summary to show colours of my choice? Eg.:

simVol  <- data.frame(simId=c(1,1,1,1,2,2,2,2), 
                      farm=rep(c('farm A', 'farm A', 'farm B', 'farm B'),2),
                      period=rep(1:2,4), 
                      volume=c(9,21,12,18,10,22,11,19))

P10meanP90 <- function(x) data.frame(
  y = mean(x), 
  ymin = quantile(x, .1),
  ymax = quantile(x, .9)
)

This command plots the distribution of volume at each farm against the period, using default colours:

ggplot(simVol, aes(x=period, y=volume, colour=farm)) + 
    stat_summary(fun.data="P10meanP90", geom="smooth", size=2)

However, if I add colour='green' to the arguments of stat_summary, it plots instead the aggregate across farms. I've tried using colour=c('green','orange'), but this still only shows a green line.

How do I change the colours in this plot?

thanks

like image 519
Racing Tadpole Avatar asked May 24 '26 19:05

Racing Tadpole


1 Answers

scale_colour_manual is the function you're looking for. http://docs.ggplot2.org/0.9.3.1/scale_manual.html

ggplot(simVol, aes(x=period, y=volume, colour=farm)) +
    stat_summary(fun.data="P10meanP90", geom="smooth", size=2) +
    scale_colour_manual(values = c("green", "orange"))

enter image description here

like image 185
Jake Burkhead Avatar answered May 26 '26 08:05

Jake Burkhead



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!