Jacas
Jacas

Reputation: 371

How to show number of occurrences on Y axis in ggplot2

I'm trying to make a grouped bar chart in R using ggplot2. Code I'm using for that is:

ggplot(loanData, aes(factor(loanData$saving_status), <Y AXIS ATTRIBUTE>, fill = loanData$class)) + geom_bar(stat="identity", position = "dodge") + scale_fill_brewer(palette = "Set1")

However, I don't know what should I replace "Y AXIS ATTRIBUTE" with in order to indicate number of occurrences for values of saving_status and class attributes on the chart. I guess what I need is to count those occurrences using some method and put the result there?

Basically what I'm looking for is something like answer to this question but I don't have value in my table I could use in the same way as "species" is used in that example. Thank you

Upvotes: 4

Views: 11673

Answers (1)

Bhavesh Ghodasara
Bhavesh Ghodasara

Reputation: 2071

You don't need to input y for count of x. just change value of stat to count/bin.

try following:

ggplot(loanData, aes(factor(loanData$saving_status),fill = loanData$class)) +
geom_bar(stat="count", position = "dodge") + 
scale_fill_brewer(palette = "Set1")

please provide sample data to help you better.

Upvotes: 9

Related Questions