How to call reorder within aes_string of ggplot

前端 未结 2 2257
天命终不由人
天命终不由人 2021-02-20 14:41

I need to reorder a barplot from high to low (left to right) using ggplot & aes_string(). For e.g. for a dataframe df <- f(X,Y,Z) this can be done with

 g         


        
相关标签:
2条回答
  • 2021-02-20 15:37

    With the latest version of ggplot, you should be use aes with !! and sym() to turn your strings into symbols.

    ggplot(top10, 
      aes(
        x = reorder(!!sym(colnames(top10)[num1]), meanFeat),
        y = meanFeat, 
        fill=!!sym(colnames(top10)[num1]))) +  
      geom_bar(stat="identity")
    
    0 讨论(0)
  • 2021-02-20 15:43

    Since aes_string works with strings, use paste:

    ggplot(top10, aes_string(x=paste0("reorder(",colnames(top10)[num1],", -Y)"),y=meanFeat,
     fill=colnames(top10)[num1])) +  geom_bar(stat="identity")
    
    0 讨论(0)
提交回复
热议问题