Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot - Proportional stacked area plot

Tags:

r

ggplot2

I don't understand why I'm not getting my proportional stacked area graph to work. When I use the following code, I get this weird skewed visual:

ViolentCrimes <- ddply(ViolentCrimes, "Year", transform, PercentofTotal = Number_of_Crimes/sum(Number_of_Crimes) * 100)

ggplot(ViolentCrimes, (aes(x = Year, y = PercentofTotal, fill = Crime_Type)) +
  geom_area() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  ylab("Percent of Total")`

Proportional Stacked Area Graph Fail

But when I change geom_area to geom_bar, and add stat="identity", the bar graph seems to work just fine, even though it's hard to read (which is why I wanted the proportional area graph):

Stacked Bar Plot

Link to full data-set: https://docs.google.com/spreadsheets/d/1Be4rhySLUGUXkNke8zirwxVpKCZw3uSmW4Hkku0Uc9E/edit?usp=sharing

Any help is appreciated - thank you very much.

like image 899
Lantz McGinnis-Brown Avatar asked Mar 21 '26 16:03

Lantz McGinnis-Brown


2 Answers

A slight improvement to the answer can be making ggplot do the heavylifting of converting y axis to percentage instead. This can be done done by adding position = fill argument to geom_area()

ViolentCrimes <- df  %>%
    group_by(Year, Crime_Type) %>%
    summarise(n = sum(Number_of_Crimes))

ggplot(ViolentCrimes, (aes(x = Year,  y = n, fill = Crime_Type))) +
    geom_area(position = "fill") +
    scale_y_continuous(labels = scales::percent)
like image 113
Nitish Sahay Avatar answered Mar 23 '26 05:03

Nitish Sahay


You just need to prepare your data, grouping by Year and Crime_type. I use dplyr:

library(dplyr)
ViolentCrimes <- df  %>%
  group_by(Year, Crime_Type) %>%
  summarise(n = sum(Number_of_Crimes)) %>%
  mutate(percentage = n / sum(n))

ggplot(ViolentCrimes, (aes(x = Year,  y = percentage, fill = Crime_Type))) +
  geom_area() 

enter image description here

like image 32
mpalanco Avatar answered Mar 23 '26 06:03

mpalanco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!