gpt4 book ai didi

r - 不同的分位数 : Boxplot vs. Violinplot

转载 作者:行者123 更新时间:2023-12-01 18:53:05 25 4
gpt4 key购买 nike

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")

boxplot vs. violinplot

据我所知,箱线图中的箱尾分别代表 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”方面)。

引用文献:

  1. http://docs.ggplot2.org/current/geom_violin.html

  2. http://docs.ggplot2.org/current/geom_boxplot.html

最佳答案

这对于评论来说太长了,所以我将其作为答案发布。我认为这种差异有两个潜在来源。首先,我的理解是 boxplot 指的是 boxplot.stats,它使用与分位数非常相似但不一定相同的铰链。 ?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.

因此,铰链与分位数的区别可能是差异的来源之一。

其次,geom_violin 指的是密度估计。源码here指向一个函数StatY密度,它引导我到here 。我找不到函数compute_密度,但我认为(也是由于帮助文件中的一些指针)它本质上是密度,默认情况下使用高斯核估计来估计密度。这可能(或可能不能)解释差异,但是

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))

确实显示出不同的值。因此,我猜测差异是由于我们是基于观察的经验分布函数还是基于核密度估计来查看分位数,尽管我承认我还没有最终证明这一点。

关于r - 不同的分位数 : Boxplot vs. Violinplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36033341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com