ggplot没有绘制组之间的连接线意味着什么? [英] ggplot not drawing connection lines between group means any more?

查看:119
本文介绍了ggplot没有绘制组之间的连接线意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎错过了ggplot2工作方式的改变。我有一个统计教科书(A. Field(2012),发现使用R的统计数字,第593页)的例子,我确信它已经工作了,但现在不再在分组数据之间产生连接线。



以下是样本数据:

 参与者< -gl(20,9,labels = P(P01P02P03P04P05P06P07P08P09P10P11P12 ,P13,P14,P15,P16,P17,P18,P19,P20))
drink <-gl(3,3,180, (3,1,180,标签= c(正面,负面,中性))$ b(啤酒,酒,水))
图像< $ b groups <-gl(9,1,180,labels = c(beerpos,beerneg,beerneut,winepos,wineneg,wineneut,waterpos,waterneg (1,6,5,38,-5,4,10,-14,-2,26,27,27,23,-15,14,21, - 6,0,1,-19,-10,28,-13,13,33,-2,9,7,-18,6,26,-16,19,23,-17,5,22, - 8,4,34,-23,14,21,-19,0,30,-6,3,32,-22,21,17,-11,4,40,-6,0,24, - 9,19,15,-10,2,15,-9,4,29,-18,7,13,-17,8,20,-17,9,30,-17,12,16,-4 ,10,9,-12,-5,24,-15,18,17,-4,8,14,-11,7,34,-14,20,19,-1,12,43,30, 8,20,-12,4,9,-10,-13,15,-6,13,23,-15,15,29,-1,10,15,15,12,20,-15,6 ,6,-16,1,40,30,19,28,-4,0,20,-10,2,8,12,8,11,-2,6,27,5,-5,17, 17,15,17,-6,6,9,-6,-13,30,21,21,15,-2,16,19,-20,3,34,23,28,27,-7, 7,12,-12,2,34,20,26,24,-10,12,12,-9,4)

长度<-data.frame(参与者,饮品,影像,团体,态度)

这里是剧情代码:

  library(ggplot2)
attitudeInt< - ggplot(longAttitude,aes(drink,attitude,color = imagery))
attitudeInt + stat_summary(fun。 y = mean,geom =point)+
stat_summary(fun.y = mean,geom =line,aes(group = imagery))+
stat_summary(fun.data = mean_cl_boot,geom =errorbar,width = 0.2)+
labs(x =饮料类型,y =平均态度,color =图像类型)

是不是这种情况下,部分 stat_summary(fun.y = mean,geom =line,aes(group = imagery))应该准确地绘制这些连接线?



感谢您的帮助。

这并不能解决 ggplot2 中的问题,但这是一个解决方法。

首先总结数据。在原始代码中,您使用 mean_cl_boot 来计算置信区间,然后在本示例中使用。

  library(plyr)
dfAtt <-ddply(longAttitude,〜drink + imagery,function(x)mean_cl_boot(x $ attitude))

dfAtt
饮料图像y ymin ymax
1啤酒正面21.05 15.65000 26.90750
2啤酒负面4.45 -2.60125 12.00000
3啤酒中性10.00 5.49750 14.75000
4正面葡萄酒25.35 22.40000 28.25000
5葡萄酒负-12.00 -14.40000 -9.49875
6葡萄酒中立11.65 8.95000 14.40125
7积水17.40 14.40000 20.45000
8水不足-9.20 -12.25000 -6.34875
9水中性2.35 -0.75125 4.90000

然后绘制您的数据:

  ggplot(dfAtt,aes(x = drink,y = y,color = imagery,group = imagery))+ 
geom_errorbar(aes(ymin = ymin,ymax = ymax ),width = .2)+
geom_line()+
geom_point()+
labs(x =饮料类型,y =平均态度,color =图像类型)


I seem to have missed a change within how ggplot2 works. I have an example from a statistics textbook (A. Field (2012), Discovering statistics using R, p. 593) which I'm sure worked but now does not produce connection lines between means of grouped data any more.

Here's the sample data:

participant<-gl(20, 9, labels = c("P01", "P02", "P03", "P04", "P05", "P06", "P07", "P08", "P09", "P10", "P11", "P12", "P13", "P14", "P15", "P16", "P17", "P18", "P19", "P20" ))
drink<-gl(3, 3, 180, labels = c("Beer", "Wine", "Water"))
imagery<-gl(3, 1, 180, labels = c("Positive", "Negative", "Neutral"))
groups<-gl(9, 1, 180, labels = c("beerpos", "beerneg", "beerneut", "winepos", "wineneg", "wineneut", "waterpos", "waterneg", "waterneut"))
attitude<-c(1, 6, 5, 38, -5, 4, 10, -14, -2, 26, 27, 27, 23, -15, 14, 21, -6, 0, 1, -19, -10, 28, -13, 13, 33, -2, 9, 7, -18, 6, 26, -16, 19, 23, -17, 5, 22, -8, 4, 34, -23, 14, 21, -19, 0, 30, -6, 3, 32, -22, 21, 17, -11, 4, 40, -6, 0, 24, -9, 19, 15, -10, 2, 15, -9, 4, 29, -18, 7, 13, -17, 8, 20, -17, 9, 30, -17, 12, 16, -4, 10, 9, -12, -5, 24, -15, 18, 17, -4, 8, 14, -11, 7, 34, -14, 20, 19, -1, 12, 43, 30, 8, 20, -12, 4, 9, -10, -13, 15, -6, 13, 23, -15, 15, 29, -1, 10, 15, 15, 12, 20, -15, 6, 6, -16, 1, 40, 30, 19, 28, -4, 0, 20, -10, 2, 8, 12, 8, 11, -2, 6, 27, 5, -5, 17, 17, 15, 17, -6, 6, 9, -6, -13, 30, 21, 21, 15, -2, 16, 19, -20, 3, 34, 23, 28, 27, -7, 7, 12, -12, 2, 34, 20, 26, 24, -10, 12, 12, -9, 4)

longAttitude<-data.frame(participant, drink, imagery, groups, attitude)

And here's the plot code:

library(ggplot2)
attitudeInt <- ggplot(longAttitude, aes(drink, attitude, colour = imagery))
attitudeInt + stat_summary(fun.y = mean, geom = "point") + 
              stat_summary(fun.y = mean, geom = "line", aes(group = imagery)) + 
              stat_summary(fun.data = mean_cl_boot, geom = "errorbar", width = 0.2) + 
              labs(x = "Type of Drink", y = "Mean Attitude", colour = "Type of Imagery")

Isn't it the case that the part stat_summary(fun.y = mean, geom="line", aes(group = imagery)) should draw exactly these connection lines?

Thanks for your help.

解决方案

This will not solve the problem in ggplot2 but this is workaround.

First summarize your data. As in original code you used mean_cl_boot for calculating confidence intervals, then used also in this example.

library(plyr)
dfAtt<-ddply(longAttitude,~drink+imagery,function(x) mean_cl_boot(x$attitude))

dfAtt
  drink  imagery      y      ymin     ymax
1  Beer Positive  21.05  15.65000 26.90750
2  Beer Negative   4.45  -2.60125 12.00000
3  Beer  Neutral  10.00   5.49750 14.75000
4  Wine Positive  25.35  22.40000 28.25000
5  Wine Negative -12.00 -14.40000 -9.49875
6  Wine  Neutral  11.65   8.95000 14.40125
7 Water Positive  17.40  14.40000 20.45000
8 Water Negative  -9.20 -12.25000 -6.34875
9 Water  Neutral   2.35  -0.75125  4.90000

Then plot your data:

ggplot(dfAtt, aes(x=drink, y=y, colour=imagery,group=imagery)) + 
  geom_errorbar(aes(ymin=ymin, ymax=ymax), width=.2) +
  geom_line() +
  geom_point() +
  labs(x = "Type of Drink", y = "Mean Attitude", colour = "Type of Imagery")

这篇关于ggplot没有绘制组之间的连接线意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆