ggplot 没有按预期绘制箱线图

ggplot not drawing boxplots as expected

考虑下面的 MWE。我想根据这些想法生成箱线图:

  1. Food 在 y 轴上根据 Amot 排序 Home,而 Amt (1:40) 在 x 轴上
  2. 显示覆盖框的平均点
  3. Food 的箱线图根据 dfsummary 数据
  4. Home 站点 的中位数排序
  5. N 观察结果的文本注释(取自 dfsummary 数据)

MWE

df <- data.frame(
  Site = sample(rep(c("Home", "Office"), size = 884)),
  Food = sample(rep(c("Banana","Apple","Egg","Berry","Tomato","Potato","Bean","Pea","Nuts","Onion","Carrot","Cabbage","Eggplant"), size=884)),
  Amt = sample(seq(1, 40, by = 0.25), size = 884, replace = TRUE)
)
random <- sample(seq(1, 884, by = 1), size = 100, replace = TRUE) # to randomly introduce 100 NAs to Amt vector
df$Amt[random] <- NA

摘要代码

dfsummary <- df %>%
  dplyr::group_by(Food, Site) %>%
  dplyr::summarise(Median = round(median(Amt, na.rm=TRUE), digits=2), N = sum(!is.na(Amt))) %>%
  ungroup()

ggplot代码

p1 <- ggplot(df, aes(Amt, Food)) +
  geom_boxplot() +
  facet_grid(facets = . ~ Site)

图表

我期待在这里看到箱线图。

添加注释

p2 <- p1 + geom_text(aes(y = 42, Food, label = paste("n=", N)), data = dfsummary, size = 3, nudge_x = 0.1) +
  facet_grid(facets = . ~ Site)

不幸的是,这也不起作用。

备注

要解决这个问题,您可能需要生成一个散点图,首先:

library(ggplot2)
p1 <- ggplot(df, aes(Amt, Food)) +
  geom_point() +
  facet_grid(facets = . ~ Site)
p1

如您所见,无法生成箱线图。 但是,如果您切换 xy

ggplot(df, aes(Food, Amt)) +
  geom_boxplot() +
  facet_grid(facets = . ~ Site)

你得到:

这在 ggplot2 的当前开发版本中运行良好,将于 2020 年 1 月发布。

# If your ggplot2 version is <= 3.2.1, do:
# remotes::install_github("tidyverse/ggplot2")
library(tidyverse)

df <- data.frame(
  Site = sample(rep(c("Home", "Office"), size = 884)),
  Food = sample(rep(c("Banana","Apple","Egg","Berry","Tomato","Potato","Bean","Pea","Nuts","Onion","Carrot","Cabbage","Eggplant"), size=884)),
  Amt = sample(seq(1, 40, by = 0.25), size = 884, replace = TRUE)
)
random <- sample(seq(1, 884, by = 1), size = 100, replace = TRUE) # to randomly introduce 100 NAs to Amt vector
df$Amt[random] <- NA

ggplot(df, aes(Amt, Food)) +
  geom_boxplot() +
  facet_grid(facets = . ~ Site)
#> Warning: Removed 98 rows containing non-finite values (stat_boxplot).

reprex package (v0.3.0)

于 2020-01-01 创建

有注释:

library(tidyverse)

df <- data.frame(
  Site = sample(rep(c("Home", "Office"), size = 884)),
  Food = sample(rep(c("Banana","Apple","Egg","Berry","Tomato","Potato","Bean","Pea","Nuts","Onion","Carrot","Cabbage","Eggplant"), size=884)),
  Amt = sample(seq(1, 40, by = 0.25), size = 884, replace = TRUE)
)
random <- sample(seq(1, 884, by = 1), size = 100, replace = TRUE) # to randomly introduce 100 NAs to Amt vector
df$Amt[random] <- NA

dfsummary <- df %>%
  dplyr::group_by(Food, Site) %>%
  dplyr::summarise(Median = round(median(Amt, na.rm=TRUE), digits=2), N = sum(!is.na(Amt))) %>%
  ungroup()

ggplot(df, aes(Amt, Food)) +
  geom_boxplot() +
  geom_text(
    aes(x = 42, Food, label = paste("n=", N)),
    data = dfsummary,
    size = 3, nudge_x = 0.1
  ) +
  facet_grid(facets = . ~ Site)
#> Warning: Removed 95 rows containing non-finite values (stat_boxplot).

reprex package (v0.3.0)

于 2020-01-01 创建