我有一些分组数据,我想要位置躲避箱线图和位置躲避点,并且我想添加一条连接组内点的线。
library(tidyverse)
mock_data <- tibble::tribble(
~Subject, ~Marker, ~Cell_type, ~Value,
"1", "A", "B", 70L,
"2", "A", "B", 80L,
"3", "A", "B", 90L,
"1", "A", "T", 5L,
"2", "A", "T", 10L,
"3", "A", "T", 15L,
"1", "B", "B", 1L,
"2", "B", "B", 2L,
"3", "B", "B", 3L,
"1", "B", "T", 99L,
"2", "B", "T", 90L,
"3", "B", "T", 99L
)
Run Code Online (Sandbox Code Playgroud)
我已经尝试了 geom_line 中的各种分组,但似乎无法获得预期的输出
mock_data |>
ggplot(aes(x = Marker, y = Value)) +
geom_boxplot(aes(fill = Cell_type), alpha = 0.2) +
geom_point(aes(col = Subject, group = Cell_type), position = position_dodge(width = 0.75), size = 3) +
geom_line(aes(group = Subject), lty = 2, col = "black")
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?谢谢
我们可以按 Maker 分面并将 Cell_type 放在“x”轴上
mock_data |>
ggplot(aes(x = Cell_type, y = Value)) +
geom_boxplot(aes(fill = Cell_type), alpha = 0.2) +
geom_point(aes(col = Subject, group = Cell_type), position = position_dodge(width = 0.75), size = 3) +
geom_line(aes(group = Subject), lty = 2, col = "black") +
facet_wrap(~Marker, strip.position = "bottom") +
theme(
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
strip.background = element_blank()
)
Run Code Online (Sandbox Code Playgroud)

创建于 2023-04-26,使用reprex v2.0.2