[英]Adding 1st/3rd quartile and 90th percentile to a line chart in R
The title says it all.标题说明了一切。 What I would like to do is plotting the median, but also displaying the two quartiles as well as the 90th percentile at each timepoint.
我想要做的是绘制中位数,但还要显示每个时间点的两个四分位数以及第 90 个百分位数。
First, here's a small sample dataset with observations y
for 3 timepoints time
(100 observations for each timepoint).首先,这是一个小样本数据集,其中
y
为 3 个时间点time
(每个时间点 100 个观察值)。
dd <- data.frame(y=c(rnorm(100, 10, 2), rnorm(100, 15, 2), rnorm(100, 20,2)), time=rep(c(1,2,3), each=100))
dd <- data.frame(y=c(rnorm(100, 10, 2), rnorm(100, 15, 2), rnorm(100, 20,2)), time=rep(c(1,2,3) ),每个=100))
Next, summarizing you data according to what you want (here only median and first quantile, but the principle is the same to obtain other quantiles)接下来,根据你想要的来总结你的数据(这里只有中位数和第一个分位数,但获取其他分位数的原理是相同的)
dd1 <- ddply(dd, "time", summarise, med=median(y), firstquart=quantile(y, probs=.01))
dd1 <- ddply(dd, "time", summarise, med=median(y), firstquart=quantile(y, probs=.01))
Finally, plotting your lines with ggplot
- first quantile is in red.最后,用
ggplot
绘制线条 - 第一个分位数为红色。
ggplot(dd1, aes(x=time, y=med)) + geom_line()+geom_line(aes(x=time, y=firstquart), colour="red")
ggplot(dd1, aes(x=time, y=med)) + geom_line()+geom_line(aes(x=time, y=firstquart), colour="red")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.