I am trying to make a graph with ordered bars according to frequency and also using a variable two separate two variables using facets. Words have to be ordered by value
The very relevant comment aside by GordonShumway (because the solution offered there is a little black magicky) - you can create a temporary value to reorder your facet - note I'm using forcats::fct_reorder
rather than reorder
library(tidyverse)
d %>%
mutate(temp = paste0(word, u_c)) %>%
ggplot(aes(x = fct_reorder(temp, n), y = n, fill = u_c)) +
geom_col(show.legend = F) +
facet_wrap(~u_c, scales = "free_y") +
coord_flip()
Given the info provided by @GordonShumway and following the answer given by @CPak, bellow I provide the entire and "tricky" and no-elegant way to fix this issue. The almost entire answer (by @CPak) and one more line finally fix the problem:
d %>%
mutate(temp = paste(word, u_c, sep = "_")) %>%
ggplot(aes(x = fct_reorder(temp, n), y = n, fill = u_c)) +
geom_col(show.legend = F) +
scale_x_discrete(labels = function(x) str_replace(x,"_candidate|_user", "")) +
facet_wrap(~u_c, scales = "free_y") +
coord_flip()
Thanks for the answers!