Search code examples
rggplot2graphreorderlist

Reordering variables using fct_relevel with ggplot in R


I am trying to use fct_relevel() to reorder variables in a graph. I have tried changing the column to a factor. I am unsure why my code isn't working. I need "Owned Panels" to show up in front of "Did Not Own Panels". I am also open to alternatives that don't rely on fct_relevel().

enter image description here

Graph code:

groups %>%
  mutate(panels = fct_relevel(panels), "Owned Panels", "Did Not Own Panels") %>%
ggplot(., aes(x=reason, y = mean, fill = panels)) + 
  geom_bar(stat = "identity", color = "black", position = position_dodge()) +
  geom_errorbar(aes(ymin = mean - se, ymax = mean+se), width = .2, position = position_dodge(.9)) +
  geom_text(aes(label = round(mean, digits =2)), position = position_dodge(width=1.0), vjust = -1.5) +
  #facet_wrap(~dv) +
  labs(title = ~ "Likelihood of solar panel installation after meeting ambassador", 
       y = "Likelihood of installing solar panels", 
       x = "Reason to install solar panels") + 
  scale_fill_discrete(name = "Ambassador solar\npanel ownership") + 
  scale_y_continuous(limits = c(1, 7), oob = scales::oob_squish) 

Data:

structure(list(dv = c("behavior", "behavior", "behavior", "behavior"
), panels = c("Owned Panels", "Owned Panels", "Did Not Own Panels", 
"Did Not Own Panels"), reason = c("Environment", "Money", "Environment", 
"Money"), mean = c(5.15789473684211, 5.36065573770492, 4.85454545454545, 
4.35483870967742), se = c(0.224988824122626, 0.194223670966034, 
0.187878787878788, 0.210884132880012)), row.names = c(NA, -4L
), groups = structure(list(dv = c("behavior", "behavior"), panels = c("Did Not Own Panels", 
"Owned Panels"), .rows = structure(list(3:4, 1:2), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

Solution

  • In the OP's code, the fct_relevel was closed before specifying the levels

    library(forcats)
    library(dplyr)
    library(ggplot2)
    groups %>%
        mutate(panels = fct_relevel(panels), "Owned Panels", "Did Not Own Panels")
                                          ^                                        
    

    Instead, it would be (also ungroup before doing this)

    groups %>% 
       ungroup %>% 
       mutate(panels =  fct_relevel(panels, "Owned Panels", "Did Not Own Panels") )    %>%
     ggplot(., aes(x=reason, y = mean, fill = panels)) + 
      geom_bar(stat = "identity", color = "black", position = position_dodge()) +
      geom_errorbar(aes(ymin = mean - se, ymax = mean+se), width = .2, position = position_dodge(.9)) +
      geom_text(aes(label = round(mean, digits =2)), 
          position = position_dodge(width=1.0), vjust = -1.5) +
      #facet_wrap(~dv) +
      labs(title = ~ "Likelihood of solar panel installation after meeting ambassador", 
           y = "Likelihood of installing solar panels", 
           x = "Reason to install solar panels") + 
      scale_fill_discrete(name = "Ambassador solar\npanel ownership") + 
      scale_y_continuous(limits = c(1, 7), oob = scales::oob_squish)
    

    enter image description here