【问题标题】:Sort stacked barplot in decreasing order in R [duplicate]在R中按降序对堆叠的条形图进行排序[重复]
【发布时间】:2019-02-05 13:04:19
【问题描述】:

我正在创建一个 2x2 表的堆叠条形图:

fuel.type <- c('diesel', 'gas', 'diesel', 'gas', 'diesel', 'gas', 'gas', 'gas', 'gas')   
make <- c('bmw', 'wv', 'audi', 'bmw', 'audi', 'audi', 'wv', 'wv', 'wv')    
table1 <- table(fuel.type, make)  
barplot(table1, legend = row.names(table1))

我得到下图:

我怎样才能得到相同的情节,但按降序排序??

【问题讨论】:

    标签: r


    【解决方案1】:

    您可以通过这种方式对数据进行排序,然后绘制它们:

    # here you define a table (your) that is sorted by the total of the columns
    table1 <- table1[,order(colSums(-table1))] 
    barplot(table1, legend = row.names(table1))
    

    【讨论】:

      猜你喜欢
      • 2021-10-10
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-18
      • 2020-06-04
      • 2016-08-13
      相关资源
      最近更新 更多