Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 Stacked Bar Chart - Each Bar being 100% and with percenage labels inside each bar

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!

like image 729
Daniel Avatar asked Jul 12 '18 12:07

Daniel


4 Answers

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.

enter image description here

like image 131
RLave Avatar answered Oct 22 '22 06:10

RLave


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))
like image 5
Shamee Avatar answered Oct 22 '22 08:10

Shamee


ggplot(mtcars, aes(x=factor(cyl), fill = factor(am))) +
  geom_bar(position = "fill")
like image 4
Lennyy Avatar answered Oct 22 '22 07:10

Lennyy


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


like image 1
Felipe Gajardo Avatar answered Oct 22 '22 07:10

Felipe Gajardo