我正在尝试为多个因素的 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))
其中产生:
有没有办法制作
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"))
关于r - 如何在因子 * 组组合没有观察值的箱线图中保持一致的箱线宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47746372/