R 中的堆积条形图 (ggplot2),其中 y 轴和条形作为计数百分比
我是 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) )
# 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) )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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.