仅稍微改变 geom_segment 的 x 位置,但保持 xend 的位置不变
Slightly change geom_segment's position of x only, but keep position of xend constant
我想画一个三列的条形图,其中两个箭头从一列开始到另外两列并且这些箭头彼此不重叠,如下所示。
我设法制作了图表,并使用 geom_segment
s 绘制了箭头。但是,箭头从 control
.
列开始时会重叠
我以为我可以通过设置geom_segment(..., position = position_nudge(x = 0.25))
来解决这个问题。然而,这不仅改变了 x
的位置,而且改变了 xend
的位置。那么,如何只稍微改变geom_segment
的x
的位置,保持xend
的位置不变呢?
MWE
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()
)
这是一个方法:
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)
我想画一个三列的条形图,其中两个箭头从一列开始到另外两列并且这些箭头彼此不重叠,如下所示。
我设法制作了图表,并使用 geom_segment
s 绘制了箭头。但是,箭头从 control
.
我以为我可以通过设置geom_segment(..., position = position_nudge(x = 0.25))
来解决这个问题。然而,这不仅改变了 x
的位置,而且改变了 xend
的位置。那么,如何只稍微改变geom_segment
的x
的位置,保持xend
的位置不变呢?
MWE
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()
)
这是一个方法:
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)