我遇到了与这篇文章中的类似的问题,该解决方案几乎适用于我的数据,但是当我添加抖动时,我在将点连接到线时遇到问题。我向 geom_point (position = "jitter") 组件添加了抖动,因为我的各个点在条中重叠,但现在我的各个线没有连接到这些点。有人对如何解决这个问题有什么建议吗?我已经尝试在数据帧值中创建抖动,但它也不起作用。
感谢任何帮助,谢谢!
我尝试了下面的代码,希望将各个点连接到线路,但它不起作用。
我也尝试过在x中添加交互,但也没有成功。
library(ggplot2)
df <- data.frame(
Group = c("A", "A", "A", "A", "B", "B"),
Subgroup = c("A.1", "A.2", "A.1", "A.2", "B.1", "B.2"),
Value = c(10, 7, 8, 9, 11, 12),
Pair = c(1, 1, 2, 2, 3, 3)
)
ggplot(df, aes(x = Subgroup, y = Value, fill = Subgroup)) +
geom_bar(stat = "summary", fun = "mean", width = 1) +
geom_point(position="jitter") +
geom_line(aes(group = Pair), color = "red", ) +
facet_wrap(vars(Group), scales = "free_x", strip.position = "bottom") +
labs(x = "Group") +
theme(
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
strip.background = element_blank(),
panel.spacing = unit(0, units = "line")
)
您也必须在
position_jitter
中使用 geom_line
并通过 seed=
参数设置相同的随机种子:
library(ggplot2)
ggplot(df, aes(x = Subgroup, y = Value, fill = Subgroup)) +
geom_bar(stat = "summary", fun = "mean", width = 1) +
geom_point(position = position_jitter(seed = 1)) +
geom_line(aes(group = Pair),
color = "red",
position = position_jitter(seed = 1)
) +
facet_wrap(vars(Group),
scales = "free_x",
strip.position = "bottom"
) +
labs(x = "Group") +
theme(
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
strip.background = element_blank(),
panel.spacing = unit(0, units = "line")
)