ggplot-比例堆积面积图

时间:2018-06-29 14:54:21

标签: r ggplot2

我不明白为什么我的比例堆积面积图无法正常工作。当我使用以下代码时,我得到了奇怪的偏斜视觉效果:

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

但是当我将geom_area更改为geom_bar并添加stat =“ identity”时,条形图似乎工作正常,即使很难阅读(这也是为什么我想要比例区域图):

Stacked Bar Plot

链接到完整的数据集: https://docs.google.com/spreadsheets/d/1Be4rhySLUGUXkNke8zirwxVpKCZw3uSmW4Hkku0Uc9E/edit?usp=sharing

感谢您的帮助-非常感谢。

2 个答案:

答案 0 :(得分:1)

您只需要准备数据(按Year和Crime_type分组)即可。我使用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

答案 1 :(得分:0)

对答案的稍微改善可以使ggplot代替将y轴转换为百分比的繁重工作。这可以通过在position = fill上添加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)