How to jitter both geom_line and geom_point by the same magnitude?

前端 未结 3 1010
刺人心
刺人心 2020-12-10 12:30

I have a ggplot2 linegraph with two lines featuring significant overlap. I\'m trying to use position_jitterdodge() so that they are more visible, b

3条回答
  •  旧巷少年郎
    2020-12-10 13:22

    One solution is to manually jitter the points:

    df$value_j <- jitter(df$value)
    
    ggplot(df, aes(dimension, value_j, shape=Time, linetype=Time, group=Time)) +
      geom_line() +
      geom_point() +
      labs(x="Dimension", y="Value")
    

    The horizontal solution for your discrete X axis isn't as clean (it's clean under the covers when ggplot2 does it since it handles the axis and point transformations for you quite nicely) but it's doable:

    df$dim_j <- jitter(as.numeric(factor(df$dimension)))
    
    ggplot(df, aes(dim_j, value, shape=Time, linetype=Time, group=Time)) +
      geom_line() +
      geom_point() +
      scale_x_continuous(labels=dimension) +
      labs(x="Dimension", y="Value")
    

提交回复
热议问题