在R(ggplot2)中堆积条形图,y轴和条形以计数百分比表示 [英] Stacked bar chart in R (ggplot2) with y axis and bars as percentage of counts

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

问题描述

我是ggplot2的新手,并且有关于生成堆叠条形图的问题。我查了这本书和专门的网页,但无法解决问题。我有两个因素,其中一个有两个等级(存在不存在),其他10个等级。让我们称这两个可变和水果。



我想创建一个堆积条形图,其中每个条形图反映了一种水果,变量中出现 - 缺少观察值的数量堆叠在彼此。这是相对容易的(参见下面图1的代码),但是我还希望bar和y轴以百分比的形式表示变量中存在与否的计数数量。换句话说,所有酒吧应该是相同的高度(总共反映100%),并且存在 - 缺席观察的计数应该被转换成百分比。



我可以使用..count .. * 100 / sum(.. count ..)将y轴比例更改为百分比,但我无法理解如何转换实际的酒吧。我创建了另一个面(方块2的代码),用百分比的方式实现我想要的,但我更喜欢两个条相互重叠。有没有人有如何实现这个想法?我提供了虚拟数据和可重复的示例。谢谢你的帮助。

Steve

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

#堆积条图
ggplot(dat,aes(x = fruit,fill = variable))+
geom_bar(aes(y = ..count .. * 100 / sum(.. count)))+
xlab(水果)+
ylab(想这是百分比)+
scale_fill_manual(Condition,values = alpha(c(firebrick,dodgerblue4),1))

 #with faceting 
ggplot(dat,aes(x = variable,fill =变量))+
geom_ bar(aes(y = ..count .. * 100 / sum(.. count ..)))+
facet_grid(。 〜水果)+
xlab(水果)+
ylab(希望这是百分比)+
scale_fill_manual(Condition,values = alpha(c(firebrick ,dodgerblue4),1))

解决方案

对于第一个图,只需将position ='fill'添加到您的geom_bar行!您实际上不需要扩大计数,因为ggplot可以自动执行。

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


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".

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.

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.

Steve

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) )  

解决方案

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天全站免登陆