ggplot2中的四分位数范围
interquartile ranges in ggplot2
set.seed(42)
DF <- data.frame(bias=rnorm(2700),cnd=1:27)
DF$cnd <- factor(DF$cnd)
试图了解 median_hilow 在 ggplot 中的用法。我希望找到一种方法来绘制上下四分位数范围。但是我在任何地方都找不到 'fun.data=median_hilow' 的完整解释。即使我认为它在做正确的事情。是否有此函数的完整文档来检查它是如何绘制 IQR 的?
library(ggplot2)
ggplot(DF,aes(x=cnd,y=bias,colour=cnd)) +
stat_summary(fun.data=median_hilow)
认为通过这样做我可以匹配结果证明它正在做我们认为的事情:
library(plyr)
iqr <- function(x, ...) {
qs <- quantile(as.numeric(x), probs = c(0.25, 0.75), na.rm = TRUE)
names(qs) <- c("ymin","ymax")
qs
}
ddply(DF, .(cnd), summarise, new = iqr(bias))
但是这个例子强调了 conf.int
论点的重要性
median_hilow
只是 smedian_hilow
的包装,来自 Hmisc
包。
来自 Hmisc
的 smean / smedian
组函数的文档。
根据下面@BondedDust 的评论,您需要先安装软件包 Hmisc
。
(输入 ?smedian_hilow
和 ?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.
smedian.hilow
根据置信区间计算中位数和上下四分位数。例如:
x <- rnorm(100)
> smedian.hilow(x, conf.int=.5) # 25th and 75th percentiles
Median Lower Upper
0.02036472 -0.76198947 0.71190404
您可以查看@BondedDust 的回答,了解如何使用 ggplot2
函数实现这一点。
如果您想要 IQR,那么您不需要 median_hilow
,至少在默认情况下是这样,因为它提供的低值作为第 2.5 个百分位,而高值作为第 97.5 个百分位。 (IQR 将是第 25 位和第 75 位。)
> smedian.hilow(1:100)
Median Lower Upper
50.500 3.475 97.525
您可以使用 0.5 的 conf.int 以这种方式将 conf.int
参数传递给 Hmisc::smedian.hilow
函数,这将为您提供四分位数范围,因为(如 Hmisc 帮助页面说):" smedian.hilow computes the sample median and a selected pair of outer quantiles having equal tail areas."
:
ggplot(DF,aes(x=cnd,y=bias,colour=cnd)) +
stat_summary(fun.data=median_hilow, conf.int=.5)
set.seed(42)
DF <- data.frame(bias=rnorm(2700),cnd=1:27)
DF$cnd <- factor(DF$cnd)
试图了解 median_hilow 在 ggplot 中的用法。我希望找到一种方法来绘制上下四分位数范围。但是我在任何地方都找不到 'fun.data=median_hilow' 的完整解释。即使我认为它在做正确的事情。是否有此函数的完整文档来检查它是如何绘制 IQR 的?
library(ggplot2)
ggplot(DF,aes(x=cnd,y=bias,colour=cnd)) +
stat_summary(fun.data=median_hilow)
认为通过这样做我可以匹配结果证明它正在做我们认为的事情:
library(plyr)
iqr <- function(x, ...) {
qs <- quantile(as.numeric(x), probs = c(0.25, 0.75), na.rm = TRUE)
names(qs) <- c("ymin","ymax")
qs
}
ddply(DF, .(cnd), summarise, new = iqr(bias))
但是这个例子强调了 conf.int
论点的重要性
median_hilow
只是 smedian_hilow
的包装,来自 Hmisc
包。
来自 Hmisc
的 smean / smedian
组函数的文档。
根据下面@BondedDust 的评论,您需要先安装软件包 Hmisc
。
(输入 ?smedian_hilow
和 ?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.
smedian.hilow
根据置信区间计算中位数和上下四分位数。例如:
x <- rnorm(100)
> smedian.hilow(x, conf.int=.5) # 25th and 75th percentiles
Median Lower Upper
0.02036472 -0.76198947 0.71190404
您可以查看@BondedDust 的回答,了解如何使用 ggplot2
函数实现这一点。
如果您想要 IQR,那么您不需要 median_hilow
,至少在默认情况下是这样,因为它提供的低值作为第 2.5 个百分位,而高值作为第 97.5 个百分位。 (IQR 将是第 25 位和第 75 位。)
> smedian.hilow(1:100)
Median Lower Upper
50.500 3.475 97.525
您可以使用 0.5 的 conf.int 以这种方式将 conf.int
参数传递给 Hmisc::smedian.hilow
函数,这将为您提供四分位数范围,因为(如 Hmisc 帮助页面说):" smedian.hilow computes the sample median and a selected pair of outer quantiles having equal tail areas."
:
ggplot(DF,aes(x=cnd,y=bias,colour=cnd)) +
stat_summary(fun.data=median_hilow, conf.int=.5)