我需要在 R 中創建一個自定義箱線圖,它使用組成箱線的分位數 0.05、0.20、0.50、0.80 和 0.95,而不是默認值。
默認圖是使用以下代碼生成的:
ggplot(data, aes(Site, LOG10Val))
geom_boxplot()
看起來像這樣:

要指定箱線圖的自定義邊界,我使用的代碼是:
ggplot(data, aes(Site, LOG10Val))
stat_summary(geom = "boxplot",
fun.data = function(x) setNames(quantile(x, c(0.05, 0.2, 0.5, 0.8, 0.95)),
c("ymin", "lower", "middle", "upper", "ymax")),
position = "dodge")
情節變成:

有沒有辦法將例外值(即> 95%)重新引入自定義箱線圖中?
謝謝。
編輯:我的資料結構如下:
# A tibble: 6 x 5
Date Site Analyte Value LOG10Val
<date> <fct> <fct> <dbl> <dbl>
1 2014-01-10 E Ammonia_mg.L 0.02 -1.70
2 2014-01-10 C Ammonia_mg.L 0.01 -2
3 2014-01-10 D Ammonia_mg.L 0.015 -1.82
4 2014-01-31 E Ammonia_mg.L 0.01 -2
5 2014-01-31 C Ammonia_mg.L 0.01 -2
6 2014-01-31 D Ammonia_mg.L 0.01 -2
uj5u.com熱心網友回復:
實作您想要的結果的一種選擇是通過第二stat_summary層添加例外值。
使用iris作為示例資料:
library(ggplot2)
ggplot(iris, aes(x = Species, y = Sepal.Length))
stat_summary(
geom = "boxplot",
fun.data = function(x) {
setNames(
quantile(x, c(0.05, 0.2, 0.5, 0.8, 0.95)),
c("ymin", "lower", "middle", "upper", "ymax")
)
}
)
stat_summary(geom = "point", fun = function(x) {
outlier_high <- x > quantile(x, .95)
outlier_low <- x < quantile(x, .05)
ifelse(outlier_high | outlier_low, x, NA)
}, na.rm = TRUE)

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/328489.html
