[英]Add whiskers to boxplot made from predefined (5 number summary) statistics in R
I am making boxplot from statistical summary data, following the procedure specified here: 我正在按照此处指定的步骤从统计摘要数据制作箱线图:
Multiple boxplots with predefined statistics using lattice-like graphs in r 使用r中的格子状图具有预定义统计信息的多个箱形图
Additionally, I want to add horizontal whiskers to boxplot, following the procedure specified here: 此外,我想按照此处指定的步骤将水平晶须添加到箱线图中:
Add whiskers (horizontal lines) to multiple boxplots 将晶须(水平线)添加到多个盒图中
Data: 数据:
> 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
I am getting error: 我收到错误消息:
stat_boxplot requires the following missing aesthetics: y
stat_boxplot需要以下缺失的美感:y
However, since I am using statistical summary instead of raw data, there is no 'y' to specify. 但是,由于我使用的是统计摘要而不是原始数据,因此没有要指定的“ y”。
If you share your data, please use dput so you can directly copy it to R without needing to reconstruct it. 如果共享数据,请使用dput,这样您就可以直接将其复制到R中,而无需重建它。
Why do you use stat_boxplot? 为什么使用stat_boxplot? If you are just interested in the boxplots, geom is enough and will show the plots as intended:
如果您仅对框线图感兴趣,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")
Then with ggplot: 然后用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
Which yields: 产生:
In case you do not want a scale convert your Type column to a factor first: 如果您不想使用比例,请先将“类型”列转换为因子:
combined$Type <- as.factor(combined$Type)
Which gives: 这使:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.