我用geom_segments将两点联系在一起。它适用于geom_point

但是当我使用geom_jitter时,它并没有给我想要的结果

。我想看到两条线之间的所有点。你能帮我怎样用geom_jitter连接点吗?为什么点看起来不平行呢?
我按照建议修改了代码,现在点位置已经改变了。

ggplot() +
geom_point(data = mydata, aes(x = lower, y = lower_p)) +
geom_point(data = mydata, aes(x = higher, y = higher_p)) +
geom_segment(aes(x = lower, y = ifelse(lower_p!= higher_p, NA, lower_p), xend = higher, yend =
higher_p), data = mydata)发布于 2019-11-10 22:27:45
由于没有发布示例数据,所以我使用一些虚拟数据来说明一些事情。让我们把它设置成:
df <- data.frame(x = c(1,1,1,2,2),
xend = c(2,2,2,3,3),
y = c(1,1,1,2,2),
yend = c(1,1,1,2,2))如果我们绘制的图类似于您发布的内容,我们会得到以下图,其中点被重复绘制了2-3次:
ggplot(df) +
geom_point(aes(x, y), colour = "red") +
geom_point(aes(xend, yend), colour = "dodgerblue") +
geom_segment(aes(x = x, y = y, xend = xend, yend = yend))

现在,很容易知道geom_jitter()是geom_point(position = "jitter")的缩写。与大多数位置一样,您可以给出position_jitter()参数,说明您希望这种抖动发生的方式。例如,我们可能只想在y方向上抖动:
ggplot(df) +
geom_point(aes(x, y), colour = "red",
position = position_jitter(height = 0.1, width = 0)) +
geom_point(aes(xend, yend), colour = "dodgerblue",
position = position_jitter(height = 0.1, width = 0)) +
geom_segment(aes(x = x, y = y, xend = xend, yend = yend),
position = position_jitter(height = 0.1, width = 0))

正如你所看到的,这看起来很可怕,因为每个点都是独立于其他点的抖动。通过设置抖动种子,我们可以更接近我们想要的东西:
ggplot(df) +
geom_point(aes(x, y), colour = "red",
position = position_jitter(height = 0.1, width = 0, seed = 1)) +
geom_point(aes(xend, yend), colour = "dodgerblue",
position = position_jitter(height = 0.1, width = 0, seed = 1)) +
geom_segment(aes(x = x, y = y, xend = xend, yend = yend),
position = position_jitter(height = 0.1, width = 0, seed = 1))

现在,这将按预期的方式处理左点(因为种子应该对每个点进行相同的随机处理),但会混淆正确的点。之所以会发生这种情况,是因为它们与左点同时被抖动为后续数,而不是与左点平行。
唯一合理的解决办法似乎是预先计算抖动并使用这样的方法,即对每个点都是相同的:
set.seed(0)
df$jit <- runif(nrow(df), -0.05, 0.05)
ggplot(df) +
geom_point(aes(x, y + jit), colour = "red") +
geom_point(aes(xend, yend + jit), colour = "dodgerblue") +
geom_segment(aes(x = x, y = y + jit, xend = xend, yend = yend + jit))

https://stackoverflow.com/questions/58793274
复制相似问题