On this page, they give the following example
library(ggplot2)
library(reshape2)
ggplot(data=tips, aes(x=day)) + geom_bar(stat="bin")
Instead of a count I'd like to have a frequency in y-axis. How can I achieve this?
On this page, they give the following example
library(ggplot2)
library(reshape2)
ggplot(data=tips, aes(x=day)) + geom_bar(stat="bin")
Instead of a count I'd like to have a frequency in y-axis. How can I achieve this?
Here's the solution which can be found in related question:
pp <- ggplot(data=tips, aes(x=day)) +
geom_bar(aes(y = (..count..)/sum(..count..)))
If you would like to label frequencies as percentage, add this (see here):
library(scales)
pp + scale_y_continuous(labels = percent)
geom_bar
generated the ..density..
variable like its cousin function geom_histogram
does. –
Dichlamydeous labels=percent
in scale_y_continuous(labels = percent)
doesnt work anymore –
Lilylilyan ggplot2_3.1.0
, scales_1.0.0
is still fine (R 3.5.1, Win 10). –
Endoblast now ..prop..
is available
ggplot(data=tips, aes(x=day)) +
geom_bar(aes(y = ..prop.., group = 1))
..count..
option does not! –
Shudder facet_wrap(~sex)
? It works with both for me. –
Atavism ..count../sum(..count..)
, but the frequency will sum to 1 over all of the facets (I think). If you use ..prop..
, the frequency will sum to 1 in each facet. Maybe it depends on what you're trying to show. –
Shudder ..count../sum(..count..)
and ..prop
work exactly the same for me as long as you set group = 1
–
Atavism ggplot(data=tips, aes(x=day)) + geom_bar(aes(y = ..prop.., group = 1)) + facet_wrap(~sex)
and ggplot(data=tips, aes(x=day)) + geom_bar(aes(y = (..count..)/sum(..count..), group = 1)) + facet_wrap(~sex)
do not yield the same plot for me. You can also remove group=1
in the ..count..
version and it looks the same. –
Shudder With ggplot2 >= 3.4.0, I think the recommended way is the following:
library("ggplot2")
library("reshape2")
ggplot(data = tips, aes(x = day)) +
geom_bar(aes(y = after_stat(prop), group = 1))
© 2022 - 2024 — McMap. All rights reserved.
..count..
? Do you have any reference to that? – Individualize