Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display frequency instead of count with geom_bar() in ggplot

Tags:

r

ggplot2

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?

like image 839
Remi.b Avatar asked Nov 07 '13 12:11

Remi.b


People also ask

What does Geom_col () Do How is it different to Geom_bar ()?

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.

What does Geom_col do in R?

geom_col makes the height of the bar from the values in dataset.

What does stat mean in Geom_bar?

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.

What is stat in Ggplot?

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.


2 Answers

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) 
like image 181
tonytonov Avatar answered Sep 19 '22 17:09

tonytonov


now ..prop.. is available

ggplot(data=tips, aes(x=day)) +    geom_bar(aes(y = ..prop.., group = 1)) 
like image 39
Dambo Avatar answered Sep 19 '22 17:09

Dambo