Reputation: 321
I have a dataset which looks like this:
edu default
1 1 0
2 3 1
3 1 1
4 1 0
5 2 1
6 2 0
...
and I could make a plot using R :
ggplot(rawdata, aes(x = edu, fill = default)) +
geom_bar() +
labs(x = 'Education') +
theme_excel()
Instead of counts of 1s and 0s in default, I want to plot the proportion of 1s like this:
I calculated the proportion separately, store the results in another data frame and made this plot.
My question is: Is there a compact way that I could do this in a single ggplot()
command like I did in the previous plot?
Update:
I forgot to mention that the data type of default
is factor. So applying mean
does not work.
Upvotes: 2
Views: 1659
Reputation: 35262
We recall that the proportion of 1's in a binary vector is simply its mean. The way to plot mean values per x
in ggplot
is using the stat_summary
function. So we get:
ggplot(rawdata, aes(x = edu, y = default)) +
stat_summary(fun.y = 'mean', geom = 'bar')
Or:
ggplot(rawdata, aes(x = edu, y = default)) +
geom_bar(stat = 'summary') #include fun.y = 'mean' to avoid the message
Both give:
Upvotes: 2