I want to add bar values on top of each stacked bar in the bar plot with geom_text() of titanic dataset

plot

#1

hello friends, I want to add bar values on top of each stacked bar in the bar plot with geom_text() of titanic dataset…
My x variable is ‘Sex’ and have filled it with the variable ‘Survived’
here is my code:
ggplot(titanic) + aes(x= Sex, fill = Survived) + geom_bar()

output:
Rplot


#2

Try this

ggplot(data, aes(fill=factor(Survived), x=Sex)) + geom_bar()+
geom_text(aes(label=…count…),stat=“count”,position=position_stack(0.5))


#3

Hello jprakash…thanks for your help…just wanted to know why do we write stat=“count” inside geom_text ().along with label=…count…we don’t write stat=“identity” inside geom_text () we write it inside geom_bar () …then why do in this case
Amod Shirke


#4

stat means “statistical transformation to use on the data for this layer”

count -> count of X variable


#5

Yes thanks again.
But why don’t we write stat=“identity” inside geom_text () just like this case…


#6

I mean when we have a y variable we dont have to write stat=“identity” inside geom_text ()…we just write geom_text ( aes (label=…)


#7

stat=“identity” means “display as it is without any summarization” that’s why we need to specify Y variable with “identity”.

Here, we want to specify part of total count and we are using …count… for label and stat as “count”