我正在尝试为 3 个组绘制一个简单的散点图,覆盖一个指示每个组平均值的线段并标记各组。 我已经设法获得带有误差线的散点图,但我只想要一个指示平均值所在位置的线段。我似乎也无法正确地标记组。
为了获取摘要统计信息,我使用 this page 中的函数“summarySE” 。 [编辑:注意 Rmisc 包中也提供了此函数]
有没有更简单的方法来做到这一点,并获得一个段而不是平均值的点?
非常感谢您的帮助!
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)
最佳答案
您可以使用geom_crossbar()
并将val
用作y
、ymin
和ymax
值。使用 scale_x_continuous()
您可以更改原始数据的 x 轴标签,或使用 @agstudy 解决方案更改原始数据,标签将自动显示。
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 - 使用 R 中的 ggplot2 绘制具有平均段的分类散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17807774/