对于该说明性数据:
set.seed(123)
df <- data.frame(
ID = 1:50,
Q = rnorm(50),
A = rnorm(50,1)
)
我想连接ID
上的成对点,这可以通过下面的答案Connect jittered points by group来实现:
pj <- position_jitter(seed = 1, width = .1, height = 0)
df %>%
pivot_longer(-ID) %>%
ggplot(aes(x = factor(name), y = value, fill = factor(name))) +
# boxplot:
geom_boxplot(
width = 0.12,
outlier.color = NA,
alpha = 0.5
) +
# data points:
geom_point(
alpha = 0.5, col = "blue",
position = pj
) +
# connecting lines:
geom_path(aes(group = ID),
alpha = 0.5,
position = pj
)
让我烦恼的是,这些点覆盖了箱线图。我希望它们与箱线图分开。具体来说,它们应该移动到箱线图之间的空间,就像下面的图:
如何才能做到这一点呢?在此提前表示感谢。
1条答案
按热度按时间9lowa7mx1#
一个选项是移动或微移点和线的位置,这需要将
name
转换为数字并考虑箱形图和抖动宽度:编辑要切换
name
类别的位置,请在转换为factor
时以所需的顺序设置级别。当然,我们必须考虑到ifelse
中的情况。