如何在 geom_boxjitter 图中将抖动点着色为不同的颜色?
How do I colour jitter points to be different colours in a geom_boxjitter plot?
我正在尝试将抖动图中的点颜色与框的颜色相匹配。我可以给它们涂上一种颜色或另一种颜色,但我似乎无法将它们设置为与盒子的颜色相匹配。
或者,我已经想出了如何使用 geom_jitter 更改抖动点的颜色,但似乎无法弄清楚如何像 [=24] 那样将它们移动到箱线图的一侧=] 会有他们。我想也许我可以将 "position = ..." 添加到我的 geom_jitter() 但这似乎不起作用...
带黑色点的抖动箱线图
具有正确颜色点但与框重叠的抖动箱线图
#hybrid jitter-box with jitter points all same colour
ggplot(all.bio2)+
geom_boxjitter(aes(x=season, y=S.chao1, fill=season),
jitter.colour = ,
outlier.colour = NULL, outlier.shape = 1,
errorbar.draw = T,
errorbar.length = 0.2)+
theme(panel.background = element_rect(fill = 'white', colour = 'black'))
#hybrid jitter-box with different coloured points but overlapping with boxes
ggplot(all.bio2)+
geom_boxjitter(aes(x=season, y=S.chao1, fill=season),
jitter.colour = NA,
outlier.colour = NA, outlier.shape = 1,
errorbar.draw = T,
errorbar.length = 0.2)+
geom_jitter(aes(x=season, y=S.chao1, colour=season), width = 0.15)+
theme(panel.background = element_rect(fill = 'white', colour = 'black'))
基于此post ,您必须使用jitter.color = NA
和jitter.shape = 21
才能使箱线图和抖动点之间的颜色相同
因此,对于您的代码,您应该尝试:
library(ggplot2)
library(ggpol)
ggplot(all.bio2, aes(x = as.factor(season), y = S.chao1, fill= as.factor(season))) +
geom_boxjitter(jitter.shape = 21, jitter.color = NA,
outlier.colour = NULL, outlier.shape = 1,
errorbar.draw = T,
errorbar.length = 0.2)+
theme(panel.background = element_rect(fill = 'white', colour = 'black'))
它对我有用(使用 mtcars
数据集)
示例(使用mtcars
数据集)
library(ggpol)
library(ggplot2)
df = mtcars[c(1:20),]
ggplot(df, aes(x = as.factor(cyl), y = mpg, fill= as.factor(cyl))) +
geom_boxjitter(jitter.shape = 21, jitter.color = NA,
outlier.colour = NULL, outlier.shape = 1,
errorbar.draw = T,
errorbar.length = 0.2)+
theme(panel.background = element_rect(fill = 'white', colour = 'black'))
我正在尝试将抖动图中的点颜色与框的颜色相匹配。我可以给它们涂上一种颜色或另一种颜色,但我似乎无法将它们设置为与盒子的颜色相匹配。
或者,我已经想出了如何使用 geom_jitter 更改抖动点的颜色,但似乎无法弄清楚如何像 [=24] 那样将它们移动到箱线图的一侧=] 会有他们。我想也许我可以将 "position = ..." 添加到我的 geom_jitter() 但这似乎不起作用...
带黑色点的抖动箱线图
具有正确颜色点但与框重叠的抖动箱线图
#hybrid jitter-box with jitter points all same colour
ggplot(all.bio2)+
geom_boxjitter(aes(x=season, y=S.chao1, fill=season),
jitter.colour = ,
outlier.colour = NULL, outlier.shape = 1,
errorbar.draw = T,
errorbar.length = 0.2)+
theme(panel.background = element_rect(fill = 'white', colour = 'black'))
#hybrid jitter-box with different coloured points but overlapping with boxes
ggplot(all.bio2)+
geom_boxjitter(aes(x=season, y=S.chao1, fill=season),
jitter.colour = NA,
outlier.colour = NA, outlier.shape = 1,
errorbar.draw = T,
errorbar.length = 0.2)+
geom_jitter(aes(x=season, y=S.chao1, colour=season), width = 0.15)+
theme(panel.background = element_rect(fill = 'white', colour = 'black'))
基于此post jitter.color = NA
和jitter.shape = 21
才能使箱线图和抖动点之间的颜色相同
因此,对于您的代码,您应该尝试:
library(ggplot2)
library(ggpol)
ggplot(all.bio2, aes(x = as.factor(season), y = S.chao1, fill= as.factor(season))) +
geom_boxjitter(jitter.shape = 21, jitter.color = NA,
outlier.colour = NULL, outlier.shape = 1,
errorbar.draw = T,
errorbar.length = 0.2)+
theme(panel.background = element_rect(fill = 'white', colour = 'black'))
它对我有用(使用 mtcars
数据集)
示例(使用mtcars
数据集)
library(ggpol)
library(ggplot2)
df = mtcars[c(1:20),]
ggplot(df, aes(x = as.factor(cyl), y = mpg, fill= as.factor(cyl))) +
geom_boxjitter(jitter.shape = 21, jitter.color = NA,
outlier.colour = NULL, outlier.shape = 1,
errorbar.draw = T,
errorbar.length = 0.2)+
theme(panel.background = element_rect(fill = 'white', colour = 'black'))