不同的分位数:箱线图与小提琴图
Differing quantiles: Boxplot vs. Violinplot
require(ggplot2)
require(cowplot)
d = iris
ggplot2::ggplot(d, aes(factor(0), Sepal.Length)) +
geom_violin(fill="black", alpha=0.2, draw_quantiles = c(0.25, 0.5, 0.75)
, colour = "red", size = 1.5) +
stat_boxplot(geom ='errorbar', width = 0.1)+
geom_boxplot(width = 0.2)+
facet_grid(. ~ Species, scales = "free_x") +
xlab("") +
ylab (expression(paste("Value"))) +
coord_cartesian(ylim = c(3.5,9.5)) +
scale_y_continuous(breaks = seq(4, 9, 1)) +
theme(axis.text.x=element_blank(),
axis.text.y = element_text(size = rel(1.5)),
axis.ticks.x = element_blank(),
strip.background=element_rect(fill="black"),
strip.text=element_text(color="white", face="bold"),
legend.position = "none") +
background_grid(major = "xy", minor = "none")
据我所知,箱形图中的方框末端分别代表 25% 和 75% 的分位数,中位数 = 50%。因此它们应该等于 draw_quantiles = c(0.25, 0.5, 0.75)
参数中 geom_violin
绘制的 0.25/0.5/0.75 分位数。
中位数和 50% 分位数拟合。但是,0.25 和 0.75 分位数都不适合箱线图的箱端(见图,尤其是 'virginica' 方面)。
参考文献:
这对于评论来说太长了,所以我 post 它作为一个答案。我看到了两个潜在的分歧来源。首先,我的理解是 boxplot
指的是 boxplot.stats
,它使用与分位数非常相似但不一定相同的 hinges
。 ?boxplot.stats
说:
The two ‘hinges’ are versions of the first and third quartile, i.e.,
close to quantile(x, c(1,3)/4). The hinges equal the quartiles for odd
n (where n <- length(x)) and differ for even n. Whereas the quartiles
only equal observations for n %% 4 == 1 (n = 1 mod 4), the hinges do
so additionally for n %% 4 == 2 (n = 2 mod 4), and are in the middle
of two observations otherwise.
hinge vs quantile
区别可能是差异的来源之一。
其次,geom_violin
指的是密度估计。源代码here points to a function StatYdensity
, which leads me to here。我找不到函数 compute_density
,但我认为(也是由于帮助文件中的一些指针)它本质上是 density
,它默认使用高斯核估计来估计密度。这可能(或可能不会)解释差异,但
by(d$Sepal.Length, d$Species, function(x) boxplot.stats(x, coef=5)$stats )
by(d$Sepal.Length, d$Species, function(v) quantile(density(v)$x))
确实显示了不同的值。所以,我猜想差异是由于我们是基于观察的经验分布函数还是基于核密度估计来查看分位数,尽管我承认我还没有最终证明这一点。
第二个因素似乎是主要原因。这里有更多证据支持这一点。
通过切换到 geom_ydensity
,可以凭经验确认差异是由于 geom_violin
使用核密度估计来计算分位数,而不是实际观察。例如,如果我们强制使用较宽的带宽 (bw=1
),则估计的密度将被过度平滑并进一步偏离箱线图中使用的基于观察的分位数:
require(ggplot2)
require(cowplot)
theme_set(cowplot::theme_cowplot())
d = iris
ggplot2::ggplot(d, aes(factor(0), Sepal.Length)) +
stat_ydensity(bw=1, fill="black", alpha=0.2, draw_quantiles = c(0.25, 0.5, 0.75)
, colour = "red", size = 1.5) +
stat_boxplot(geom ='errorbar', width = 0.1)+
geom_boxplot(width = 0.2)+
facet_grid(. ~ Species, scales = "free_x") +
xlab("") +
ylab (expression(paste("Value"))) +
coord_cartesian(ylim = c(3.5,9.5)) +
scale_y_continuous(breaks = seq(4, 9, 1)) +
theme(axis.text.x=element_blank(),
axis.text.y = element_text(size = rel(1.5)),
axis.ticks.x = element_blank(),
strip.background=element_rect(fill="black"),
strip.text=element_text(color="white", face="bold"),
legend.position = "none") +
background_grid(major = "xy", minor = "none")
所以,是的,要小心这个 - 密度估计的参数会影响结果!
require(ggplot2)
require(cowplot)
d = iris
ggplot2::ggplot(d, aes(factor(0), Sepal.Length)) +
geom_violin(fill="black", alpha=0.2, draw_quantiles = c(0.25, 0.5, 0.75)
, colour = "red", size = 1.5) +
stat_boxplot(geom ='errorbar', width = 0.1)+
geom_boxplot(width = 0.2)+
facet_grid(. ~ Species, scales = "free_x") +
xlab("") +
ylab (expression(paste("Value"))) +
coord_cartesian(ylim = c(3.5,9.5)) +
scale_y_continuous(breaks = seq(4, 9, 1)) +
theme(axis.text.x=element_blank(),
axis.text.y = element_text(size = rel(1.5)),
axis.ticks.x = element_blank(),
strip.background=element_rect(fill="black"),
strip.text=element_text(color="white", face="bold"),
legend.position = "none") +
background_grid(major = "xy", minor = "none")
据我所知,箱形图中的方框末端分别代表 25% 和 75% 的分位数,中位数 = 50%。因此它们应该等于 draw_quantiles = c(0.25, 0.5, 0.75)
参数中 geom_violin
绘制的 0.25/0.5/0.75 分位数。
中位数和 50% 分位数拟合。但是,0.25 和 0.75 分位数都不适合箱线图的箱端(见图,尤其是 'virginica' 方面)。
参考文献:
这对于评论来说太长了,所以我 post 它作为一个答案。我看到了两个潜在的分歧来源。首先,我的理解是 boxplot
指的是 boxplot.stats
,它使用与分位数非常相似但不一定相同的 hinges
。 ?boxplot.stats
说:
The two ‘hinges’ are versions of the first and third quartile, i.e., close to quantile(x, c(1,3)/4). The hinges equal the quartiles for odd n (where n <- length(x)) and differ for even n. Whereas the quartiles only equal observations for n %% 4 == 1 (n = 1 mod 4), the hinges do so additionally for n %% 4 == 2 (n = 2 mod 4), and are in the middle of two observations otherwise.
hinge vs quantile
区别可能是差异的来源之一。
其次,geom_violin
指的是密度估计。源代码here points to a function StatYdensity
, which leads me to here。我找不到函数 compute_density
,但我认为(也是由于帮助文件中的一些指针)它本质上是 density
,它默认使用高斯核估计来估计密度。这可能(或可能不会)解释差异,但
by(d$Sepal.Length, d$Species, function(x) boxplot.stats(x, coef=5)$stats )
by(d$Sepal.Length, d$Species, function(v) quantile(density(v)$x))
确实显示了不同的值。所以,我猜想差异是由于我们是基于观察的经验分布函数还是基于核密度估计来查看分位数,尽管我承认我还没有最终证明这一点。
第二个因素
通过切换到 geom_ydensity
,可以凭经验确认差异是由于 geom_violin
使用核密度估计来计算分位数,而不是实际观察。例如,如果我们强制使用较宽的带宽 (bw=1
),则估计的密度将被过度平滑并进一步偏离箱线图中使用的基于观察的分位数:
require(ggplot2)
require(cowplot)
theme_set(cowplot::theme_cowplot())
d = iris
ggplot2::ggplot(d, aes(factor(0), Sepal.Length)) +
stat_ydensity(bw=1, fill="black", alpha=0.2, draw_quantiles = c(0.25, 0.5, 0.75)
, colour = "red", size = 1.5) +
stat_boxplot(geom ='errorbar', width = 0.1)+
geom_boxplot(width = 0.2)+
facet_grid(. ~ Species, scales = "free_x") +
xlab("") +
ylab (expression(paste("Value"))) +
coord_cartesian(ylim = c(3.5,9.5)) +
scale_y_continuous(breaks = seq(4, 9, 1)) +
theme(axis.text.x=element_blank(),
axis.text.y = element_text(size = rel(1.5)),
axis.ticks.x = element_blank(),
strip.background=element_rect(fill="black"),
strip.text=element_text(color="white", face="bold"),
legend.position = "none") +
background_grid(major = "xy", minor = "none")
所以,是的,要小心这个 - 密度估计的参数会影响结果!