I\'m plotting a categorical variable and instead of showing the counts for each category value.
I\'m looking for a way to get ggplot
to display the perc
If you want percentages on the y-axis and labeled on the bars:
library(ggplot2)
library(scales)
ggplot(mtcars, aes(x = as.factor(am))) +
geom_bar(aes(y = (..count..)/sum(..count..))) +
geom_text(aes(y = ((..count..)/sum(..count..)), label = scales::percent((..count..)/sum(..count..))), stat = "count", vjust = -0.25) +
scale_y_continuous(labels = percent) +
labs(title = "Manual vs. Automatic Frequency", y = "Percent", x = "Automatic Transmission")
When adding the bar labels, you may wish to omit the y-axis for a cleaner chart, by adding to the end:
theme(
axis.text.y=element_blank(), axis.ticks=element_blank(),
axis.title.y=element_blank()
)