我正在按照此处指定的程序从统计摘要数据制作箱线图:
具有预定义统计量的多个箱线图,在 r 中使用类似格的图形
此外,我想按照此处指定的过程将水平晶须添加到箱线图:
将晶须(水平线)添加到多个箱线图
数据:
> 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需要以下缺失的美学: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
这会产生:
箱线图
如果您不想要刻度,请先将"类型"列转换为因子:
combined$Type <- as.factor(combined$Type)
这给了:
箱线图因子