首页 文章

R(ggplot2) - 堆积条形图问题

提问于
浏览
0

我试图从数据框创建一个如下所示的图:

management depth id          genus rep1 rep2 rep3 rep4 sum
1         OM     A  1     alternaria    3    0    0    0   3
2         OM     A  2    aspergillus    0    0    0    0   0
3         OM     A  3     chaetomium    0    0    0    0   0
103       PM     A  1     alternaria    0    0    0    0   0
104       PM     A  2    aspergillus    4    1    4   35  44
105       PM     A  3     chaetomium    0    0    0    7   7

我想创建一个堆积条形图,管理在x轴上,填充属,y应代表总和 . 我用了

stack<-ggplot(df, aes(x=management, fill=genus)) + geom_bar(position="fill", color="white")

我的第一个问题是我不知道如何在这段代码中实现总和 . 我可以创建两个单独的图,但如果我可以将两个条都放入一个图中会更好 .

我还使用上面的数据框创建了一个堆栈条形图,但结果如下:
plot

因为我对一个属具有非常大的值,ggplot将始终创建一个双列堆积条 . 反正有没有解决这个问题?

1 回答

  • 0

    如果您提供了预期的结果,那么定制解决方案会更容易,但我相信这是您正在寻找的?

    library(ggplot2)
    df <- read.table(textConnection("
     row.id management depth id          genus rep1 rep2 rep3 rep4 sum
    1         OM     A  1     alternaria    3    0    0    0   3
    2         OM     A  2    aspergillus    0    0    0    0   0
    3         OM     A  3     chaetomium    0    0    0    0   0
    103       PM     A  1     alternaria    0    0    0    0   0
    104       PM     A  2    aspergillus    4    1    4   35  44
    105       PM     A  3     chaetomium    0    0    0    7   7"), header=TRUE, stringsAsFactors = FALSE) 
    
    ggplot(df, aes(x=management, y=sum, fill=genus)) + 
    geom_bar( color="white", stat="identity")
    

    enter image description here

相关问题