在for循环内的R中绘制boxplot的Quantiles值

时间:2016-12-18 17:44:34

标签: r plot ggplot2 boxplot iqr

假设我有一个数据帧空气质量。我做了一个for循环来绘制空气质量数据集的所有箱形图。

name <- names(airquality)

classes<-sapply(airquality,class)

airquality[is.na(airquality)] <- 0

for (name in name[classes == 'numeric']) {
  boxplot(airquality[,name])
}

现在我想显示所有的Quantiles值,即First Quantile,Median,Third Quantile和mean,如下图所示。我在网上搜索了很多,但没有找到任何适合我需要的东西。下面是我想绘制的图表:

enter image description here

2 个答案:

答案 0 :(得分:2)

这是一个例子,只使用&#34; Wind&#34;属性。

B = boxplot(airquality[,"Wind"])
text(1.3, B$stats, B$stats)

IQR = B$stats[4] - B$stats[2]
segments(0.5, c(B$stats[2], B$stats[4]), 0.7, c(B$stats[2], B$stats[4]))
text(0.6, B$stats[3], IQR)
arrows(0.6, B$stats[3]+0.5, 0.6, B$stats[4]-0.1, 0.1)
arrows(0.6, B$stats[3]-0.5, 0.6, B$stats[2]+0.1, 0.1)

Boxplot of wind

答案 1 :(得分:1)

使用您的代码:

name <- names(airquality)
classes<-sapply(airquality,class)
airquality[is.na(airquality)] <- 0
for (name in name[classes == 'numeric']) {
  boxplot(airquality[,name])
  text(x=1.25,y=fivenum(airquality[,name]), labels =fivenum(airquality[,name]))
  text(x=0.75,y=median(airquality[,name]), labels=IQR(airquality[,name]))
  arrows(0.77, fivenum(airquality[,name])[2], 0.77, fivenum(airquality[,name])[4], angle= 90 ,length=0.07,code=3)
}

Plot就在这里,在这个链接中:Boxplot with IQR rule