Percentage / Probability per layer at graph

Hey my dear friends :slight_smile:
hope all of you are ok
i'm trying to do some exercise on titanic dataset. i graphed survived by gender of passengers,filled by survived or not. And drilled down / faceted by passenger ticket class to check relationship between these three categorical variables.
what i need is to put on each column two percentages : within class 1 , Percentage of males survived from total males is .....% and percentage of survived from total class 1 is ....%
is it doable my friends ???

ggplot(titanic[!$survived),],aes(x=sex, fill=survived))+geom_bar()+theme_bw()+facet_wrap(~pclass)+labs(y="Passengers' count",title="Titanic Survival Rates by class and Gender")

the secret is to use ...count...

You didn't give me enough to reproduce your data, but it's something like this:

ggplot(titanic[!$survived),],aes(x=sex, fill=survived))+
  geom_bar(aes(y = (..count..)/sum(..count..)))+
  labs(y="Passengers' count",title="Titanic Survival Rates by class and Gender")

@jdlong but is there s way to put the probability / percentage on the bar itself ?? i feel it will be more insightful when users see it visualized and quantified with numbers also :slight_smile:

i found the solution here

but , cannot apply because y is count at aes(x=sex, fill=survived) , How can i put y as a number of records of a certain variable ?

1 Like

Often, in situations like this, I do the "calculations" outside of ggplot (i.e. in the data frame itself) and then just use those figures in the ggplot (using the new calculated amount in the label argument of geom_text, and possibly setting x and y as well in the geom_text.