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")`

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):

Link to full data-set: https://docs.google.com/spreadsheets/d/1Be4rhySLUGUXkNke8zirwxVpKCZw3uSmW4Hkku0Uc9E/edit?usp=sharing
Any help is appreciated - thank you very much.
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)
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()

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