我想畫一個三列的條形圖,其中兩個箭頭從一列開始到另外兩列,并且這些箭頭彼此不重疊,如下圖。

我設法制作了圖表,并使用geom_segments 繪制了箭頭。但是,箭頭從列開始時會重疊control。

我以為我可以通過設定來解決這個問題geom_segment(..., position = position_nudge(x = 0.25))。然而,這不僅改變了 的位置,x而且還改變了xend。那么,我應該如何稍微改變geom_segment的位置x,保持 的位置xend不變?

移動電源
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()
)
uj5u.com熱心網友回復:
這是一種方法:
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)

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/376599.html
上一篇:繪制包含多個組的條形圖
