R Geom_点和Geom_误差条不对齐

R Geom_点和Geom_误差条不对齐,r,ggplot2,R,Ggplot2,我是R新手,对ggplot和以下数据集(从较大的集合中选择作为代表)存在问题,geom_errorbar条与平均点不对齐(使用geom_点),并且在某些情况下,水平条与geom_errorbar中的垂直条不对齐,因此,横杆与垂直线分开或偏离中心,而不是在顶部和底部带有横杆的“I” 我已经查看了所有手册页的ggplot,geom\u point,geom\u errorbar,位置抖动(闪避,抖动闪避)。我在这里也尝试了很多东西,比如改变geom_点和geom_错误条调用中的美学(例如) 以下是一

我是R新手,对ggplot和以下数据集(从较大的集合中选择作为代表)存在问题,
geom_errorbar
条与平均点不对齐(使用geom_点),并且在某些情况下,水平条与
geom_errorbar
中的垂直条不对齐,因此,横杆与垂直线分开或偏离中心,而不是在顶部和底部带有横杆的“I”

我已经查看了所有手册页的
ggplot
geom\u point
geom\u errorbar
位置抖动
(闪避,抖动闪避)。我在这里也尝试了很多东西,比如改变
geom_点
geom_错误条
调用中的美学(例如)

以下是一组基本数据:

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"))

df看起来
position\u jitter
对错误条的不同组件应用的方式不同。这看起来像个虫子

这里有一个变通方法,可以更直接地实现您的目标。添加一列(我在这里称之为
version
)来区分一个测试的多个运行,并按该列对其进行分组,然后使用
position\u dodge
来避免重叠

library(dplyr)
df2 <- df %>% 
  group_by(Test) %>% 
  mutate(version = row_number()) %>% 
  ungroup()

subplot <- ggplot(df2, aes(x = Test, y = mean, group = version)) +
  geom_point(position = position_dodge(width = 0.5))+
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2,
            position = position_dodge(width = 0.5)) 
subplot
首先

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)
Test=c(“A”、“B”、“c”、“D”、“A”、“c”、“D”)
平均值=c(1100.793684,1,1,51.615601,1,2.456456)
sd=c(1,2.045985,1,1,4.790053,1,4.250668)
下限=(平均值+sd)
上限=(平均标准差)
范围=1:长度(测试)

df我将此数据集和问题发布到。似乎我确实错过了一些简单的事情——我需要为
geom\uuz
调用设置种子,以使每个点的抖动保持一致。然而,
geom_errorbar
似乎存在问题,因为设置种子并不能解决纵横杆问题

经过进一步调查(来自Github团队),似乎横杆的抖动与线路无关。有一个解决办法(截至2018年10月23日)来解决这个问题。同时使用
position\u dodge
geom\u linerange

  ggplot(df, aes(x = Test, y = mean)) +
  geom_point(aes(x= Test, y = mean), 
             position = position_jitter(width = 0.2, height = 0.2, seed = 123))+
  geom_linerange(aes(ymin = lower, ymax = upper),
             position = position_jitter(width = 0.2, height = 0.2, seed = 123))

谢谢大家的帮助。

谢谢。不幸的是,这在完整的数据集上似乎不起作用,因为我有更多的变量。我会继续努力的。关于为什么横杆的宽度在点之间变化的任何想法(c.f.“B”和“A/1”上的宽度)。我在下面的问题和答案中也看到了这一点?我添加了一个使用facets的变体,它允许错误条保持相同的宽度,而不管每个测试的版本有多少。感谢您的帮助。我的数据集的完整版本已经为另一个功能刻面。我最初认为问题与我的刻面有关,但现在我想知道是否可以将我的刻度从离散转换为连续,看看这是否有帮助。如果你需要为其他东西保留刻面,那么使用离散刻度可能是你最好的选择。我已经更新了我的回复以显示一个。谢谢你的帮助。虽然我现在正在研究一种解决方案,但这对完整数据集也不起作用。
position\u dodge
position\u jitter
更好地解决这个问题,因为
position\u dodge
允许您使用给定的
测试值控制点之间的间距,而不是通过抖动随机确定。非常正确,
position\u dodge
对这种方法很有效。但是,我的完整数据集要大得多,
position_dodge
不允许我清楚地表示每个点,并且没有解决横杆漂移的问题。我已经在我的完整数据集上尝试了他的方法,现在我将尝试你的方法。
df2 <- df %>% 
group_by(Test) %>% 
  mutate(version = row_number()) %>% 
  mutate(label = paste(Test, version)) %>%
  ungroup()

subplot <- ggplot(df2, aes(x = label, y = mean)) +
  geom_point(position = position_dodge(width = 0.5))+
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2,
                position = position_dodge(width = 0.5))
subplot
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)
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
  ggplot(df, aes(x = Test, y = mean)) +
  geom_point(aes(x= Test, y = mean), 
             position = position_jitter(width = 0.2, height = 0.2, seed = 123))+
  geom_linerange(aes(ymin = lower, ymax = upper),
             position = position_jitter(width = 0.2, height = 0.2, seed = 123))