Search code examples
rggplot2visualization

R: Boxplot whiskers going from minimum to maximum by a group


I would like to create a boxplot by groups where whiskers go from the maximum to minimum in each group, while the 500th highest value point should be highlighted.

This is the code that has worked without an error so far:

# 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

The points seem to show just fine, however, it seems that the whiskers are not created for each group but from the maximum and minimum of the whole dataset instead.

I have tried to create a separate table where min and max for each group is calculated, however this produces an error:

# 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

Solution

  • In the future, please provide a minimum, reproducible example so that we can more effectively help you.

    Nevertheless, here is a working example of a box and whisker plot where the whiskers extend from the minimum to the maximum value per group with a specific value highlighted.

    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_errorbar(data = group_stats,
                    aes(x = group, ymin = min_value, ymax = max_value),
                    width = 0.2, color = "black", linewidth = 0.9) +
      geom_boxplot(aes(x = group, y = value, fill = group),
                   outlier.shape = NA, coef = 0) +
      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")
    

    Created on 2024-07-02 with reprex v2.1.0