Reordering boxplots by median using `fct_reorder`

74 Views Asked by At

Current Code

box_scottishpolitics_karma <<- ggplot(hedges_scottishpolitics_data,
                                        aes(x=fct_reorder(unique_word, 
                                            utterance_score, median), 
    y=utterance_score)) + 
    geom_boxplot(fill="red", color = "black") +
    scale_y_continuous(trans=scales::pseudo_log_trans(base = 10)) +
    ggtitle("Subreddit: Scottish Politics") +
    xlab("Hedge/Booster") +
    ylab("Karma Score (log10)") +
    coord_flip() +
    theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
    geom_boxplot(data=boosters_scottishpolitics_data, fill="lightblue", 
                 color = "black") +
    coord_flip() +
    theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

Current Output

boxplots, but not sorted by the median values.

I cannot seem to get it to sort the boxplots by their median values.

I have tried using reorder() as well as fct_reorder(). However, neither of these seem to work. Both reorder and fct_reorder produce the same result.

Solution

Since the creation of this post there has been a slight change in data structure. The z-score is now also part of the dataset and must be used to create a plot as well.

# Combining the 2 datasets into one
hedges_scottishpolitics_data <- hedges_scottishpolitics_data %>%
    mutate(type = "hedge")
boosters_scottishpolitics_data <<- boosters_scottishpolitics_data %>%
    mutate(type = "booster")
all_scottishpolitics_data <- rbind(hedges_scottishpolitics_data, boosters_scottishpolitics_data)

# Turning 'type' column into factors (for colouring in boxplot)
all_scottishpolitics_data$type <- as.factor(all_scottishpolitics_data$type)
  all_scottishpolitics_data <- all_scottishpolitics_data %>%
    ungroup() %>%
    # add 2 columns, one ordered by median karma score and one ordered by 
    # median z score. (so we can create both boxplots).
    mutate(word_med_order = fct_reorder(unique_word, utterance_score))%>%
    mutate(word_med_order_z = fct_reorder(unique_word, z_score))
  
  # Karma boxplot, now ordered by median using word_med_order
  box_scottishpolitics_karma <- ggplot(all_scottishpolitics_data, aes(x = word_med_order, y = utterance_score, fill = type)) +
    geom_boxplot(color = "black") +
    scale_y_continuous(trans = scales::pseudo_log_trans(base = 10)) +
    ggtitle("Subreddit: r/scottishpolitics") +
    xlab("Hedge/Booster") +
    ylab("Karma Score (log10)") +
    coord_flip() +
    theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
  
  # Z-score boxplot, ordered by median using word_med_order_z
  box_scottishpolitics_z <- ggplot(all_scottishpolitics_data, aes(x = word_med_order_z, y = z_score, fill = type)) +
    geom_boxplot(color = "black") +
    scale_y_continuous(trans = scales::pseudo_log_trans(base = 10)) +
    ggtitle("Subreddit: r/scottishpolitics") +
    xlab("Hedge/Booster") +
    ylab("Z-Score (log10)") +
    coord_flip() +
    theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))

Resulting (correct) plots

Karma

Z-score

0

There are 0 best solutions below