【问题标题】:How do I colour jitter points to be different colours in a geom_boxjitter plot?如何在 geom_boxjitter 图中将抖动点着色为不同的颜色?
【发布时间】:2020-03-19 02:17:37
【问题描述】:

我正在尝试将抖动图中点的颜色与框的颜色相匹配。我可以为它们着色一种或另一种颜色,但我似乎无法将它们设置为与框的颜色相匹配。

另外,我已经弄清楚如何使用 geom_jitter 更改抖动点的颜色,但似乎无法弄清楚如何将它们移动到箱线图的一侧,就像 geom_boxjitter 那样。我想也许我可以在我的 geom_jitter() 中添加“position = ...”,但这似乎不起作用...

带有黑点的抖动箱线图

具有正确色点但与框重叠的抖动箱线图

#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'))

【问题讨论】:

  • 对不起,我一开始没看懂你的问题,我编辑了我的代码,现在应该可以工作了

标签: r ggplot2 plot boxplot jitter


【解决方案1】:

根据这篇帖子How to plot a hybrid boxplot: half boxplot with jitter points on the other half?,您必须使用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'))

【讨论】:

猜你喜欢
  • 2016-05-11
  • 2020-07-02
  • 2022-01-24
  • 2018-10-12
  • 1970-01-01
  • 2013-02-26
  • 1970-01-01
  • 1970-01-01
  • 2022-10-05
相关资源
最近更新 更多