解决了jitter problem,现在我想使这些点更明显。我选择pch=21
将黑色圆圈填充为彩色。但是,盒子变成了相同的配色方案。如何更改框的颜色?我想在图A 中使用与在图B 中相同的框色。
library(tidyverse)
library(ggpubr)
mtcars$cyl=factor(mtcars$cyl)
p1=mtcars %>% ggplot(aes(x=cyl, y=mpg, fill=cyl))+
geom_boxplot(show.legend = F, aes(fill=cyl))+
geom_point(position=position_jitterdodge(jitter.width=2, dodge.width = 0),
pch=21, aes(fill=factor(wt)), show.legend = F)
p2=mtcars %>% ggplot(aes(x=cyl, y=mpg, fill=cyl))+
geom_boxplot(show.legend = F)+
geom_point(position=position_jitterdodge(jitter.width=0, dodge.width = 0.3),
aes(color=factor(wt)), show.legend = F)
ggarrange(p1,p2,labels=c("A","B"))
答案 0 :(得分:2)
您可以使用scale_colour_manual()
手动选择颜色。但是,使用此示例很难,因为您已经将factor(wt)
用作geom_point()
的填充美学,该vs
具有大约30个级别,因此您必须手动指定每个级别的颜色。为了演示,我将填充美学更改为p1=mtcars %>% ggplot(aes(x=cyl, y=mpg))+
geom_boxplot(show.legend = F, aes(fill=cyl))+
geom_point(position=position_jitterdodge(jitter.width=2, dodge.width = 0),
pch=21, aes(fill=factor(vs)), show.legend = F) +
scale_fill_manual(values = c("4" = "red",
"6" = "green",
"8" = "yellow",
"0" = "lightblue",
"1" = "black"))
p1
:
$users = [
[name => 'Alice', age => 22],
[name => 'Bob', age => 23],
[name => 'Charlie', age => 19]
];
答案 1 :(得分:1)
解决颜色与填充纠缠的一种快速方法是通过在每个有色点下方添加一个稍大的黑点来改变轮廓点的外观。以下是对p2
的修改:
mtcars %>% ggplot(aes(x=cyl, y=mpg, fill=cyl))+
geom_boxplot(show.legend = F) +
# add new geom_point layer BELOW the coloured version, using the same parameters,
# jittered to the same positions (set the same seed), but with larger size
geom_point(position=position_jitterdodge(jitter.width=0, dodge.width = 0.3, seed = 1234),
aes(group = factor(wt)), show.legend = F, size = 2) +
geom_point(position=position_jitterdodge(jitter.width=0, dodge.width = 0.3, seed = 1234),
aes(color=factor(wt)), show.legend = F)
注意:如果您有许多相互靠近的重叠点,则此技巧将无法正常工作,因为黑色的“轮廓”将完全位于重叠部分的下方。 (尽管在这种情况下,比较简洁的可视化选择可能更合适。)
答案 2 :(得分:0)