【问题标题】:Reorder stacks in horizontal stacked barplot (R)在水平堆积条形图中重新排序堆栈 (R)
【发布时间】:2016-04-05 23:32:49
【问题描述】:

我正在尝试使用 ggplot 制作水平堆叠条形图。以下是我的数据框中 300 个站点中三个站点的实际值。到目前为止,我使用从这些 previous questions 中提取的信息,我承认我可能还没有完全理解。

df <- data.frame(id=c("AR001","AR001","AR001","AR001","AR002","AR002","AR002","AR003","AR003","AR003","AR003","AR003"), 
             landuse=c("agriculture","developed","forest","water","agriculture","developed","forest","agriculture","developed","forest","water","wetlands"), 
             percent=c(38.77,1.76,59.43,0.03,69.95,0.42,29.63,65.4,3.73,15.92,1.35,13.61))
df

      id     landuse percent 
1  AR001 agriculture   38.77 
2  AR001   developed    1.76 
3  AR001      forest   59.43 
4  AR001       water    0.03 
5  AR002 agriculture   69.95 
6  AR002   developed    0.42 
7  AR002      forest   29.63 
8  AR003 agriculture   65.40 
9  AR003   developed    3.73 
10 AR003      forest   15.92 
11 AR003       water    1.35 
12 AR003    wetlands   13.61 

str(df)

'data.frame':   12 obs. of  3 variables:
 $ id     : Factor w/ 3 levels "AR001","AR002",..: 1 1 1 1 2 2 2 3 3 3 ...
 $ landuse: Factor w/ 5 levels "agriculture",..: 1 2 3 4 1 2 3 1 2 3 ...
 $ percent: num  38.77 1.76 59.43 0.03 69.95 ...

df <- transform(df, 
                landuse.ord  = factor(
                  landuse,
                  levels=c("agriculture","forest","wetlands","water","developed"),
                  ordered =TRUE))

cols <- c(agriculture="maroon",forest="forestgreen", 
      wetlands="gold", water="dodgerblue", developed="darkorchid") 

ggplot(df,aes(x = id, y = percent, fill = landuse.ord, order=landuse.ord)) + 
    geom_bar(position = "stack",stat = "identity", width=1) + 
    coord_flip() +
    scale_fill_manual(values = cols) 

生成此图。

  1. 我想做的是对条形重新排序,以便它们按农业类别的值降序排列 - 在本例中,AR002 位于顶部,然后是 AR003,然后是 AR001。我尝试将 aes 的内容更改为 aes(x = reorder(landuse.ord, percent),但这消除了堆叠,并且似乎可能将每个土地利用类别的百分比相加:

  1. 我想按顺序排列,从左到右:农业、森林、湿地、水、开发。我尝试使用代码的transform 部分来做到这一点,它在图例中以正确的顺序排列,但不是在情节本身?

在此先感谢...根据对其他人问题的回答,我已经取得了很大的进步,但现在似乎卡在了这一点上!

更新:这是所有 326 个网站的最终图表!

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    好的,根据您的 cmets,我相信这是您的解决方案。将这些行放在 cols 之后

      #create df to sort by argiculture's percentage
    ag<-filter(df, landuse=="agriculture")
      #use the df to sort and order df$id's levels
    df$id<-factor(df$id, levels=ag$id[order(ag$percent)], ordered = TRUE)
      #sort df, based on ordered ids and ordered landuse
    df<-df[order(df$id, df$landuse.ord),]
    
    ggplot(df,aes(x = id, y = percent, fill = landuse.ord, order=landuse.ord)) + 
        geom_bar(position = "stack",stat = "identity", width=1) + 
        coord_flip() +
        scale_fill_manual(values = cols) 
    

    cmets 应阐明每条线路的用途。这将重新排序您的原始数据框,如果这是一个问题,我会创建一个副本,然后对新副本进行操作。

    【讨论】:

    • 正确!这完全奏效了。所以现在当我做str(df) 时,我可以看到 id 按农业百分比排序:$ id : Ord.factor w/ 3 levels "AR001"&lt;"AR003"&lt;..: 1 1 1 1 2 2 2 2 2 3 ... 我将在我原来的问题中发布新图表。很兴奋!非常感谢戴夫。
    猜你喜欢
    • 2020-04-10
    • 2014-07-07
    • 2012-03-02
    • 1970-01-01
    • 2013-05-15
    • 2018-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多