Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R – ggplot2 order of the bar colors and labels

I’m trying to get my bar plot to change the order of the labels. I’m trying to get the labels to show up in the order of the bars. Therefore, I’d like the order to be w1, w2, w3. What am I missing? Below is code example:

rm(list = ls())
library(ggplot2)
library(tidyr)
library(tidyverse)

df <- data.frame(
  experience = c("C Exp", "C Exp", "No E Exp", "No E Exp", "Other E Exp", "Other E Exp"),
  overall_rec = c("Agree", "Disagree", "Agree", "Disagree", "Agree", "Disagree"),
  pct_overall_w1 = c(0.6326530612, 0.3673469388, 0.8604651163, 0.1395348837, 0.7465277778, 0.2534722222),
  pct_overall_w2 = c(0.05882352941, 0.9411764706, 0.06097560976, 0.9390243902, 0.05211726384, 0.9478827362),
  pct_overall_w3 = c(0.04615384615, 0.9538461538, 0.1, 0.9, 0.08076009501, 0.919239905)
)

# Filter the data frame to include only rows where overall_rec = Agree
df_filtered <- subset(df, overall_rec == "Agree") %>% 
  gather(key = "pct_overall", value = "percentage", pct_overall_w1:pct_overall_w3) %>% 
  ungroup() %>% 
  select(experience, pct_overall, percentage) %>% 
  arrange(experience)

df_filtered$pct_overall <- factor(df_filtered$pct_overall, levels = c("pct_overall_w3", "pct_overall_w2", "pct_overall_w1"))


# Create the dodged bar chart with updated legend labels and percentage labels
ggplot(df_filtered, aes(x = experience, y = percentage, fill = pct_overall)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "Percentage of Overall Recommendation by Experience",
       x = "Experience",
       y = "Percentage",
       fill = "Recommendation") +
  scale_fill_manual(values = c("#1f77b4", "#2ca02c", "#d62728"),
                    labels = c("w3", "w2", "w1")) +
  geom_text(aes(label = paste0(round(percentage * 100), "%")), 
            position = position_dodge(width = 0.9), 
            vjust = -0.5, 
            size = 3) +
  theme_classic() +
  coord_flip()

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

One option would be to reverse the order of the legend using guide_legend(reverse=TRUE):

library(ggplot2)

ggplot(df_filtered, aes(x = percentage, y = experience, fill = pct_overall)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(
    title = "Percentage of Overall Recommendation by Experience",
    x = "Experience",
    y = "Percentage",
    fill = "Recommendation"
  ) +
  scale_fill_manual(
    values = c("#1f77b4", "#2ca02c", "#d62728"),
    labels = c("w3", "w2", "w1")
  ) +
  geom_text(aes(label = paste0(round(percentage * 100), "%")),
    position = position_dodge(width = 0.9),
    hjust = -0.5,
    size = 3
  ) +
  theme_classic() +
  guides(fill = guide_legend(reverse = TRUE))

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading