2014-02-18 90 views
0

下面是产生数据和ggplot2分面线图的代码,它似乎工作正常。但是,我想根据另一个(组)的值更改线的颜色,这在当前不在我的示例中。我试过添加:在一个分面线图中按分组变化线条,每个分面只有一条线

+element_line(color=combined$group)) 

但它没有改变任何东西。我正在尝试做什么?再次,我想在每个方面只保留1行,但根据“组”的值更改颜色。在此先感谢您的帮助!

require(ggplot2)  
Year<-c(rep(2012,10),rep(2013,10),rep(2014,10)) 
MULTDV<-rnorm(30) 
Construct<-factor(rep(1:10,3)) 
group<-rep(c(rep(1,5), rep(2,5)),3) 

combined<-as.data.frame(cbind(Year, MULTDV, Construct, group)) 
qplot(factor(Year), MULTDV, data=combined, xlab=NULL, ylab=NULL, stat="identity") + stat_summary(fun.y=mean, colour="steelblue3", geom="line", aes(group=1))+facet_wrap(~Construct, nrow = 2) 

回答

0

既然这样,你的代码不会产生“刻面线图”和FactTest没有任何地方所定义。但是,下面的代码应该让你接近你想要做什么:

combined<-data.frame(Year, MULTDV, Construct, group) 

qplot(factor(Year), MULTDV, data=combined, 
     geom = "line", group = Construct, color = factor(group), 
     facets = ~ Construct) 
0

我最终行动做以下,这工作,虽然有可能是一个更优雅的方式来做到这一点。

require(ggplot2)  
Year<-c(rep(2012,10),rep(2013,10),rep(2014,10)) 
MULTDV<-rnorm(30) 
Construct<-factor(rep(1:10,3)) 
group<-rep(c(rep(1,5), rep(2,5)),3) 
combined<-as.data.frame(cbind(Year, MULTDV, Construct, group)) 
cols<- c("1"="steelblue3","2"="red") 

ggplot(combined,aes(x=factor(Year),y=MULTDV))+ 
    stat_summary(fun.y=mean, geom="line", aes(group=2, colour=factor(group)))+ 
    facet_wrap(~Construct, nrow = 2)+ 
    geom_point()+ 
    scale_colour_manual(values = cols, guide="none")+ 
    theme(legend.position = "none") 

aaronwolen的代码似乎给了我同样的结果,当我试了一下,我刚刚已经想通了这个其他的方式之前,我看到了他的回应。谢谢阿隆!