这让我感到困惑,我确信我错过了一些简单的事情。任何帮助,将不胜感激。
我希望红色和蓝色点分开,每个设置在第二个图像中的相应箱图上,但是在第一个图像中使用数字x轴。
df <- data.frame(x = rep(c(1, 2, 10), each = 20),
g = rep(c("A", "B"), times = 30),
y = c(rnorm(60, 0, 1)))
# OK - boxplot by x and g
ggplot(df, aes(y = y, x = x, fill = g, color = g, group = interaction(x, g))) +
geom_boxplot()
# Not OK. The dots are only grouped by x, not g
ggplot(df, aes(y = y, x = x, fill = g, color = g, group = interaction(x, g))) +
geom_point()
# I want the points to correctly overlay the boxplots
ggplot(df, aes(y = y, x = x, fill = g, color = g, group = interaction(x, g))) +
geom_boxplot(alpha = 0.1) +
geom_point()
(我已经通过x上的刻面修复了它,但我希望将轴作为数字来反映正确的缩放比例)
ggplot(df, aes(y = y, x = g, fill = g, color = g, group = interaction(x, g))) +
geom_boxplot(alpha = 0.1) +
geom_point() +
facet_wrap(~x)
答案 0 :(得分:4)
您可以在position=position_dodge(...)
中使用geom_point
。
ggplot(df, aes(y = y, x = x, fill = g, color = g, group = interaction(x, g))) +
geom_boxplot(alpha = 0.1, width=0.75) +
geom_point(position = position_dodge(width=0.75))
答案 1 :(得分:3)
如果您还想要抖动,可以使用position_jitterdodge
。
ggplot(df, aes(y = y, x = x, fill = g, color = g, group = interaction(x, g))) +
geom_boxplot(alpha = 0.1, width=0.75) +
geom_point(position = position_jitterdodge(jitter.width=0.85))
答案 2 :(得分:0)