2016-08-12 122 views
0

我想绘制两列原始数据(我已经使用融合将它们合并为一个数据框),然后为每个添加单独的错误栏。但是,我想为每列的原始数据制作一对颜色,而错误栏则制作另一组颜色,但似乎无法使其工作。我得到的阴谋是在下面的链接。我想为原始数据和错误栏使用不同的颜色对。出于说明的目的,下面编码一个简单的可再现的例子。如何更改stat_summary()中的颜色

dat2.m<-data.frame(obs=c(2,4,6,8,12,16,2,4,6),variable=c("raw","raw","raw","ip","raw","ip","raw","ip","ip"),value=runif(9,0,10)) 

    c <- ggplot(dat2.m, aes(x=obs, y=value, color=variable,fill=variable,size = 0.02)) +geom_jitter(size=1.25) + scale_colour_manual(values = c("blue","Red")) 

    c<- c+stat_summary(fun.data="median_hilow",fun.args=(conf.int=0.95),aes(color=variable), position="dodge",geom="errorbar", size=0.5,lty=1) 

    print(c) 

[1]: http://i.stack.imgur.com/A5KHk.jpg

+0

你能发布一个可重复的例子吗?或者告诉我们情节中出了什么问题? – jdobres

+0

剧情没有错,我只是不确定如何让误差线成为与原始数据不同的颜色对。 – user85727

+0

我可以发布一个可重复的例子,当然 – user85727

回答

0

一个解决此方法是使用geom_pointstat_summary重复调用。使用这些函数的参数data将数据集的子集提供给每个调用,并将颜色属性设置为aes()之外。这是重复性的,有点击败ggplot的紧凑性,但它会。

c <- ggplot(dat2.m, aes(x = obs, y = value, size = 0.02)) + 
    geom_jitter(data = subset(dat2.m, variable == 'raw'), color = 'blue', size=1.25) + 
    geom_jitter(data = subset(dat2.m, variable == 'ip'), color = 'red', size=1.25) + 
    stat_summary(data = subset(dat2.m, variable == 'raw'), fun.data="median_hilow", fun.args=(conf.int=0.95), color = 'pink', position="dodge",geom="errorbar", size=0.5,lty=1) + 
    stat_summary(data = subset(dat2.m, variable == 'ip'), fun.data="median_hilow", fun.args=(conf.int=0.95), color = 'green', position="dodge",geom="errorbar", size=0.5,lty=1) 

print(c) 

enter image description here

0

为了记录:我认为这是一个非常,非常糟糕的主意。除非你有一个至关重要的用例,否则我认为你应该重新检查你的计划。

但是,您可以通过添加一组新变量来避开它,并在末尾填充一个空格。你会/需要玩的传说,但这应该工作(虽然它肯定是丑陋的):

dat2.m<- data.frame(obs=c(2,4,6,8,12,16,2,4,6),variable=c("raw","raw","raw","ip","raw","ip","raw","ip","ip"),value=runif(9,0,10)) 


c <- ggplot(dat2.m, aes(x=obs, y=value, color=variable,fill=variable,size = 0.02)) +geom_jitter(size=1.25) + scale_colour_manual(values = c("blue","Red","green","purple")) 

c<- c+stat_summary(fun.data="median_hilow",fun.args=(conf.int=0.95),aes(color=paste(variable," ")), position="dodge",geom="errorbar", size=0.5,lty=1) 

print(c)