2016-04-23 157 views
2

我相信答案很简单,但目前它逃避我。我想使用stat_summary()制作折线图,在每个x轴刻度(代表一个单独的时间点),每个组的形状(代表实验条件)都有不同的形状。图形使用stat_summary形状

这里的数据

set.seed(124) 
ID <- rep(1:12, times = 3) 
Group <- rep(c("A", "B", "C"), times = 12) 
score <- rnorm(36, 25, 3) 
session <- rep(c("s1","s2", "s3"), each = 12) 

df <- data.frame(ID, Group, session, score) 

现在我可以通过制作手段表中的每个时间点到达那里。像这样。

gMeans <- aggregate(score ~ session + Group, data = df, mean) 

然后像这样绘制图形。

pMeans <- ggplot(data = gMeans, aes(x = session, y = score, group = Group, shape = Group)) + 
    geom_line(aes(linetype = Group), size = 1) + 
    geom_point(size = 5, fill = "white") + 
    scale_color_hue(name = "Group", l = 30) + 
    scale_shape_manual(name = "Group", values = c(23,22, 21)) + 
    scale_linetype_discrete(name = "Group") + 
    theme_bw() 

pMeans 

但是我想能够跳过具有通过使用stat_summary()做出的装置表中的步骤。我可以用不同的线条类型获得类似的图形,但是我无法弄清楚如何为每个组获得每个轴上的不同形状。我尝试了下面的代码和geom_point()geom_line()许多不同的排列,但无济于事。我如何修改下面的代码来获得看起来像从上面的代码派生的输出的输出?

pline <- ggplot(df, aes(x=session, y=score, group = Group, shape = Group)) + 
       stat_summary(fun.y="mean", geom="line", size=1.1, aes(linetype=Group, shape = Group)) + 
       scale_shape_manual(values=c(1:3)) 

pline 
+1

一个快速的方法是将功能添加到你的_point和_line调用:'ggplot(数据= DF,AES(X =会话,Y =得分,基团=集团,形状=集团))+ geom_line(size = 5,fill =“white”,stat =“summary”,fun.y =),我们可以看到geom_line(aes(linetype = Group),size = 1,stat =“summary”,fun.y = mean)+ 意思)' – user20650

+0

谢谢你的提示@ user20650。这很有帮助,它添加了形状,但是尽管在'geom_point'图层中调用了'fill = white',形状出现了黑色填充。任何想法,为什么这可能是? – llewmills

+0

你需要像你的例子一样添加scale_shape('scale_shape_manual(name =“Group”,values = c(23,22,21))') – user20650

回答

2

这将有助于还清理传说:

library(ggplot2) 

set.seed(124) 
ID <- rep(1:12, times = 3) 
Group <- rep(c("A", "B", "C"), times = 12) 
score <- rnorm(36, 25, 3) 
session <- rep(c("s1","s2", "s3"), each = 12) 

df <- data.frame(ID, Group, session, score) 

gg <- ggplot(df, aes(x=session, y=score, group = Group, shape = Group)) 
gg <- gg + stat_summary(fun.y="mean", geom="line", size=1.1, 
         aes(linetype = Group), show.legend=FALSE) 
gg <- gg + stat_summary(fun.y="mean", geom="point", size=5, 
         aes(shape = Group), fill="white") 
gg <- gg + scale_shape_manual(name = "Group", values = c(23, 22, 21)) 
gg <- gg + theme_bw() 
gg <- gg + theme(legend.key=element_blank()) 
gg 

的线被遮挡,所以它没有什么意义,让他们在图例中。由于您对线路使用stat_summary()(vs geom_line(),嵌入stat="summary",所以最好还是保留IMO geom的成语)。

enter image description here