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

社会主义新天地 提交于 2019-12-17 20:26:55

问题


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


回答1:


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



回答2:


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')


来源:https://stackoverflow.com/questions/11129432/draw-bloxplots-in-r-given-25-50-75-percentiles-and-min-and-max-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!