简体   繁体   中英

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

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

However, since I am using statistical summary instead of raw data, there is no 'y' to specify.

If you share your data, please use dput so you can directly copy it to R without needing to reconstruct it.

Why do you use stat_boxplot? If you are just interested in the boxplots, geom is enough and will show the plots as intended:

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:

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:

Boxplot

In case you do not want a scale convert your Type column to a factor first:

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

Which gives:

Boxplot factors

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM