gpt4 book ai didi

r - 使用ggplot手动绘制箱线图

转载 作者:行者123 更新时间:2023-12-02 01:44:47 24 4
gpt4 key购买 nike

我认为我的问题与 this one 非常相似,唯一的区别是我喜欢使用 ggplot (并且 ggplot 的答案缺少一点细节)。我有这样的数据:

show<-structure(list(Median = c(20, 39, 21, 52, 45.5, 24, 36, 20, 134, 
27, 44, 43), IQR = c(4, 74, 28, 51.5, 73.5, 18, 47.5, 26.5, 189.5,
46, 54, 61), FirstQuartile = c(`25%` = 19, `25%` = 24, `25%` = 12,
`25%` = 30.5, `25%` = 36.5, `25%` = 18, `25%` = 16.5, `25%` = 13,
`25%` = 53.5, `25%` = 15, `25%` = 24.5, `25%` = 27), ThirdQuartile = c(`75%` = 23,
`75%` = 98, `75%` = 40, `75%` = 82, `75%` = 110, `75%` = 36,
`75%` = 64, `75%` = 39.5, `75%` = 243, `75%` = 61, `75%` = 78.5,
`75%` = 88), Group = c("Program Director", "Editor", "Everyone",
"Board Director", "Board Director", "Program Director", "Editor",
"Everyone", "Board Director", "Everyone", "Editor", "Program Director"
), Decade = c("1980's", "1980's", "1980's", "1980's", "1990's",
"1990's", "1990's", "1990's", "2000's", "2000's", "2000's", "2000's"
)), row.names = c(NA, -12L), class = c("tbl_df", "tbl", "data.frame"
))

我想画一个这样的图表: enter image description here

以“团体”为颜色,而不是“团契”。问题是,该图是从“完整”数据(有 800 行左右)绘制的,而我显然只有上面的摘要数据。我意识到它无法绘制异常值,但没关系。任何帮助,将不胜感激!我特别纠结如何绘制 ymin/max 和凹口的边缘。谢谢

最佳答案

您可以将 geom_boxplot()stat = "identity" 一起使用,并填写五个箱线图数字作为美观。

library(ggplot2)

# show <- structure(...) # omitted for previty

ggplot(show, aes(Decade, fill = Group)) +
geom_boxplot(
stat = "identity",
aes(lower = FirstQuartile,
upper = ThirdQuartile,
middle = Median,
ymin = FirstQuartile - 1.5 * IQR, # optional
ymax = ThirdQuartile + 1.5 * IQR) # optional
)

正如 jpsmith 在下面的评论中指出的那样,如果您没有数据的范围,1.5 * IQR 规则就会变得很棘手。但是,如果您有有关数据极值或数据域的信息,则可以按如下方式限制须线:

# Dummy values assuming data is >= 0 up to infinity
show$min <- 0
show$max <- Inf

ggplot(show, aes(Decade, fill = Group)) +
geom_boxplot(
stat = "identity",
aes(lower = FirstQuartile,
upper = ThirdQuartile,
middle = Median,
ymin = pmax(FirstQuartile - 1.5 * IQR, min),
ymax = pmin(ThirdQuartile + 1.5 * IQR, max))
)

关于r - 使用ggplot手动绘制箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71053105/

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