Possible Duplicate:
geom_boxplot with precomputed values
I have a table where each row is a different sample and each column is the name, minimum, maximum, mean, 25th percentile, 50th percentile, 75th percentile respectively. Here is a sample.
sample1 1 38 10 8 10 13
sample2 1 39 10 9 11 14
sample3 2 36 11 10 10 13
I would like to know how I can use the data in this format in order to plot boxplots since that is the data that is actually plotted. The format above is a tab separated table. Thanks
This post shows how you can do this with bxp
which is the function that boxplot
uses, but you need to put your data in the right order with the first row being the minimum, and the last row being the maximum.
First, read in the data
dat <- read.table(text="sample1 1 38 10 8 10 13
sample2 1 39 10 9 11 14
sample3 2 36 11 10 10 13", row.names=1, header=FALSE)
Then, put in order and t
ranspose
dat2 <- t(dat[, c(1, 4, 5, 6, 2)]) #Min, 25pct, 50pct, 75pct, Max
and plot
bxp(list(stats=dat2, n=rep(10, ncol(dat2)))) #n is the number of observations in each group
This is a duplicate, however for posterity and since I already started writing...
dat <- data.frame(name=paste0('sample',1:3), min=c(1,1,2), max=c(38,39,36), mean=c(10,10,11), q25=c(8,9,10), q50=c(10,11,10), q75=c(13,14,13))
ggplot(dat, aes(x=name, ymin=min, ymax=max, lower=q25, middle=q50, upper=q75))+geom_boxplot(stat='identity')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With