Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

interquartile ranges in ggplot2

Tags:

r

ggplot2

set.seed(42)
DF <- data.frame(bias=rnorm(2700),cnd=1:27)
DF$cnd <- factor(DF$cnd)

Trying to understand the use of median_hilow in ggplot. I was hoping to find a way to plot upper and lower interquartile ranges. But I can't find the a full explanation for 'fun.data=median_hilow' anywhere. Even though I assume it is doing the correct thing. Is there any full documentation for this function to check how it is plotting IQRs?

library(ggplot2)
ggplot(DF,aes(x=cnd,y=bias,colour=cnd)) + 
  stat_summary(fun.data=median_hilow)
like image 655
user1320502 Avatar asked Dec 15 '22 16:12

user1320502


1 Answers

median_hilow is just a wrapper around smedian_hilow which comes from the Hmisc package.

From the documentation of smean / smedian group of functions from Hmisc.

As per @BondedDust 's comment below you need to have the package Hmisc installed previously.

(type ?smedian_hilow and ?median_hilow):

A number of statistical summary functions is provided for use with summary.formula and summarize (as well as tapply and by themselves). smean.cl.normal computes 3 summary variables: the sample mean and lower and upper Gaussian confidence limits based on the t-distribution. smean.sd computes the mean and standard deviation. smean.sdl computes the mean plus or minus a constant times the standard deviation. smean.cl.boot is a very fast implementation of the basic nonparametric bootstrap for obtaining confidence limits for the population mean without assuming normality. These functions all delete NAs automatically. smedian.hilow computes the sample median and a selected pair of outer quantiles having equal tail areas.

The smedian.hilow calculates the median and lower and upper quartiles according to a confidence interval. As an example:

x <- rnorm(100)

> smedian.hilow(x, conf.int=.5)  # 25th and 75th percentiles
     Median       Lower       Upper 
 0.02036472 -0.76198947  0.71190404 

And you can have a look at @BondedDust's answer on exactly how this should be implemented with the ggplot2 function.

like image 150
LyzandeR Avatar answered Jan 07 '23 19:01

LyzandeR