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

标签 r ggplot2

我认为我的问题与 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/

相关文章:

r - dplyr 根据订单条件和 if 语句进行汇总

r - 在 R 中使用 quanteda 的 2 个单词短语搭配

用相邻行 "ROW"而非列中的值替换行中的 NA

r - 语音到文本转换 R

r - facet_wrap 标题换行和 free_y 轴上的小数位数 (ggplot2)

r - 分组条形图 : order x-axis & keep constant bar width, 以防缺少级别

r - 在 R 中使用 lapply 绘制多个数据帧

r - gganimate:在标题表达式中包含除状态级别变量或框架以外的其他变量

r - 将大型数据集加载到 R 中的最快方法和最快格式是什么

r - 绘图切断 X 和 Y 标签