ggplot多个boxplots和stat_summary位置。

问题描述 投票:0回答:1

我有下面的代码,我想改变boxplots的颜色,使它们有相同的填充颜色(灰色)。我想改变boxplots的颜色,使它们具有相同的填充颜色(灰色).另外,我想让stat_summary文本贴在每个barplot的底部,但vjust似乎只提供相对位置?

我想把统计_摘要文本贴在每个条形图的底部,但vjust似乎只提供相对位置?

boxp <- ggplot(mtcars, aes(as.factor(cyl), wt, fill=as.factor(am)) ) +
  geom_bar(position = "dodge", stat = "summary", fun.y = "median") +
  geom_boxplot(outlier.shape = NA, width=0.2, color = "black", position = position_dodge(0.9)) +
  stat_summary(aes(label=round(..y..,2)), fun.y=median, geom="text", size=8, col = "white", vjust=8, position = position_dodge(0.9)) +
  stat_summary(fun.y=mean, geom="point", shape=18, size=4, col="white", position = position_dodge(0.9)) +
  labs(x = "Conditions", y = "Medians") +
  scale_y_continuous(limits=c(0,7),oob = rescale_none) +
  theme_bw()
boxp

enter image description here

r ggplot2 boxplot
1个回答
1
投票

这里有一个可能的解决方案,但它需要ggplot v3.3.0的版本。stage() 职能。

要指出主要的变化。

  • 我没有把填充作为一个隐式分组,而是明确地设置了分组,这样它就不会和填充挂钩了。
  • 我把填充作为条形geom的美学添加了。
  • 现在,boxplot有了未映射的美感 fill = 'gray'
  • 文本统计摘要使用 stage() 来计算统计量,但却用 0 作为实际安置。
library(ggplot2)
library(scales)

ggplot(mtcars, aes(as.factor(cyl), wt,
                   group = interaction(as.factor(cyl), as.factor(am)))) +
  geom_bar(aes(fill=as.factor(am)), position = "dodge", stat = "summary", fun = "median") +
  geom_boxplot(outlier.shape = NA, width=0.2, 
               color = "black", fill = 'gray',
               position = position_dodge(0.9)) +
  stat_summary(aes(label=round(after_stat(y), 2), y = stage(wt, after_stat = 0)), 
               fun=median, geom="text", size=8, col = "white", vjust=-0.5,
               position = position_dodge(0.9)) +
  stat_summary(fun=mean, geom="point", shape=18, size=4, col="white", position = position_dodge(0.9)) +
  labs(x = "Conditions", y = "Medians") +
  scale_y_continuous(limits=c(0,7),oob = rescale_none) +
  theme_bw()

于2020-05-06创建,由 重读包 (v0.3.0)

© www.soinside.com 2019 - 2024. All rights reserved.