Plotting Quantiles values of boxplot in R inside a for loop

隐身守侯 提交于 2019-12-12 06:53:08

问题


Suppose I have a data frame airquality. I made a for loop to plot all the boxplot of the air-quality data set.

name <- names(airquality)

classes<-sapply(airquality,class)

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

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

Now I want to display all the Quantiles values i.e. First Quantile, Median, Third Quantile and mean as in the below image. I searched the web a lot but didn't find anything that suits my need. Below is the desired graph which I want to plot:


回答1:


Here is an example, just using the "Wind" attribute.

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)




回答2:


With your code:

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

The Plot is here, in this link: Boxplot with IQR rule



来源:https://stackoverflow.com/questions/41211129/plotting-quantiles-values-of-boxplot-in-r-inside-a-for-loop

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