R 中第 10 个和第 90 个百分位数的附加线的箱线图

问题描述 投票:0回答:1

不幸的是,我在 R 方面不是很有经验,但我需要解决一个问题,这对我来说似乎很难,但如果知道如何在 R 中使用箱线图,可能会很容易。如果你能提供帮助,我将不胜感激我用这个:

我需要在分组箱线图中为第 10 个和第 90 个百分位数添加额外的水平线或点。除此之外,箱线图应具有共同特征,例如最小值、最大值、具有通常的第 25 个百分位数、中位数和第 75 个百分位数和异常值的框。

我尝试调整这里发布的几个解决方案,但没有一个适用于我的情况。一个有前途的尝试类似于下面编写函数的解决方案 - 但我需要中位数而不是平均值,除此之外我还需要显示第 10 个和第 90 个百分位数。此外,通过变量

Col 对框进行分组也很重要(请参见下面的示例代码):

如果你能给我一些解决办法,我将不胜感激!

dataset_stack <- structure(list(Col = c("Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Green", "Green", "Green", "Green", "Green", "Green", "Green", "Green", "Green", "Green", "Green", "Green", "Green", "Green", "Green", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red"), TTC = c(0.9, 0.7, 0, 0.1, 0.1, 0.4, 0.9, 0.8, 0.1, 0, 0.7, 0.2, 0.7, 0.2, 0, 0.8, 0.7, 0.8, 0.9, 0.3, 0.9, 0.8, 0.3, 1, 0.6, 0.4, 0.3, 0.3, 0.3, 0.2, 0.2, 0.7, 0.9, 0.9, 0.6, 0.4, 0.1, 0.4, 0.8, 0, 0.7, 0.4, 0.7)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -43L)) bp.vals <- function(x, probs=c(0.1, 0.25, 0.75, .9)) { r <- quantile(x, probs=probs , na.rm=TRUE) r = c(r[1:2], exp(mean(log(x))), r[3:4]) names(r) <- c("ymin", "lower", "middle", "upper", "ymax") r } # Sample usage of the function with the built-in mtcars data frame ggplot(dataset_stack, aes(x=factor(Col), y=TTC)) + stat_summary(fun.data=bp.vals, geom="boxplot")
    
r function group-by boxplot percentile
1个回答
0
投票
您可以使用

stat_summary()

 函数并添加一个 
fun()
 来指示特定的 
quantile()
 作为彩色点:

ggplot(dataset_stack, aes(x=factor(Col), y=TTC)) + geom_boxplot() + stat_summary(fun.y="mean", geom="point", shape=16, size=4, fill="black") + stat_summary(geom = "point", fun = \(x) quantile(x, 0.1),shape=16, size=4,color="red")+ stat_summary(geom = "point", fun = \(x) quantile(x, 0.9),shape=16, size=4,color="blue")+ theme_bw()

如果您想添加

min

max
 值,例如 
grey
 颜色,您可以使用函数 
stat_summary()
 :

ggplot(dataset_stack, aes(x=factor(Col), y=TTC)) + geom_boxplot() + stat_summary(fun.y="mean", geom="point", shape=16, size=4, color="black") + stat_summary(fun.y="min", geom="point", shape=16, size=4, color="grey") + stat_summary(fun.y="max", geom="point", shape=16, size=4, color="grey") + theme_bw()

加起来:

ggplot(dataset_stack, aes(x=factor(Col), y=TTC)) + geom_boxplot() + stat_summary(fun.y="mean", geom="point", shape=16, size=4, fill="black") + stat_summary(fun.y="min", geom="point", shape=16, size=4, color="grey") + stat_summary(fun.y="max", geom="point", shape=16, size=4, color="grey") + stat_summary(geom = "point", fun = \(x) quantile(x, 0.1),shape=16, size=4,color="red")+ stat_summary(geom = "point", fun = \(x) quantile(x, 0.9),shape=16, size=4,color="blue")+ theme_bw()

© www.soinside.com 2019 - 2024. All rights reserved.