问题描述
我绘制了一个箱形图+点。我想为这些点添加颜色。 position_jitterdodge
可以很好地工作,如图B所示,没有颜色,点很接近,这是我打算做的。但是,当我尝试为点添加颜色时, jitter.width
参数不再起作用(图A)。这些点相距太远。我为 jitter.width
尝试了不同的数字,无法正常工作。我该如何解决这个问题?
I have plotted a boxplot+points. I want to add colors to the points. The position_jitterdodge
worked fine without color as shown in Figure B, the points are close, which is I intended to do. But when I try to add colors to the points, the jitter.width
parameter doesn't work any more (Figure A). The points are too far apart. I tried different numbers for jitter.width
, not working. How do I solve this problem?
library(tidyverse)
library(ggpubr)
mtcars$cyl <- factor(mtcars$cyl)
p1 <- mtcars %>% ggplot(aes(x = cyl, y = mpg, fill = cyl)) +
geom_boxplot() +
geom_point(position = position_jitterdodge(jitter.width = 0.2),
aes(color = factor(wt)), show.legend = FALSE)
p2 <- mtcars %>% ggplot(aes(x = cyl, y = mpg, fill = cyl)) +
geom_boxplot() +
geom_point(position = position_jitterdodge(jitter.width = 0.2))
ggarrange(p1, p2, labels = c("A", "B"))
推荐答案
在p1中,这些点不仅抖动,它们也被 factor(wt)
躲避。如果只希望抖动,请在 position_jitterdodge
中设置 dodge.width = 0
。
In p1, the points are not only jittered, they are also dodged by factor(wt)
. If you only want jitter, set dodge.width = 0
in position_jitterdodge
.
这篇关于指定颜色时,ggplot geom_point position_jitterdodge不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!