使用geom_point与position_jitterdodge作品只有当你设置填充美感.我不明白为什么会这样!
这个命令
library(ggplot2)
ggplot(diamonds[ sample(nrow(diamonds), 1000), ],
aes(x = cut, y = carat, color = clarity)) +
geom_point(shape = 21, position = position_jitterdodge())
Run Code Online (Sandbox Code Playgroud)
产生错误:
Error: position_jitterdodge requires the following missing aesthetics: fill
Run Code Online (Sandbox Code Playgroud)
但这有效:
ggplot(diamonds[ sample(nrow(diamonds), 1000), ],
aes(x = cut, y = carat, fill = clarity)) +
geom_point(shape = 21, position = position_jitterdodge())
Run Code Online (Sandbox Code Playgroud)

简单地提供NA值来填充不是一个可行的解决方法:
ggplot(diamonds[ sample(nrow(diamonds), 1000), ],
aes(x = cut, y = carat, color = clarity, fill=NA)) +
geom_point(shape = 21, position = position_jitterdodge())
> Error in seq.default(h[1], h[2], length = n) :
'to' cannot be NA, NaN or infinite
Run Code Online (Sandbox Code Playgroud)
虽然它可以指定一个任意常量(原谅可怕的结果):
ggplot(diamonds[ sample(nrow(diamonds), 1000), ],
aes(x = cut, y = carat, color = clarity, fill='constant')) +
geom_point(shape = 21, position = position_jitterdodge())
Run Code Online (Sandbox Code Playgroud)

关于如何在不指定填充的情况下使用抖动/闪避的任何想法?(即仅有色点)
编辑:进一步对@joran的评论,我想在积分图上叠加积分.由于不一定会用来fill区分箱形图,如果geom_point(position=position_jitterdodge())没有容纳的地块那将是很好的fill.也许目前不可能,但......
#This doesn't work:
ggplot(diamonds[ sample(nrow(diamonds), 1000), ],
aes(x = cut, y = carat, color = clarity)) +
geom_boxplot() +
geom_point(shape = 21, position = position_jitterdodge())
#This does, although obviously no one wants a plot like this
ggplot(diamonds[ sample(nrow(diamonds), 1000), ],
aes(x = cut, y = carat, color = clarity, fill='constant')) +
geom_boxplot() +
geom_point(shape = 21, position = position_jitterdodge())
#This is way it's intended to work, but marries you to 'fill'
ggplot(diamonds[ sample(nrow(diamonds), 1000), ],
aes(x = cut, y = carat, fill = clarity)) +
geom_boxplot() +
geom_point(shape = 21, position = position_jitterdodge())
Run Code Online (Sandbox Code Playgroud)
好的,这是我的解决方法.指定fill你真正想要的美学(color在我的情况下),然后填空scale_fill_manual
我制作了一个与我的实际用例更相似的不同假数据集,因为上面指定的钻石数据实际上不是盒子+点的好选择
my_dat <- data.frame(class=factor(rep(1:2, 600)),
y=rnorm(1200)),
x=rep(letters[1:3], each=400))
ggplot(my_dat, aes(x=x, y=y, fill=class, color=class)) +
geom_boxplot(outlier.shape = NA) +
geom_point(shape = 21, alpha = 0.5, position=position_jitterdodge()) +
scale_fill_manual(values = rep(NA, 2))
Run Code Online (Sandbox Code Playgroud)
