Background: I am new to R and ggplot2 and hope to get a simple answer that benefits others in the same situation.
Question: What is a simple way in ggplot2 to make each bar add up to 100% and displaying percentage labels inside each bar?
Goal: What I want it to look like (each column totals 100%, with percentage labels inside each column).
Current Chart: This is the bar chart I have so far; based on the "mtcars" dataset (What it currently looks like). Below I have for the "Current Chart":
library(ggplot2) # loads ggplot2
data <- mtcars # gets the mtcars dataset
#INFO: A simple barchart with X-axis being Nr of Cylinders, fill color indicating whether or not the car model has an Automatic gearbox..
# ..and Y-axis showing the count of cars by Nr of Cylinders
ggplot(mtcars, aes(x=cyl, fill = factor(am))) +
geom_bar() +
stat_count(geom = "text",
aes(label = paste(round((..count..)/sum(..count..)*100), "%")), # This row calculates the percentage across all data, adding up to 100%, but I want it to add up to 100% per X-variable (Nr of Cylinders = 4, 6, 8)
position=position_stack(vjust=0.5), colour="white") # This row positions the bar percentage level and colors it white.
Many thanks in advance!