Graph with ordered bars and using facets

前端 未结 2 1056
盖世英雄少女心
盖世英雄少女心 2021-01-22 23:28

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

相关标签:
2条回答
  • 2021-01-22 23:54

    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()
    
    0 讨论(0)
  • 2021-01-23 00:04

    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!

    0 讨论(0)
提交回复
热议问题