如何在ggplot2 stat_summary图中设置多种颜色? [英] How to set multiple colours in a ggplot2 stat_summary plot?
问题描述
如何使用 ggplot2
和 stat_summary
来显示我选择的颜色?例如:
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)
)
$ b这个命令使用默认的颜色绘制每个农场的交易量与期间的分布:
ggplot(simVol,aes(x = period,y = volume,color = farm))+
stat_summary(fun.data =P10meanP90,geom =平滑,size = 2)
但是,如果我添加
color ='绿色
添加到stat_summary
的参数中,而是绘制了农场之间的聚合。我尝试过使用color = c('green','orange')
,但这仍然只显示一条绿线。
谢谢
$ b $ p解决方案 / div>
scale_colour_manual
是您正在寻找的功能。 http://docs.ggplot2.org/0.9.3.1/scale_manual.html
pre $ggplot(simVol,aes(x = period,y = volume,color = farm))+
stat_summary (fun.data =P10meanP90,geom =smooth,size = 2)+
scale_colour_manual(values = c(green,orange))
How do I use
ggplot2
withstat_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 ofstat_summary
, it plots instead the aggregate across farms. I've tried usingcolour=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.htmlggplot(simVol, aes(x=period, y=volume, colour=farm)) + stat_summary(fun.data="P10meanP90", geom="smooth", size=2) + scale_colour_manual(values = c("green", "orange"))
这篇关于如何在ggplot2 stat_summary图中设置多种颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文