R 中的堆积条形图 (ggplot2),其中 y 轴和条形作为计数百分比

发布于 2024-09-17 05:37:55 字数 1679 浏览 1 评论 0原文

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

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

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

史蒂夫

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

在此处输入图像描述

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

enter image description here

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

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看春风乍起 2024-09-24 05:37:55

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

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

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')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文