简体   繁体   English

boxplot ggplot 中位数,第一和第三百分位数 R

[英]boxplot ggplot with median, first and third percentile R

I would like to do a boxplot with two different groups with only three different measurements (is that even possible?).我想用两个不同的组做一个boxplot ,只有三个不同的测量值(这甚至可能吗?)。 Here is my data:这是我的数据:

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"))  

I would like to create a boxplot with County on the x-axis, median, low and high on the y-axis, and ID (either true/false) as fill.我想创建一个boxplot ,x 轴为县,y 轴为中值、低和高,ID(真/假)作为填充。 As a result, I want six different (in this case) boxplots (three false and three true).结果,我想要六个不同的(在这种情况下)箱线图(三个假和三个真)。 But I am not sure how to do this with my data, since I don't have ymin and ymax .但我不确定如何处理我的数据,因为我没有yminymax

I have tried this, but it does not take lower and upper into account:我已经尝试过了,但它没有考虑上下限:

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

Have anyone experienced the same problem?有没有人遇到过同样的问题?

A boxplot is parameterised by:箱线图通过以下方式参数化:

  • ymin : the lower whisker ymin : 下须
  • lower : the 25th percentile lower :第 25 个百分位
  • middle : the 50th percentile middle :第 50 个百分位
  • upper : the 75th percentile upper :第 75 个百分位
  • ymax : the upper whisker ymax : 上胡须

As you correctly pointed out, it seems that we cannot fit these 5 parameters with only three observations per group.正如您正确指出的那样,我们似乎无法将这 5 个参数拟合为每组仅三个观察值。 However, you might be interested in the geom_crossbar() layer, which will give you a boxplot-like appearance without the whiskers, and it requires only 3 parameters.但是,您可能对geom_crossbar()层感兴趣,它会为您提供没有胡须的箱线图外观,并且只需要 3 个参数。 Example below:下面的例子:

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM