如何使geom_line和geom_point抖动相同的幅度? [英] How to jitter both geom_line and geom_point by the same magnitude?

查看:88
本文介绍了如何使geom_line和geom_point抖动相同的幅度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ggplot2折线图,其中有两条线具有明显的重叠.我试图使用position_jitterdodge()以便它们更可见,但是我无法以相同的方式获得指向两个抖动的线和点.我正在尝试仅使点和线水平抖动(因为我不想建议y轴上的任何变化).这是MWE:

I have a ggplot2 linegraph with two lines featuring significant overlap. I'm trying to use position_jitterdodge() so that they are more visible, but I can't get the lines and points to both jitter in the same way. I'm trying to jitter the points and line horizontally only (as I don't want to suggest any change on the y-axis). Here is an MWE:

## Create data frames
dimension <- factor(c("A", "B", "C", "D"))
df <- data.frame("dimension" = rep(dimension, 2),
                 "value" = c(20, 21, 34, 32,
                             20, 21, 36, 29),
                 "Time" = c(rep("First", 4), rep("Second", 4)))
## Plot it
ggplot(data = df, aes(x = dimension, y = value,
                      shape = Time, linetype = Time, group = Time)) +
    geom_line(position = position_jitterdodge(dodge.width = 0.45)) +
    geom_point(position = position_jitterdodge(dodge.width = 0.45)) +
    xlab("Dimension") + ylab("Value")

哪个丑陋?

我在这里显然有一些根本上的错误:我应该怎么做才能使geom_point抖动遵循geom_line抖动?

I've obviously got something fundamentally wrong here: What should I do to make the geom_point jitter follow the geom_line jitter?

推荐答案

仅针对水平的另一种选择是指定position_dodge并将其传递给每个几何的position参数.

Another option for horizontal only would be to specify position_dodge and pass this to the position argument for each geom.

pd <- position_dodge(0.4)

ggplot(data = df, aes(x = dimension, y = value,
                      shape = Time, linetype = Time, group = Time)) +
  geom_line(position = pd) +
  geom_point(position = pd) +
  xlab("Dimension") + ylab("Value")

这篇关于如何使geom_line和geom_point抖动相同的幅度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆