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!
library(ggplot2) # loads ggplot2
data <- mtcars
ggplot(mtcars, aes(x=cyl, fill = factor(am))) +
geom_bar(position = "fill") +
stat_count(geom = "text",
aes(label = paste(round((..count..)/sum(..count..)*100), "%")),
position=position_fill(vjust=0.5), colour="white")
Your code was almost there, just needed _fill
instead of _stack
in position.
I know it's late but posting this because I often look for this too... you needed:
# Get labels
percentData <- mtcars %>% group_by(cyl) %>% count(am) %>%
mutate(ratio=scales::percent(n/sum(n)))
to calculate the proportions within each bar and then to plot:
# Plot
ggplot(mtcars,aes(x=factor(cyl),fill=factor(am)))+
geom_bar(position="fill")+
geom_text(data=percentData, aes(y=n,label=ratio),
position=position_fill(vjust=0.5))
ggplot(mtcars, aes(x=factor(cyl), fill = factor(am))) +
geom_bar(position = "fill")
Just to add to RLave response:
You can change the axis to show numbers as percentage instead of proportions using the scales package:
library(scales) # <- includes scales library
library(ggplot2)
data <- mtcars
ggplot(mtcars, aes(x=cyl, fill = factor(am))) +
geom_bar(position = "fill") +
stat_count(geom = "text",
aes(label = paste(round((..count..)/sum(..count..)*100), "%")),
position=position_fill(vjust=0.5), colour="white") +
scale_y_continuous(labels = percent) # <- set the desired formatting
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