简体   繁体   中英

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

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"))

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM