在for循环中绘制R中boxplot的分位数值 [英] Plotting Quantiles values of boxplot in R inside a for loop

查看:211
本文介绍了在for循环中绘制R中boxplot的分位数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 名称<  - 名称(空调)

类<-sapply(airquality,class)

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

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

现在我想显示所有分位数值,即First分位数,中位数,第三分位数和平均值,如下图所示。我在网上搜索了很多,但没有找到任何适合我需要的东西。以下是我想要绘制的图表:

解决方案

下面是一个例子,只是使用Wind属性。

  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)
箭头(0.6,B $ stats [3] +0.5,0.6,B $ stats [4] -0.1,0.1)
箭头(0.6, B $ stats [3] -0.5,0.6,B $ stats [2] +0.1,0.1)


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:

解决方案

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)

这篇关于在for循环中绘制R中boxplot的分位数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆