ggplot jitter geom_errorbar?

esyap4oy  于 2023-11-14  发布在  其他
关注(0)|答案(2)|浏览(121)

我的数据看起来像这样:

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绘制由group着色的点x,y,误差条为lb, ub。因为x是离散的,所以我想jitter,这样点和条就不会重叠。现在,我可以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()


的数据

jrcvhitl

jrcvhitl1#

您可以使用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()

字符串


的数据

b09cbbtk

b09cbbtk2#

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

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()

字符串


的数据

相关问题