How to order bars within all facets?

前端 未结 1 1088
执笔经年
执笔经年 2020-12-03 23:46

This question is following my (now deleted) question.

This is my data :

i <- data.frame(
nbr =c(15.18 ,11.53 ,13.37 ,9.2, 10.9, 12.23 ,9.53, 9.81,         


        
相关标签:
1条回答
  • 2020-12-04 00:17

    There is a discussion regarding this issue here, which they proposed the below two functions as a solution to this problem which you can find here.

    scale_x_reordered <- function(..., sep = "___") {
      reg <- paste0(sep, ".+$")
      ggplot2::scale_x_discrete(labels = function(x) gsub(reg, "", x), ...)
    }
    
    reorder_within <- function(x, by, within, fun = mean, sep = "___", ...) {
      new_x <- paste(x, within, sep = sep)
      stats::reorder(new_x, by, FUN = fun)
    }
    
    
    ggplot(ii, aes(reorder_within(sn, nbr, s), nbr)) +
         geom_bar(stat = 'identity') +
         scale_x_reordered() +
         facet_wrap(.~ s, ncol=2,scales = "free_x") + 
        theme(axis.text.x=element_text(angle=90,hjust=1,vjust=.5,colour='gray50'))
    

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