箱线图 ggplot 中位数,第一和第三百分位数 R

boxplot ggplot with median, first and third percentile R

我想对两个不同的组进行 boxplot,只有三个不同的测量值(这可能吗?)。这是我的数据:

data <- data.frame( "County" = 1:6, Median = c(5,7,8,2,4, 5), Low = c( 0.5,2,4,1,2,3),
                    High = c(10,12,11,9,10,15), ID = c("TRUE", "TRUE", "FALSE", "TRUE", "FALSE", "FALSE"))  

我想创建一个 boxplot,x 轴为县,y 轴为中位数、低和高,ID(true/false)作为填充。因此,我想要六个不同的(在本例中)箱线图(三个错误和三个正确)。但我不确定如何使用我的数据执行此操作,因为我没有 yminymax.

我已经试过了,但是它没有考虑上下限:

ggplot(dat, aes(x = County, y = Median, lower = Low, upper = High, fill = ID)) +
  geom_boxplot()

有没有人遇到过同样的问题?

箱线图参数化为:

  • ymin: 下须
  • lower:第 25 个百分位数
  • middle:第 50 个百分位数
  • upper:第 75 个百分位数
  • ymax: 上须

正如您正确指出的那样,我们似乎无法用每组仅三个观测值来拟合这 5 个参数。但是,您可能对 geom_crossbar() 层感兴趣,它会给您一个没有胡须的类似箱线图的外观,并且它只需要 3 个参数。下面的示例:

library(ggplot2)

data <- data.frame( "County" = 1:6, Median = c(5,7,8,2,4, 5), Low = c( 0.5,2,4,1,2,3),
                    High = c(10,12,11,9,10,15), ID = c("TRUE", "TRUE", "FALSE", "TRUE", "FALSE", "FALSE")) 

ggplot(data, aes(x = as.factor(County), 
                 y = Median, 
                 ymin = Low, 
                 ymax = High)) +
  geom_crossbar(aes(colour = ID))