reordering geom_bar when using facet_wrap

后端 未结 1 466
难免孤独
难免孤独 2021-01-04 20:15

What I try to achieve is having the bars ordered by a given variable per panel.

A simple example:

    library(ggplot2)
    library(plyr)

    df <         


        
相关标签:
1条回答
  • 2021-01-04 20:29

    I think that grid.arrange is a much better tool for this than trying to shoehorn free scales into a faceting framework:

    library(gridExtra)
    q1 <- ggplot(subset(df,group == 1),aes(x = reorder(fac,val),y = val)) + 
            geom_bar() + 
            facet_wrap(~group) + 
            coord_flip()
    
    q2 <- q1 %+% subset(df,group == 2)
    
    grid.arrange(q1,q2,nrow = 1)
    

    enter image description here

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