首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ggplot geom_errorbar?

ggplot geom_errorbar?
EN

Stack Overflow用户
提问于 2016-02-26 22:31:54
回答 2查看 10.2K关注 0票数 13

我的数据看起来像这样:

代码语言:javascript
复制
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着色的带有错误条lb, ub的点x,y。因为x是离散的,所以我想要jitter,这样点和条就不会重叠。现在,我可以对点执行jitter操作,但不能对直线执行操作。另外,我希望点的顺序是A,B,C

代码语言:javascript
复制
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()

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-26 22:45:49

可以使用position_dodge来获得所需的顺序和在点位置绘制的误差线

代码语言:javascript
复制
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()

票数 18
EN

Stack Overflow用户

发布于 2019-09-12 23:26:34

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

代码语言:javascript
复制
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()

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35654364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档