将胡须添加到由 R 中预定义的(5 个数字摘要)统计数据制作的箱线图中

Add whiskers to boxplot made from predefined (5 number summary) statistics in R

我正在根据此处指定的程序从统计汇总数据制作箱线图:

Multiple boxplots with predefined statistics using lattice-like graphs in r

此外,我想按照此处指定的步骤将水平胡须添加到箱线图中:

Add whiskers (horizontal lines) to multiple boxplots

数据:

> combined

#    X   Type  Max  Mid  Min  Q25  Q75
# 1  v1   01 0.76 0.41 0.03 0.13 0.67
# 2  v1   02 0.43 0.27 0.10 0.20 0.33
# 3  v2   01 0.28 0.14 0.03 0.08 0.20
# 4  v2   02 0.77 0.13 0.02 0.06 0.44

require(ggplot2)
require(scales)
p <- ggplot(combined, aes(x=X, ymin=`Min`, lower=`Q25`, middle=`Mid`, upper=`Q75`, ymax=`Max`))
p <- p + stat_boxplot(geom ='errorbar') + geom_boxplot(aes(fill=Type), stat="identity")
p

我遇到错误:

stat_boxplot requires the following missing aesthetics: y

但是,由于我使用的是统计摘要而不是原始数据,所以没有'y'指定。

如果您共享您的数据,请使用 dput 以便您可以直接将其复制到 R 而无需重建它。

为什么使用stat_boxplot?如果您只对箱线图感兴趣,geom 就足够了,并且会按预期显示图表:

dput(combined)

structure(list(X = structure(c(1L, 2L, 1L, 2L), .Label = c("v1", 
"v2"), class = "factor"), Type = c(1, 2, 1, 2), Max = c(0.9, 
0.7, 0.8, 0.7), Mid = c(0.5, 0.3, 0.2, 0.5), Min = c(0.1, 0.01, 
0.02, 0.1), Q25 = c(0.3, 0.1, 0.1, 0.2), Q75 = c(0.6, 0.5, 0.5, 
0.6)), .Names = c("X", "Type", "Max", "Mid", "Min", "Q25", "Q75"
), row.names = c(NA, -4L), class = "data.frame")

然后用ggplot:

p <- ggplot(combined, aes(x=X, ymin=`Min`, lower=`Q25`, middle=`Mid`, upper=`Q75`, ymax=`Max`))
p <- p + geom_boxplot(aes(fill=Type), stat="identity")
p

产生:

Boxplot

如果您不想使用比例,请先将您的类型列转换为系数:

combined$Type <- as.factor(combined$Type)

给出:

Boxplot factors