如何将水平胡须添加到我为数据制作的箱线图中(在 R 中)?

问题描述 投票:0回答:1

我有一个名为“processed_AGB_BA”的数据框,其中我想在两列中绘制两个数据箱线图,名为:“AGB_patch_conv”和“AGB_patch_arboreal”。我的目标是在一张图中对两个箱线图进行并排比较,包括水平胡须。

这让您了解数据框的样子:

> dput(head(processed_AGB_BA))
structure(list(patch_ID = c("1st near school", "SRB_OUT_01_M", 
"SRB_OUT_01_NM", "SRB_OUT_02_M", "SRB_OUT_02_NM", "SRB_OUT_21_M"
), AGB_patch_conv = c(66295.8445967275, 49135.973074405, 51484.2294385078, 
41572.738788761, 71509.7267936017, 28132.3367655501), BA_patch_conv = c(36.5263013888889, 
24.1978819444444, 27.54125, 21.865337037037, 45.5953240740741, 
14.8647916666667), AGB_patch_arboreal = c(51387.2269090947, 53555.0952762403, 
64546.2000860895, 38690.7951211127, 43723.8206226884, 25208.3905166321
), BA_patch_arboreal = c(28.2327368055556, 20.7482475974128, 
23.4128590199378, 30.5281148491577, 30.4031276527036, 12.8046908575741
)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))

这个代码给了我部分我想要的东西:

ggplot(processed_AGB_BA, aes(x = factor(1), y = AGB_patch_conv)) +
  geom_boxplot(fill = "transparent", color = "black", coef = 1.5) +
  geom_boxplot(aes(x = factor(2), y = AGB_patch_arboreal), fill = "transparent", color = "black", coef = 1.5) +
  labs(x = "", y = "AGB [kg/ha]") +
  scale_x_discrete(labels = c("Conventional", "Arboreal Forest")) +
  ggtitle("Comparison of AGB: Conventional vs. Arboreal") +
  theme_minimal() +
  theme(panel.background = element_rect(fill = "white"),
        axis.text.x = element_text(size = 12))

我没有成功的是,将水平胡须添加到线条的末尾(四分位距的 1.5 倍)。使用 stat_boxplot(geom = "errorbar", width = 0.5) 只会在第一个箱线图上给出一条线,而在第二个箱线图中则没有任何内容(见图)。

我认为这与“aes(x = Factor(1)...)”的使用有关

有人对我如何解决这个问题有任何建议吗?

boxplot with wrong whiskers

r visualization boxplot
1个回答
0
投票
library(tidyverse)

data <- tibble(
  patch_ID = c(
    "1st near school",
    "SRB_OUT_01_M",
    "SRB_OUT_01_NM",
    "SRB_OUT_02_M",
    "SRB_OUT_02_NM",
    "SRB_OUT_21_M"
  ),
  AGB_patch_conv = c(
    66295.8445967275,
    49135.973074405,
    51484.2294385078,
    41572.738788761,
    71509.7267936017,
    28132.3367655501
  ),
  BA_patch_conv = c(
    36.5263013888889,
    24.1978819444444,
    27.54125,
    21.865337037037,
    45.5953240740741,
    14.8647916666667
  ),
  AGB_patch_arboreal = c(
    51387.2269090947,
    53555.0952762403,
    64546.2000860895,
    38690.7951211127,
    43723.8206226884,
    25208.3905166321
  ),
  BA_patch_arboreal = c(
    28.2327368055556,
    20.7482475974128,
    23.4128590199378,
    30.5281148491577,
    30.4031276527036,
    12.8046908575741
  )
) |> 
  select(AGB_patch_conv, AGB_patch_arboreal) |> 
  pivot_longer(cols = everything())
  
    
data |> 
  ggplot(aes(x = name, y = value)) +
  stat_boxplot(geom = "errorbar") +
  geom_boxplot(fill = "transparent", color = "black", coef = 1.5) +
  labs(x = "", y = "AGB [kg/ha]") +
  scale_x_discrete(labels = c("Arboreal Forest", "Conventional")) +
  ggtitle("Comparison of AGB: Arboreal vs. Conventional") +
  theme_minimal() +
  theme(panel.background = element_rect(fill = "white"),
        axis.text.x = element_text(size = 12))

所以,我做的第一件事就是使用

select(AGB_patch_conv, AGB_patch_arboreal) |>  pivot_longer(cols = everything())

将数据重塑为“长”格式

这意味着您不必两次调用

geom_boxplot
,因为您可以在
aes(x = name....)
内部提供
ggplot
。 (
name
pivot_wider
自动赋予重新整形的数据标签的名称,
value
是它赋予包含值的列的名称。

@moooh 的答案中提供了重要的一点 - 在 ggplot 中使用

stat_boxplot(geom = "errorbar")
,你的胡须上就得到了所需的线条。但无论如何,为 ggplot 提供良好的数据结构是件好事。

特别是对于您的数据集,我可能会从以下内容开始:

data_for_graph <-
    processed_AGB_BA |> 
    select(AGB_patch_conv, AGB_patch_arboreal) |>    
    pivot_longer(cols = everything())`

为您的图表制作数据集,然后

data_for_graph |>
    ggplot(...

...正如我上面已经描述过的

© www.soinside.com 2019 - 2024. All rights reserved.