ggplot: showing % instead of counts in charts of categorical variables
require(ggplot2)
## Loading required package: ggplot2
require(scales)
## Loading required package: scales
require(knitr)
p <- ggplot(mtcars, aes(x = hp)) +
geom_bar(aes(y = (..count..)/sum(..count..))
, binwidth = 25)
p
p <- ggplot(mtcars, aes(x = hp)) +
geom_bar(aes(y = (..count..)/sum(..count..))
, binwidth = 25
, fill = 'white'
, colour = 'black') +
scale_y_continuous(labels = percent_format())
p
p <- ggplot(mtcars, aes(x = as.factor(cyl)
, fill = as.factor(cyl))) +
geom_bar(aes(y = (..count..)/sum(..count..))
, colour = 'black') +
scale_y_continuous(labels = percent_format())
p
p <- ggplot(mtcars, aes(x = as.factor(cyl), fill = as.factor(cyl))) +
geom_bar(aes(y = (..count..)/sum(..count..))
, colour = 'black') +
scale_y_continuous(labels = percent_format()) +
scale_fill_grey() +
theme_bw()
p