Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw bloxplots in R given 25,50,75 percentiles and min and max values [duplicate]

Tags:

r

boxplot

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

like image 234
Julio Diaz Avatar asked Oct 18 '25 20:10

Julio Diaz


2 Answers

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 transpose

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
like image 133
GSee Avatar answered Oct 21 '25 10:10

GSee


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')
like image 23
Justin Avatar answered Oct 21 '25 11:10

Justin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!