0

all. I would like to use fct_reorder by two conditions (within groups) after a pivot_longer procedure. I did not find any tutorial on this. This question here is similar, however, it not deals with pivot_longer enter link description here

this is my current and desired plot plots

Code is here:

df1 %>% 
  select(-young_total) %>% 
  pivot_longer(-mood) %>% 
  ggplot(., aes(x=name, y=value, fill=mood)) + 
  theme_bw() +
  geom_bar(stat = "summary", position = "dodge") +
  facet_wrap(~mood) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

Simulate data is here:

df1 <- structure(list(mood = c("depression", "typical", "maniac", "typical", 
                               "typical", "depression", "typical", "typical", "typical", "typical", 
                               "depression", "depression", "depression", "maniac", "typical", 
                               "maniac", "typical", "typical", "typical", "depression", "depression", 
                               "typical", "maniac", "depression", "maniac", "depression", "depression", 
                               "depression", "typical", "maniac", "typical", "depression", "depression", 
                               "depression", "typical", "depression", "maniac", "typical", "depression", 
                               "maniac", "typical", "typical", "maniac", "typical", "typical", 
                               "typical", "typical", "depression", "depression", "depression"
), young_1 = c(0, 0, 1, 1, 0, 0, 0, 0, 0, 2, 0, 1, 1, 1, 0, 2, 
               0, 1, 0, 0, 0, 0, 2, 0, 3, 0, 0, 0, 1, 3, 0, 0, 0, 0, 0, 0, 2, 
               0, 0, 3, 1, 0, 3, 1, 0, 1, 1, 0, 0, 0), young_2 = c(0, 0, 2, 
                                                                   0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 3, 0, 1, 0, 0, 0, 0, 1, 0, 
                                                                   4, 0, 3, 0, 0, 3, 0, 0, 0, 0, 1, 0, 3, 0, 0, 2, 0, 0, 3, 0, 0, 
                                                                   0, 1, 0, 0, 0), young_3 = c(0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 
                                                                                               0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                                                                               0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 3, 0, 1, 0, 0, 0, 0, 0), young_4 = c(0, 
                                                                                                                                                                  0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 1, 2, 2, 3, 0, 0, 0, 1, 0, 0, 
                                                                                                                                                                  2, 0, 1, 2, 0, 2, 0, 2, 0, 0, 0, 0, 1, 0, 2, 0, 2, 2, 0, 0, 3, 
                                                                                                                                                                  0, 0, 0, 0, 2, 0, 0), young_5 = c(1, 0, 1, 1, 0, 0, 0, 1, 0, 
                                                                                                                                                                                                    0, 0, 2, 0, 2, 0, 2, 0, 0, 0, 1, 0, 0, 0, 1, 4, 2, 0, 2, 1, 3, 
                                                                                                                                                                                                    0, 0, 1, 2, 0, 2, 0, 1, 1, 1, 0, 0, 2, 0, 1, 0, 1, 1, 0, 0), 
young_6 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 
            2, 0, 1, 1, 0, 0, 0, 1, 1, 2, 0, 0, 0, 0, 3, 0, 0, 0, 1, 
            0, 0, 1, 0, 0, 2, 0, 0, 2, 0, 0, 0, 1, 0, 1, 0), young_7 = c(0, 
                                                                         0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 0, 1, 0, 0, 
                                                                         0, 0, 1, 2, 2, 0, 0, 2, 0, 4, 0, 0, 0, 1, 0, 0, 2, 0, 0, 
                                                                         2, 0, 0, 3, 0, 0, 0, 1, 0, 1, 0), young_8 = c(0, 0, 1, 0, 
                                                                                                                       0, 0, 0, 0, 0, 1, 0, 0, 0, 4, 0, 1, 0, 1, 0, 0, 0, 0, 1, 
                                                                                                                       0, 3, 0, 4, 0, 1, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 
                                                                                                                       3, 0, 0, 0, 0, 0, 0, 0), young_9 = c(0, 0, 1, 0, 0, 0, 0, 
                                                                                                                                                            0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 
                                                                                                                                                            0, 1, 0, 2, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 
                                                                                                                                                            0, 1, 0, 1, 0), young_10 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                                                                                                                                                                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                                                                                                                                                                         1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 2, 0, 0, 1, 0, 0, 0, 0, 0, 
                                                                                                                                                                                         0, 0), young_11 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                                                                                                                                                                                             1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 4, 0, 0, 
                                                                                                                                                                                                             0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0), young_total = c(2, 
                                                                                                                                                                                                                                                                                    0, 9, 3, 0, 1, 2, 2, 1, 9, 0, 7, 2, 18, 2, 24, 0, 7, 2, 3, 
                                                                                                                                                                                                                                                                                    0, 0, 13, 8, 30, 6, 11, 10, 5, 41, 0, 0, 3, 9, 2, 5, 13, 
                                                                                                                                                                                                                                                                                    2, 16, 26, 1, 0, 33, 1, 3, 1, 10, 4, 4, 0)), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                               -50L), class = c("tbl_df", "tbl", "data.frame"))
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Luis
  • 1,388
  • 10
  • 30
  • Does this answer your question? [How to order bars within all facets?](https://stackoverflow.com/questions/51710863/how-to-order-bars-within-all-facets) – bouncyball Feb 18 '21 at 16:28
  • 1
    also this question https://stackoverflow.com/questions/18624394/ggplot-bar-plot-with-facet-dependent-order-of-categories – bouncyball Feb 18 '21 at 16:29
  • not at all, they did not use pivot_longer, which makes things harder. I'm trying to implement though – Luis Feb 18 '21 at 17:27

1 Answers1

1

So this answer is based on similar logic from the answer from the post mentioned in the comments by @bouncyball

You create a dummy variable name2 that specifies the order within the groups you are faceting by (name and mood) and uses fct_reorder to set. This dummy variable can be mapped/cross-referenced back to the x-axis values by specifying the labels argument in scale_x_discrete and using scale = "free_x" in facet_wrap.

library(tidyverse)

df2 <- df1 %>% 
  select(-young_total) %>% 
  pivot_longer(-mood) %>% 
  mutate(name2 = paste(name, mood)) %>% 
  mutate(name2 = fct_reorder(name2, .x = value, .fun = mean))

df2 %>%
  ggplot(aes(x = name2, y = value, fill = mood)) +
  theme_bw() +
  geom_bar(stat = "summary", position = "dodge") +
  scale_x_discrete(labels =  deframe(df2 %>% select(name2, name))) +
  facet_wrap( ~ mood, scale = "free_x") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))

enter image description here

EJJ
  • 1,474
  • 10
  • 17