首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >只稍微改变geom段的x位置,但保持xend的位置不变

只稍微改变geom段的x位置,但保持xend的位置不变
EN

Stack Overflow用户
提问于 2021-12-03 05:54:08
回答 1查看 128关注 0票数 0

我想用三列绘制一个条形图,其中两个箭头从一列开始到另两列,这些箭头不重叠,如下所示。

我成功地生成了图表,并使用geom_segments绘制箭头。但是,当箭头从列control开始时会重叠。

我以为我可以通过设置geom_segment(..., position = position_nudge(x = 0.25))来处理这个问题。然而,这不仅改变了x的地位,也改变了xend的地位。那么,我应该如何稍微改变geom_segmentx的位置,保持xend的位置不变?

米维

代码语言:javascript
复制
library(tidyverse)

demoData <- tribble(
  ~priming, ~rt,
  "control", 374,
  "phonological", 267,
  "orthographic", 304
) |>
mutate(
  diff.from.baseline = rt - first(rt)
)
  
baseline <- demoData |>
  filter(priming == "control") |>
  dplyr::select(rt) |>
  pull()

demoData |>
  ggplot(
    aes(
      x = priming,
      y = rt
    )
  ) +
  geom_col() +
  #### from control to orthographic
  geom_segment(
    aes(
      x = "control",
      y = baseline,
      xend = "control",
      yend = baseline + 50
    )#,
    #position = position_dodge2(
    #  width = 0.5,
    #  preserve = "total"
    #  )
    #position = position_nudge(
    #  x = 0.25,
    #  xend = 0
    #)
  ) +
  geom_segment(
    aes(
      x = "control"  + 0.25,
      y = baseline + 50,
      xend = "orthographic",
      yend = baseline + 50
    )#,
    #position = position_dodge2(
    #  width = 1,
    #  preserve = "total"
    #  )
    #position = position_nudge(
    #  x = 0.25,
    #  xend = 0
    #)
  ) +
  geom_segment(
    aes(
      x = "orthographic",
      y = baseline + 50,
      xend = "orthographic",
      yend = demoData |>
        filter(priming == "orthographic") |>
        dplyr::select(rt) |>
        pull()
    ),
    arrow = arrow()
  ) +
  #### from control to phonological
  geom_segment(
    aes(
      x = "control",
      y = baseline,
      xend = "control",
      yend = baseline + 100
    )
  ) +
  geom_segment(
    aes(
      x = "control",
      y = baseline + 100,
      xend = "phonological",
      yend = baseline + 100
    )
  ) +
  geom_segment(
    aes(
      x = "phonological",
      y = baseline + 100,
      xend = "phonological",
      yend = demoData |>
        filter(priming == "phonological") |>
        dplyr::select(rt) |>
        pull()
    ),
    arrow = arrow()
  )
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-03 06:36:27

下面是一个方法:

代码语言:javascript
复制
segments <- data.frame(seg = rep(c(1:2), each = 4),
                       x = c(0.8, 0.8, 3, 3, 1.2, 1.2, 2, 2),
                       y = c(as.numeric(demoData[1,2]), 450, 
                                        450, as.numeric(demoData[3,2]),
                            as.numeric(demoData[1,2]), 425, 
                                       425, as.numeric(demoData[2,2])))

ggplot() +
  geom_path(data = segments, aes(x, y, group = seg), arrow = arrow()) +
  geom_col(data = demoData, 
           aes(x = as.numeric(factor(demoData$priming, 
                                     levels = demoData$priming)), rt)) +
  scale_x_continuous(breaks = 1:3, labels = demoData$priming)

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

https://stackoverflow.com/questions/70210095

复制
相关文章

相似问题

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