Use forcat::fct_reorder to sort plots within facet_wrap

后端 未结 1 2006
情书的邮戳
情书的邮戳 2021-01-13 14:28

I have data with Country level stats over time. I use facet_wrap() to plot by Country but I want to order the plots based on only the most recent value (2015)

相关标签:
1条回答
  • 2021-01-13 15:10

    Here you go!

    gather2 <- df1 %>%
        mutate(Country=fct_reorder(Country, `2015`)) %>%
        gather("Year", myValue, 2:4)
    
    gg2 <- ggplot() +
      geom_line(data = gather2, aes(x = Year, y = myValue, group = Country), size = 1.5, color = "orange") +
      facet_wrap(~ Country, ncol = 3) +
      theme(aspect.ratio = (35/50))
    gg2
    

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