在水平堆叠barplot(R)中对堆叠进行重新排序 [英] Reorder stacks in horizontal stacked barplot (R)

查看:1044
本文介绍了在水平堆叠barplot(R)中对堆叠进行重新排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用ggplot制作水平堆叠的barplot。以下是我的数据框中300个网站中三个网站的实际值。这里是我到目前为止,使用从这些


  1. 我会喜欢做的是重新排列酒吧,使其按照农业类别的价值降序排列 - 在此示例中,AR002将位于顶部,然后是AR003,然后是AR001。我尝试将aes的内容更改为 aes(x = reorder(landuse.ord,percent)),但是这样可以消除堆叠,并且似乎可以总结每个土地的百分比使用类别:



< ol start =2>
  • 我想从左到右依次堆叠农业,森林,湿地,水,我试着用变换代码的一部分,这些代码在图例中以正确的顺序排列,但不是在图表本身中?

    先谢谢了......我根据对其他人的问题的答案取得了很大的进步,但现在似乎停留在这一点!



    更新:这是所有326个网站的完成图表!

    解决方案

    好的根据您的意见,我相信这是您的解决方案。将这些行放在cols< -...之后:

      #create df按aggiculture的百分比排序
    ag < - 过滤器(df,landuse ==agriculture)
    #使用df对df $ id的级别进行排序和排序
    df $ id< -factor(df $ id,levels = ag $ id [order ag $ percent)],ordered = TRUE)
    #sort df,基于有序的ID和有序的土地用途
    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)

    评论应该阐明每一行的目的。这将重新排序您的原始数据帧,如果这是一个问题,我会创建一个副本,然后对新副本进行操作。


    I'm trying to make a horizontal stacked barplot using ggplot. Below are the actual values for three out of 300 sites in my data frame. Here's where I've gotten to so far, using info pulled from these previous questions which I admit I may not have fully understood.

    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) 
    

    which produces this graph.

    1. What I would like to do is to reorder the bars so that they are in descending order by value for the agriculture category - in this example AR002 would be at the top, followed by AR003 then AR001. I tried changing the contents of aes to aes(x = reorder(landuse.ord, percent), but that eliminated the stacking and seemed to have maybe summed the percentages for each land use category:

    1. I would like to have the stacks in order, from left to right: agriculture, forest, wetlands, water, developed. I tried doing that with the transform part of the code, which put it in the correct order in the legend, but not in the plot itself?

    Thanks in advance... I have made a ton of progress based on answers to other peoples' questions, but seem to now be stuck at this point!

    Update: here is the finished graph for all 326 sites!

    解决方案

    Ok based on your comments, I believe this is your solution. Place these lines after 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) 
    

    The comments should clarify each of the lines purposes. This will reorder your original data frame, if that is a problem I would create a copy and then operate on the new copy.

    这篇关于在水平堆叠barplot(R)中对堆叠进行重新排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆