[英]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
.但我不确定如何处理我的数据,因为我没有
ymin
和ymax
。
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.