Graph with ordered bars and using facets

情到浓时终转凉″ 提交于 2019-12-02 00:49:21

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!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!