使用R中的ggplot2的平均分段的分类散点图 [英] Categorical scatter plot with mean segments using ggplot2 in R
问题描述
我试图为3组绘制一个简单的散点图,叠加一段表示每个组的平均值并标注组。
我已经设法得到一个带误差线的散点图,但我只想要一段指示平均值在哪里。我似乎也无法获得集体标签的权利。 为了得到摘要统计数据,我使用 this page 。
有没有更简单的方法来做到这一点,并得到一个段而不是点的意思?
我非常感谢你的帮助!
library(ggplot2)
library(plyr)
df < - data.frame(tt = rep(1:3,each = 40),
val = round(rnorm (120,m = rep(c(4,5,7),each = 40))))
#加载summarySE函数后:
dfc< - summarySE(df,measurevar =val,groupvars =tt)
ggplot(dfc,aes(tt,val),main =散点图平均值,
xlab =Groups,ylab =Values,names = c(Group1,Group2,Group3))+
geom_jitter(aes(tt,val),data = df, color = I(red),
position = position_jitter(width = 0.05))+
geom_point(size = 3)+
geom_errorbar(aes(ymin = val-sd,ymax = val + sd),width = 0.01,size = 1)
val
作为 y
, ymin
和 ymax
值。使用 scale_x_continuous()
,您可以更改原始数据的x轴标签或使用@agstudy解决方案更改原始数据,并且标签将自动出现。 ggplot()+
geom_jitter(aes(tt,val),data = df,color = I红色),
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))
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 <- data.frame(tt = rep(1:3, each = 40),
val = round(rnorm(120, m = rep(c(4, 5, 7), each = 40))))
# After loading the summarySE function:
dfc <- summarySE(df, measurevar="val", groupvars="tt")
ggplot(dfc, aes(tt, val), main="Scatter plot with mean bars",
xlab="Groups", ylab="Values", names=c("Group1", "Group2", "Group3"))+
geom_jitter(aes(tt, val), data = df, colour = I("red"),
position = position_jitter(width = 0.05)) +
geom_point(size = 3) +
geom_errorbar(aes(ymin=val-sd, ymax=val+sd), width = 0.01, size = 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"))
这篇关于使用R中的ggplot2的平均分段的分类散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!