繁体   English   中英

ggplot jitter geom_errorbar?

[英]ggplot jitter geom_errorbar?

我的数据看起来像这样:

df1 <-
  structure(
    list(
      y = c(-0.19, 0.3,-0.05, 0.15,-0.05, 0.15),
      lb = c(-0.61,
             0.1,-0.19,-0.06,-0.19,-0.06),
      ub = c(0.22, 0.51, 0.09, 0.36,
             0.09, 0.36),
      x = structure(
        c(1L, 2L, 1L, 2L, 1L, 2L),
        .Label = c("X1",
                   "X2"),
        class = "factor"
      ),
      Group = c("A", "A", "B", "B", "C",
                "C")
    ),
    .Names = c("y", "lb", "ub", "x", "Group"),
    row.names = c(NA,-6L),
    class = "data.frame"
  )

我想使用ggplot2来绘制由x,y group着色的点x,y用误差条lb, ub 因为x是离散的,我想要jitter所以点和条不重叠。 现在,我可以jitter点而不是线。 另外,我希望点的顺序是A,B,C

ggplot(data = df1, aes(x, y, color = Group)) + geom_point(size = 4, position = "jitter") +
  geom_errorbar(
    aes(ymin = lb, ymax = ub),
    width = 0.1,
    linetype = "dotted"
  ) +
  geom_hline(aes(yintercept = 0), linetype = "dashed") + theme_bw()

在此输入图像描述

您可以使用position_dodge来实现所需的顺序以及在点的位置绘制的误差线

ggplot(data = df1, aes(x, y, color = Group)) +
    geom_point(size = 4, position=position_dodge(width=0.5)) +
    geom_errorbar(
        aes(ymin = lb, ymax = ub),
        width = 0.1,
        linetype = "dotted",
        position=position_dodge(width=0.5)) +
    geom_hline(aes(yintercept = 0), linetype = "dashed") + 
    theme_bw()

在此输入图像描述

如果你想要抖动,我喜欢这样:

ggplot(data = df1, aes(x, y, color = Group)) +
    geom_pointrange(aes(ymin = lb, ymax = ub), 
                    position=position_jitter(width=0.5), 
                    linetype='dotted') +
    theme_bw()

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM