在水平堆叠条形图中重新排序堆栈 (R)

标签 r ggplot2

我正在尝试使用 ggplot 制作水平堆叠条形图。以下是我的数据框中 300 个站点中的 3 个站点的实际值。到目前为止,我使用从这些 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) 

生成此图表。

initial ggplot result

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

second ggplot result

  • 我希望堆栈按从左到右的顺序排列:农业、森林、湿地、水、已开发。我尝试用 transform 来做到这一点代码的一部分,它在图例中以正确的顺序放置它,但在绘图本身中却没有?
  • 预先感谢...根据其他人问题的回答,我已经取得了很多进展,但现在似乎陷入了困境!

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

    最佳答案

    好的,根据您的评论,我相信这就是您的解决方案。将这些行放在 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) 
    

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

    关于在水平堆叠条形图中重新排序堆栈 (R),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36438883/

    相关文章:

    r - 基于 R 中的另一个向量创建一个向量?

    r - 绘制按 R 中的唯一 ID 分组的每日时间序列汇总图

    r - 使用最小值和最大值的置信区间

    r - `fill` 比例尺未显示在图例中

    r - 插入符号神经网络错误 : "missing values in resampled performance measures"

    r - 如何在 Travis CI 上为 R 包运行 covr::codecov()

    r - 将手动添加的行包含到 ggplot2 指南图例中

    r - ggplot boxplot - 对数轴的 mustache 长度

    r - 将两个数据帧除以 R 中的公共(public)行

    r - 在 ggplot2 for R 中,如何反转条形颜色的顺序?