Plotting Quantiles values of boxplot in R inside a

2019-09-07 11:29发布

问题:

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