-1

I would like to create a stacked bar chart in R. My X axis just contains data on sex i.e male or female. I just need the y axis to show percentages of the stacked bars. The "Survived" column is just a mixture of 0s and 1s. I.e 1 denoting that an indiividual survived an experience and 0 showing that the individual did not survive the experience. I am not sure what to put in for the y label. Can anyone help please?

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

Jed
  • 35
  • 6
  • There's several other posts you could check out, [here](https://stackoverflow.com/questions/40249943/adding-percentage-labels-to-a-bar-chart-in-ggplot2), [and here](https://stackoverflow.com/questions/3695497/show-instead-of-counts-in-charts-of-categorical-variables). If you want to get help on this site in the future, check out [this post on reproducible examples](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – astrofunkswag Mar 05 '20 at 17:16

1 Answers1

1

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:

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

enter image description here


Reproducible example

df <- data.frame(Sex = sample(c("M","F"),100, replace = TRUE),
                 Survive = sample(c(0,1), 100, replace = TRUE))
dc37
  • 15,840
  • 4
  • 15
  • 32