R:箱线图胡须按一组从最小值到最大值

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

我想按组创建一个箱线图,其中每组中的须线从最大值到最小值,而第 500 个最高值点应突出显示。

这是迄今为止没有错误的代码:

# 500th highest
highlight_df <- combined_df %>%
  group_by(Group) %>%
  arrange(desc(SCR)) %>%
  slice(500)

# Plot with whiskers extending to min and max values
ggplot(combined_df, aes(x = Group, y = SCR, fill = Group)) +
  geom_boxplot(outlier.shape = NA, coef = 0) + 
  geom_errorbar(data = combined_df, aes(x = Group, ymin = min(SCR), ymax = max(SCR)), width = 0.2, color = "black", size = 0.9) +  # Draw whiskers manually
  geom_point(data = highlight_df, aes(x = Group, y = SCR), color = "red", size = 3, shape = 18) + 
  scale_fill_manual(values = group_colors) + 
  labs(title = "title",
       x = "Datum",
       y = "y") +
  theme_minimal() +
  theme(legend.position = "none")

Code output

这些点似乎显示得很好,但是,胡须似乎不是为每个组创建的,而是根据整个数据集的最大值和最小值创建的。

我尝试创建一个单独的表,其中计算每个组的最小值和最大值,但这会产生错误:

# 500th highest
highlight_df <- combined_df %>%
  group_by(Group) %>%
  arrange(desc(SCR)) %>%
  slice(500)

# Calculate group-specific min and max values
group_stats <- combined_df %>%
  group_by(Group) %>%
  summarise(min_SCR = min(SCR), max_SCR = max(SCR))

# Plot with whiskers extending to min and max values
ggplot(combined_df, aes(x = Group, y = SCR, fill = Group)) +
  geom_boxplot(outlier.shape = NA, coef = 0) +
  geom_errorbar(data = group_stats, aes(x = Group, ymin = min_SCR, ymax = max_SCR),
                width = 0.2, color = "black", size = 0.9) +
  geom_point(data = highlight_df, aes(x = Group, y = SCR), color = "red", size = 3, shape = 18) +
  scale_fill_manual(values = group_colors) +
  labs(title = "title",
       x = "Datum",
       y = "y") +
  theme_minimal() +
  theme(legend.position = "none")


Error in `geom_errorbar()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 2nd layer.
Caused by error in `FUN()`:
! object 'SCR' not found
r ggplot2 visualization
1个回答
0
投票

将来,请提供最小的、可重现的示例,以便我们更有效地帮助您。

尽管如此,这里还是一个箱线图和须线图的工作示例,其中须线从每组的最小值延伸到最大值,并突出显示了特定值。

library(tidyverse)

set.seed(333)

# Example Dataset
df <- data.frame(
  group = rep(c("a", "b"), each = 20),
  value = round(rnorm(40, 100, 50))
)

# 15th highest
highlight_df <- df %>%
  group_by(group) %>%
  arrange(desc(value)) %>%
  slice(15)

# Calculate group-specific min and max values
group_stats <- df %>%
  group_by(group) %>%
  summarise(min_value = min(value),
            max_value = max(value))

# Plot with whiskers extending to min and max values
df %>%
  ggplot() +
  geom_boxplot(aes(x = group, y = value, fill = group),
               outlier.shape = NA, coef = 0) +
  geom_errorbar(data = group_stats,
                aes(x = group, ymin = min_value, ymax = max_value),
                width = 0.2, color = "black", linewidth = 0.9) +
  geom_point(data = highlight_df,
             aes(x = group, y = value),
             color = "red", size = 3, shape = 18) +
  labs(title = "Box and Whisker Plot with Custom Whiskers",
       x = "Group",
       y = "Value") +
  theme_minimal() +
  theme(legend.position = "none")

创建于 2024-07-02,使用 reprex v2.1.0

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