简体   繁体   English

ggplot2 - 使用 geom_boxplot 生成带有自定义晶须的水平箱线图时出错

[英]ggplot2 - Error while generating a horizontal boxplot with custom whiskers using geom_boxplot

For educational purposes, I am trying to generate a horizontal boxplot combined with a dotplot using ggplot2. However, I would like to generate a custom boxplot with whiskers at the 2.5% and 97.5% percentiles instead of the 1.5*IQR definition that is used by geom_boxplot .出于教育目的,我正在尝试使用 ggplot2 生成一个水平箱线图和一个点图。但是,我想生成一个自定义箱线图,其胡须位于 2.5% 和 97.5% 百分位数,而不是 1.5*IQR 定义geom_boxplot Therefore, I decided to use the following code:因此,我决定使用以下代码:

y <- rnorm(100)
df = data.frame(y)
df_boxplot <- data.frame(
  x_coord = 0.5,
  y0 = quantile(y, 0.025),
  y25 = quantile(y, 0.25),
  y50 = median(y),
  y75 = quantile(y, 0.75),
  y100 = quantile(y, 0.975)
)
# Vertical orientation with custom whiskers works
ggplot() +
  geom_boxplot(data = df_boxplot,
    aes(x = x_coord, ymin = y0, lower = y25, middle = y50, upper = y75, ymax = y100),
    stat = "identity"
  ) +
  geom_jitter(data = df, aes(y=y, x=0.5))
# Horizontal orientation with custom whiskers throws an error
ggplot() +
  geom_boxplot(data = df_boxplot,
               aes(y=x_coord, xmin = y0, xlower = y25, xmiddle = y50, xupper = y75, xmax = y100),
               stat = "identity"
  ) +
  geom_dotplot(data = df, aes(x=y))

# Using horizontal stat_summary with custom whiskers works
f <- function(x) {
  r <- quantile(x, probs = c(0.025, 0.25, 0.5, 0.75, 0.975))
  names(r) <- c("ymin", "lower", "middle", "upper", "ymax")
  r
}
ggplot() +
  stat_summary(data = df, aes(x=y, y=0.5), fun.data=f, geom="boxplot", position="dodge2", orientation = "y") +
  geom_dotplot(data = df, aes(x=y))

However, when using the horizontal orientation together with the custom definition of whiskers, I get the following error:但是,当使用水平方向和胡须的自定义定义时,出现以下错误:

Error in `$<-.data.frame`(` tmp `, "xmin", value = numeric(0)): `$<-.data.frame`(` tmp` , "xmin", value = numeric(0)) 错误:

Replacement has 0 rows, data has 1替换有 0 行,数据有 1

This error seems to be geom_boxplot specific, as it works fine with stat_summary .这个错误似乎是geom_boxplot特定的,因为它与stat_summary一起工作得很好。 I would like to ask what the reason for the error in geom_boxplot is and how to fix it.请问geom_boxplot报错的原因是什么,如何解决。 The plot is supposed to look something like this: plot 应该看起来像这样:

组合点图和箱线图

You need to set orientation = 'y' to get the horizontal boxplot directly from geom_boxplot .您需要设置orientation = 'y'以直接从geom_boxplot获取水平箱线图。 You might think geom_boxplot would guess this from the supplied aesthetics, but it doesn't.您可能认为geom_boxplot会从提供的美学中猜出这一点,但事实并非如此。

ggplot() +
  geom_boxplot(data = df_boxplot,
               aes(y = x_coord, xmin = y0, xlower = y25, xmiddle = y50, 
                   xupper = y75, xmax = y100),
               stat = "identity", orientation = 'y'
  ) +
  geom_dotplot(data = df, aes(x=y))

在此处输入图像描述

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

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