Sorting ggplot2 plot after a secondary variable while maintaining facet_wrap

44 Views Asked by At

I have a plot which shows 6 plots for a variable called trial (called Base1, Base2, etc) for a variable called load. With 3 loads. To besser compare them I want to have Base 1 of each load (1,3,5) next to each other. Same with Base 2 and so on. I can't figure out how to sort by trial while still maintaining individual plots for each load condition. Anyone got any ideas? Here's my R code:

# do the same as boxplot
load_all_base6_pm6_load_boxplot <- load_all_vp_avg_load |>
  dplyr::filter(trial != "" & trial != "Practice") |>
  ggplot(aes(x = trial, y = avg_color_angle_abs_deviation, fill = trial)) +
  geom_boxplot() +  # Use geom_boxplot to create a boxplot
  geom_point(aes(x = trial, y = avg_color_angle_abs_deviation), color = "black", size = 2) +
  labs(x = "Trial load 5", y = "Average Color Angle Deviation abs") +  # Add axis labels
  theme_minimal() +
  facet_wrap( ~ load, labeller = labeller(load = function(variable) paste("Load", variable)))

load_all_base6_pm6_load_boxplot <- load_all_base6_pm6_load_boxplot +
  geom_text(data = load_all_vp_avg_load %>%
              dplyr::filter(trial != "" & trial != "Practice") %>%
              dplyr::filter(avg_color_angle_abs_deviation %in% outliers_all),
            aes(x = trial, y = avg_color_angle_abs_deviation, label = url_code),
            vjust = -1) +
  facet_wrap(~ load, labeller = labeller(load = function(variable) paste("Load", variable)))

Tried the creating a order desired_order <- c("Base1", "Base2", "Base3")

and implementing it load_all_vp_avg_load$trial <- factor(load_all_vp_avg_load$trial, levels = desired_order)

I tried using reorder ggplot(aes(x = reorder(trial, trial), y = avg_color_angle_abs_deviation, fill = trial)) +

However this only sorts them min to max

This is how it looks atm

0

There are 0 best solutions below