Categorical scatter plot with mean segments using ggplot2 in R

匿名 (未验证) 提交于 2019-12-03 01:10:02

问题:

I am trying to plot a simple scatter plot for 3 groups, overlaying a segment indicating the mean for each group and labelling the groups. I have managed to get a scatter plot with error bars, but I would only like a segment indicating where the mean is. I also cannot seem to be getting the group labelling right.

To get the summary statistics I am using the function "summarySE" from this page.

Is there any simpler way to do this, and to get a segment instead of a point for the mean?

I really appreciate your help!

library(ggplot2) library(plyr)  df 

回答1:

You can use geom_crossbar() and use val as y, ymin and ymax values. With scale_x_continuous() you can change x axis labels for original data or use @agstudy solution to change original data and labels will appear automatically.

ggplot()+   geom_jitter(aes(tt, val), data = df, colour = I("red"),                position = position_jitter(width = 0.05)) +   geom_crossbar(data=dfc,aes(x=tt,ymin=val, ymax=val,y=val,group=tt), width = 0.5)+   scale_x_continuous(breaks=c(1,2,3),labels=c("Group1", "Group2", "Group3")) 



回答2:

To get the group labelling , You can change continuous tt a factor like this :

dfc$tt 

Of course before calling summarySE and creating dfc.

and using crossbar as mentioned in the other solution below , you get:



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!