Racing Tadpole
Racing Tadpole

Reputation: 4360

How to set multiple colours in a ggplot2 stat_summary plot?

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

Upvotes: 3

Views: 9087

Answers (1)

Jake Burkhead
Jake Burkhead

Reputation: 6535

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

Upvotes: 5

Related Questions