在ggplot2的geom_bar上堆叠多列 [英] Stacking multiple columns on a geom_bar in ggplot2

查看:838
本文介绍了在ggplot2的geom_bar上堆叠多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上:


  1. 我想绘制一个条形图来显示两个表格列的聚合值,其中I已设法使用:
    err.bar< - ggplot(ss.data,aes(x = pop,y = obs + proc))
    err.bar< - err.bar + geom_bar(position =stack,stat =identity)
    err.bar


  2. 我想为阴影,不一定是颜色,聚合棒的两个部分。

  3. 最后,我想通过将它们分组来对它们进行着色根据物种(即,按照Excel图上x轴标签所示的物种E& C)


    我使用的数据类似于:


    • pop E1 E2 E3 E4 E5 E6 E7 C1 C2 C3 C4

    • i obs 0.0027 0.0018 0.0464 0.0095 0.0034 0.0117 0.017 0.1178 0.0449 0.039 0.0903
      li proc 0.0319 0.0196 0.0511 0.0143 0.0048 0.0078 0.0396 0.1662 0.074 0.1681 0.1358 b $ b / ul>

      解决方案

      这是一个解决方案,可以让您获得所需的大部分内容。但请注意,ggplot的设计不允许在单个绘图中分别设置阴影和颜色参数。相反,我使用灰色填充颜色为 obs 和 proc 类别添加了阴影,并且我已将这些物体分组为小面而不是以不同的方式着色)。

        library(ggplot2)
      library(reshape2)

      ss.data = data.frame(
      pop = c(E1,E2,E3,E4,E5,E6,E7,C1,
      obs = c(0.0027,0.0018,0.0464,0.0095,0.0034,0.0117,0.017,0.11178,
      0.0449,0.039,0.0903),
      proc = c(0.0319,0.0196,0.0511,0.0143,0.0048,0.0078,0.0396,0.16262,
      0.074,0.1681,0.1358),stringsAsFactors = FALSE)

      #添加新列'种类'通过删除'pop'中的尾数字。
      ss.data $ species = gsub(\\,,ss.data $ pop)

      #将数据转换为长形式reshape2包。
      mdat = melt(ss.data,id.vars = c(pop,species),
      measure.vars = c(obs,proc))

      plot_1 = ggplot(mdat,aes(x = pop,y = value,fill = variable))+
      theme_bw()+
      geom_bar(position =stack,stat =identity )+
      scale_fill_manual(values = c(grey50,grey80))+
      facet_grid(。〜species,space =free_x,scales =free_x,
      labeller = label_both)

      ggsave(plot_1.png,plot = plot_1,width = 6.5,height = 4)


      Essentially:

      1. I want to draw a bar-graph that shows the aggregated value of two table columns, which I have managed to do using: err.bar <- ggplot(ss.data, aes(x=pop, y=obs+proc)) err.bar <- err.bar + geom_bar(position="stack", stat = "identity") err.bar

      2. I want to shade, not necessarily color, the two parts of the aggregated bars.

      3. Finally I want to color the bars by grouping them according to species (i.e., by species E & C as indicated on the x-axis labels on the Excel graph)

      The data I am using is similar to:

      • pop E1 E2 E3 E4 E5 E6 E7 C1 C2 C3 C4
      • obs 0.0027 0.0018 0.0464 0.0095 0.0034 0.0117 0.017 0.1178 0.0449 0.039 0.0903
      • proc 0.0319 0.0196 0.0511 0.0143 0.0048 0.0078 0.0396 0.1662 0.074 0.1681 0.1358

      解决方案

      Here is a solution that gets you most of what you want. But please note that ggplot is not designed to allow separate 'shade' and 'color' parameters in a single plot. Instead, I have shaded your obs and proc categories using grey fill colors, and I have grouped the species into facets (instead of coloring them differently).

      library(ggplot2)
      library(reshape2)
      
      ss.data = data.frame(
          pop=c("E1", "E2", "E3", "E4", "E5", "E6", "E7", "C1", "C2", "C3", "C4"),
          obs=c(0.0027, 0.0018, 0.0464, 0.0095, 0.0034, 0.0117, 0.017, 0.1178,
                0.0449, 0.039, 0.0903),
          proc=c(0.0319, 0.0196, 0.0511, 0.0143, 0.0048, 0.0078, 0.0396, 0.1662,
                 0.074, 0.1681, 0.1358), stringsAsFactors=FALSE)
      
      # Add new column 'species' by removing the trailing digits from 'pop'.
      ss.data$species = gsub("\\d", "", ss.data$pop)
      
      # Convert data to long-form with 'melt' from the reshape2 package.
      mdat = melt(ss.data, id.vars=c("pop", "species"),
                  measure.vars=c("obs", "proc"))
      
      plot_1 = ggplot(mdat, aes(x=pop, y=value, fill=variable)) +
               theme_bw() +
               geom_bar(position="stack", stat="identity") +
               scale_fill_manual(values=c("grey50", "grey80")) +
               facet_grid(. ~ species, space="free_x", scales="free_x",
                   labeller=label_both)
      
      ggsave("plot_1.png", plot=plot_1, width=6.5, height=4)
      

      这篇关于在ggplot2的geom_bar上堆叠多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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