R (ggplot2) 中的堆叠条形图,y 轴和条形作为计数的百分比 [英] Stacked bar chart in R (ggplot2) with y axis and bars as percentage of counts

查看:30
本文介绍了R (ggplot2) 中的堆叠条形图,y 轴和条形作为计数的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ggplot2 的新手,有一个关于生成堆积条形图的问题.我查看了这本书和专用网页,但无法解决问题.我有两个因素,其中一个有 2 个级别(存在 - 不存在),其他 10 个级别.让我们称这两个为变量"和水果".

I'm a novice with ggplot2 and have a question about generating a stacked bar plot. I checked the book and the dedicated webpage, but can't solve the problem. I have two factors, one of which has 2 levels (presence-absence), the other 10 levels. Lets call these two "variable" and "fruit".

我想创建一个堆叠条形图,其中每个条形反映一种水果,并且变量"中存在与不存在观察值的数量相互堆叠.这相对容易(请参阅下面图 1 的代码),但我还希望条形和 y 轴将变量"中存在-不存在的计数表示为百分比.换句话说,所有条形都应具有相同的高度(总共反映 100%),并且存在与不存在观察的计数应转换为百分比.

I'd like to create a stacked bar plot where each bar reflects a type of fruit and the number of presence-absence observations in "variable" are stacked on top of each other. This is relatively easy (see code for plot 1 below), but I would also like the bars and y axis to express the number of counts of presence-absence in "variable" as a percentage. In other words, all the bars should be the same height (reflecting a total of 100%) and the counts of presence-absence observations should be converted into percentages.

我可以使用 ..count..*100/sum(..count..) 将 y 轴比例更改为百分比,但我无法理解如何转换实际条形.我创建了另一个带有分面的图(下面图 2 的代码),它在百分比方面达到了我想要的效果,但我更喜欢两个条形图.有没有人知道如何实现这一目标?我提供了虚拟数据和可重复的示例.谢谢你的帮助.

I can change the y axis scale to a percentage using ..count..*100/sum(..count..) but I can't fathom how to convert the actual bars. I created another plot with faceting (code for plot 2 below) that achieves what I want in terms of percentages, but I would prefer the two bars on top of each other. Does anyone have an idea of how to achieve this? I've provided dummy data and reproducible example. Thanks for any help.

史蒂夫

dat <- data.frame( fruit=c("Apple", "Apple", "Orange", "Orange", "Orange", "Orange",
                   "Orange", "Pear", "Pear", "Pear"), variable=c("Present", "Absent",
                   "Present", "Present", "Present", "Present", "Absent", "Absent",
                   "Absent", "Present") )  

# stacked bar plot  
ggplot(dat, aes(x = fruit, fill = variable) ) +  
    geom_bar( aes(y = ..count..*100/sum(..count..) ) ) +
    xlab("Fruit") +
    ylab("Would like this to be percentage") + 
    scale_fill_manual("Condition", values = alpha( c("firebrick", "dodgerblue4"), 1) )  

# with faceting  
ggplot(dat, aes(x = variable, fill = variable) ) +   
    geom_bar( aes(y = ..count..*100/sum(..count..) ) ) +   
    facet_grid(. ~ fruit) +  
    xlab("Fruit") +
    ylab("Would like this to be percentage") + 
    scale_fill_manual("Condition", values = alpha( c("firebrick", "dodgerblue4"), 1) )  

推荐答案

对于第一个图形,只需将 position = 'fill' 添加到 geom_bar 线!您实际上并不需要缩放计数,因为 ggplot 有一种方法可以自动执行此操作.

For the first graph, just add position = 'fill' to your geom_bar line !. You don't actually need to scale the counts as ggplot has a way to do it automatically.

ggplot(dat, aes(x = fruit)) + geom_bar(aes(fill = variable), position = 'fill')

这篇关于R (ggplot2) 中的堆叠条形图,y 轴和条形作为计数的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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