简体   繁体   English

如何在 R 中创建堆积条形图,其中 y 轴应表示条形的百分比?

[英]How do I create a stacked bar chart in R, where the y axis should denote the percentages for the bars?

I would like to create a stacked bar chart in R. My X axis just contains data on sex ie male or female.我想在 R 中创建一个堆积条形图。我的 X 轴只包含性别数据,即男性或女性。 I just need the y axis to show percentages of the stacked bars.我只需要 y 轴来显示堆叠条的百分比。 The "Survived" column is just a mixture of 0s and 1s. “Survived”列只是 0 和 1 的混合。 Ie 1 denoting that an indiividual survived an experience and 0 showing that the individual did not survive the experience.即 1 表示个人在经历中幸存下来,而 0 表示个人没有在经历中幸存下来。 I am not sure what to put in for the y label.我不确定要为 y 标签添加什么。 Can anyone help please?有人可以帮忙吗?

ggplot(data = df, mapping = aes(x = Sex, y = ? , fill = Survived)) + geom_bar(stat = "identity") ggplot(data = df, mapping = aes(x = Sex, y = ? , fill = Survived)) + geom_bar(stat = "identity")

One possible solution is to use dplyr package to calculate percentage of each categories outside of ggplot2 and then use those values to get your bargraph using geom_col :一种可能的解决方案是使用dplyr包计算ggplot2之外每个类别的ggplot2 ,然后使用这些值使用geom_col获取条形图:

library(dplyr)
df %>% count(Sex, Survive) %>%
  group_by(Sex) %>%
  mutate(Percent = n/sum(n)*100) 

# A tibble: 4 x 4
# Groups:   Sex [2]
  Sex   Survive     n Percent
  <fct>   <dbl> <int>   <dbl>
1 F           0    26    55.3
2 F           1    21    44.7
3 M           0    34    64.2
4 M           1    19    35.8

And now with the plotting part:现在是绘图部分:

library(dplyr)
library(ggplot2)

df %>% count(Sex, Survive) %>%
  group_by(Sex) %>%
  mutate(Percent = n/sum(n)*100) %>%
  ggplot(aes(x = Sex, y = Percent, fill = as.factor(Survive)))+
  geom_col()

在此处输入图片说明


Reproducible example可重现的例子

df <- data.frame(Sex = sample(c("M","F"),100, replace = TRUE),
                 Survive = sample(c(0,1), 100, replace = TRUE))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建频率堆积条形图,但是在 R 中的条形图和 y 轴上的频率有百分比标签? - How do I create a frequency stacked bar chart however have percentage labels on the bars and frequencies on the y axis, in R? 给定百分比,如何绘制堆积条形图? - How do I plot a stacked bar chart, given percentages? 如何在R中创建金字塔条形图,在条之间具有y轴标签 - How To Create Pyramid Bar Chart in R with y-axis labels between the bars R (ggplot2) 中的堆积条形图,y 轴和条形作为计数的百分比 - Stacked bar chart in R (ggplot2) with y axis and bars as percentage of counts 如何在R中使用百分比作为y轴而不是计数的数据框中创建条形图? - How do you create a bar graph for a data frame in R that uses percentages as the y-axis instead of a count? 如何创建一个条形图,其中 y 轴测量变量的平均值? - How do I create a bar chart where y axis measures mean of a variable? 如何创建条形图,使条形图相互堆叠(重叠)? - How can I create a bar chart where the bars are stacked in front of (overlaying) one another? ggplot 堆叠条形图:如何在 geom_text(aes(label = x) 上显示百分比而不使条形消失? - ggplot stacked bar chart: how can I display percentages on geom_text(aes(label = x) without making bars disappear? R:ggplot堆叠的条形图,y轴上有计数,但百分比为标签 - R: ggplot stacked bar chart with counts on y axis but percentage as label Plotly 堆叠水平条形图在 R 中的百分比 - Plotly stacked horizontal bar chart as percentages in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM