r - 如何在因子 * 组组合没有观察值的箱线图中保持一致的箱线宽度?

标签 r ggplot2 dplyr

我正在尝试为多个因素的 2 个组创建一个箱线图以及观察次数的标签。当一个组在一个因子水平上没有观测值时,带有观测值的组的框占据了两者的空间,看起来很奇怪。

最小的例子:

library(tidyverse)

mtcars %>%
  select(mpg, cyl,am) %>%
  filter(!(cyl == 8 & am == 0)) %>%
  ggplot(aes(factor(cyl),mpg,fill=factor(am))) + 
  stat_boxplot(geom = "errorbar") + ## Draw horizontal lines across ends of whiskers
  geom_boxplot(outlier.shape=1, outlier.size=3, 
               position =  position_dodge(width = 0.75)) +
  geom_text(data = mtcars %>%
              select(mpg, cyl, am) %>%
              filter(!(cyl == 8 & am == 0)) %>%
              group_by(cyl, am) %>%
              summarize(Count = n(),
                      q3 = quantile(mpg, 0.75),
                      iqr = IQR(mpg),
                      lab_pos = max(ifelse(mpg < q3+1.5*iqr, mpg, NA), na.rm = TRUE)),
                      aes(x= factor(cyl), y = lab_pos,label = paste0("n = ",Count, "\n")),
                  position = position_dodge(width = 0.75))

其中产生:

enter image description here

有没有办法制作 am(1) 的盒子在 cyl(8)宽度的一半,所以它与图上的其他框一致?我曾尝试使用假数据,但这会导致 am(0) 的计数标签。在 cyl(8) .

最佳答案

通过安装最新版本的 ggplot2,我得到了一个合理的解决方案。来自 GitHub 并使用 position_dodge2使用 preserve = "single"默认情况下。

# Install devtools
install.packages('devtools')

# Install dependency of scales package
install.packages(c("RColorBrewer", "stringr", "dichromat", 
                   "munsell", "plyr", "colorspace"))

# Load devtools
library(devtools)

# Move to development mode
# This installed scales and ggplot2 in the "~/R-dev" directory, 
# so CRAN version of ggplot2 is not removed.
dev_mode(TRUE)

# Install scales
install_github("hadley/scales")

# Main branch of development
install_github("hadley/ggplot2", "hadley/develop")

# load development version of ggplot2
library(dplyr)
library(ggplot2)

mtcars %>%
  select(mpg, cyl,am) %>%
  filter(!(cyl == 8 & am == 0)) %>%
  ggplot(aes(factor(cyl),mpg,fill=factor(am))) + 
  stat_boxplot(geom = "errorbar",
               position =  position_dodge2(width = 0.75, preserve = "single")) + 
  geom_boxplot(outlier.shape=1, outlier.size=3, 
               position =  position_dodge2(width = 0.75, preserve = "single")) +
  geom_text(data = mtcars %>%
              select(mpg, cyl, am) %>%
              filter(!(cyl == 8 & am == 0)) %>%
              group_by(cyl, am) %>%
              summarize(Count = n(),
                        q3 = quantile(mpg, 0.75),
                        iqr = IQR(mpg),
                        lab_pos = max(mpg)),
            aes(x= factor(cyl), y = lab_pos,label = paste0("n = ",Count, "\n")),
            position = position_dodge2(width = 0.75, preserve = "single"))

enter image description here

关于r - 如何在因子 * 组组合没有观察值的箱线图中保持一致的箱线宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47746372/

相关文章:

r - 在 dplyr mutate 中使用 sum 函数

R cSplit 仅使用字符串中的第一个分隔符

r - 在 ggplot2 不同数据集中混合直方图和密度图

r - 如何说服 ggplot2 geom_text 在时间序列图中标记指定日期?

r - dplyr:如何使用 mutate 按列索引而不是列名称引用列?

r - 如果键列值与 dplyr (R) 重复,则获取平均值

r - 从数据框中提取值并将其填充到 R 中的模板短语中

r - 在r中的网格图中绘制色标

R,数据表 : Sum all columns whose names are stored in a vector

r - 将 ggplot 与 ddply 相结合