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
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,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