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?
How is it different to geom_bar() ? geom_bar() uses the stat_count() statistical transformation to draw the bar graph. geom_col() assumes the values have already been transformed to the appropriate values.
geom_col makes the height of the bar from the values in dataset.
In geom_bar() , the default dependent measure is the count (i.e., stat = "count" by default). In the above example, we've overridden the default count value by specifying stat = "identity" . This indicates that R should use the y-value given in the ggplot() function.
In <span class=\"code\">ggplot2</span>, you refer to this statistical summary as a <i>stat.</i></p>\n<p class=\"Tip\">One very convenient feature of <span class=\"code\">ggplot2</span> is its range of functions to summarize your data in the plot. This means that you often don't have to pre-summarize your data.
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)
now ..prop..
is available
ggplot(data=tips, aes(x=day)) + geom_bar(aes(y = ..prop.., group = 1))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With