我是 R 新手,对 ggplot 和以下数据集(从较大的集合中选择作为代表)有问题,其中geom_errorbar条形图与平均点(使用 geom_point)不对齐,并且在某些情况下水平条形图与垂直条形不对齐中geom_errorbar,这样横杆就不再是顶部和底部带有横杆的“I”,而是与垂直线分离或偏离中心。
ggplot我已经查看了, geom_point, geom_errorbar, position_jitter(dodge, jitterdodge)的所有手册页。我还从这里尝试了很多事情,例如改变geom_point和geom_errorbar调用中的美观(例如如何使 geom_bar 中的闪避与 geom_errorbar, geom_point 中的闪避一致)
这是一个基本数据集:
df <- structure(list(
Test = c("A", "B", "C", "D", "A", "C", "D"),
mean = c(1, 100.793684, 1, 1, 51.615601, 1, 2.456456),
sd = c(1, 2.045985, 1, 1, 4.790053, 1, 4.250668),
lower = c(2, 102.839669, 2, 2, 56.405654, 2, 6.707124),
upper = c(0, 98.747699, 0, 0, 46.825548, 0, -1.79421)),
row.names = c(NA, -7L), class = c("tbl_df", "tbl", "data.frame"))
Run Code Online (Sandbox Code Playgroud)
现在我正在使用的代码:
subplot <- ggplot(df, aes(x = Test, y = mean)) +
geom_point(aes(x= Test, y = mean),
position = position_jitter(width = 0.2, height = 0.2))+
geom_errorbar(aes(ymin = lower, ymax = upper),
width = 0.1,
position = position_jitter(width = 0.2, height = 0.2))
subplot
Run Code Online (Sandbox Code Playgroud)
这就是我得到的:
我怀疑这是我错过的一些基本的东西。我在线图和其他散点图中使用了相同的代码,效果很好,所以我对我所做的事情感到困惑。我也在不同的计算机上的两个不同的 R 安装上对其进行了测试。
非常感谢任何帮助。
小智 2
第一的,
Test = c("A", "B", "C", "D", "A", "C", "D")
mean = c(1, 100.793684, 1, 1, 51.615601, 1, 2.456456)
sd = c(1, 2.045985, 1, 1, 4.790053, 1, 4.250668)
lower = (mean+sd)
upper = (mean-sd)
range = 1:length(Test)
df <- data.frame(Test,mean,sd,lower,upper,range)
Run Code Online (Sandbox Code Playgroud)
然后
subplot <- ggplot(df, aes(x = Test, y = mean,group=range)) +
geom_point(position = position_dodge(width = 0.2))+
geom_errorbar(aes(ymin = lower, ymax = upper),
width = 0.1, position = position_dodge(width = 0.2))
subplot
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
4246 次 |
| 最近记录: |