如何使用 geom_segments 进行抖动
how to use geom_segments for jitter
我在 ggplot 中使用 geom_segments 链接了两点。它适用于 geom_point but when i use geom_jitter it doesn't gives me the desired results 。我想看到两者之间有线的所有点。你能帮我如何使用 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)
由于没有发布示例数据,我使用一些虚拟数据来说明一些事情。让我们设置一下:
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")
是 shorthand 可能会很方便。与大多数位置一样,您可以给出 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))
我在 ggplot 中使用 geom_segments 链接了两点。它适用于 geom_point
我按照建议修改了代码,现在点的位置已经改变了
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)
由于没有发布示例数据,我使用一些虚拟数据来说明一些事情。让我们设置一下:
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")
是 shorthand 可能会很方便。与大多数位置一样,您可以给出 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))