首页 > 解决方案 > ggplot中计数的累积堆积面积图与R

问题描述

我有一个在过去几十年中采用的政策时间序列,并且想要制作一个累积政策计数的堆叠区域图,因为它们在采用后仍然有效。我希望它们按组织分组,x 为时间,y 为累积计数,以显示随着时间的推移政策采用的增长。

数据:

df<- data.frame(
  organization = c("a", "a", "c", "c", "a", "b"),
  year = c(1990, 1991, 1992, 1993, 1994, 1995),
  count= c(1,1,1,0,1,1))

我尝试了以下方法:

df%>%
group_by(organization, year) %>%
summarise(total = sum(count)) %>%
ggplot(  aes(x=year, y= cumsum( total),fill=factor(organization))) +
geom_area(position = "stack")

现在我得到了一个这样的情节,它不是累积的——我认为这是因为多年来没有采取任何政策。

在此处输入图像描述

我有兴趣得到这样的东西:

在此处输入图像描述

图片来源:https ://www.r-graph-gallery.com/136-stacked-area-chart.html

我真的很感激任何帮助!!!

标签: rggplot2plotareastacked

解决方案


对于每个组织,您都需要确保至少有一个最小和最大年份的计数值。这样ggplot2才能填补空白。此外,您需要小心累积总和。因此,如果最早和去年不存在值,我在下面显示的解决方案会添加零计数。

我添加了一些代码,以便您可以为没有数据的第一年和最后一整年数据的组织自动添加行。要合并此自动化代码,您需要合并数据框并更改定义中的变量以适应您自己的数据。tail_dat complete_datdatdata.frame()

library(ggplot2)
library(dplyr)
library(tidyr)

# Create sample data
dat <- tribble(
  ~organization, ~year, ~count,
  "a", 1990, 1,
  "a", 1991, 1,
  "b", 1991, 1,
  "c", 1992, 1,
  "c", 1993, 0,
  "a", 1994, 1,
  "b", 1995, 1
)
dat
#> # A tibble: 7 x 3
#>   organization  year count
#>   <chr>        <dbl> <dbl>
#> 1 a             1990     1
#> 2 a             1991     1
#> 3 b             1991     1
#> 4 c             1992     1
#> 5 c             1993     0
#> 6 a             1994     1
#> 7 b             1995     1

# NOTE incorrect results for comparison
dat %>%
  group_by(organization, year) %>%
  summarise(total = sum(count)) %>%
  ggplot(aes(x = year, y = cumsum(total), fill = organization)) +
  geom_area()
#> `summarise()` regrouping output by 'organization' (override with `.groups` argument)


# Fill out all years and organization combinations
complete_dat <- tidyr::expand(dat, organization, year = 1990:1995)
complete_dat
#> # A tibble: 18 x 2
#>    organization  year
#>    <chr>        <int>
#>  1 a             1990
#>  2 a             1991
#>  3 a             1992
#>  4 a             1993
#>  5 a             1994
#>  6 a             1995
#>  7 b             1990
#>  8 b             1991
#>  9 b             1992
#> 10 b             1993
#> 11 b             1994
#> 12 b             1995
#> 13 c             1990
#> 14 c             1991
#> 15 c             1992
#> 16 c             1993
#> 17 c             1994
#> 18 c             1995

# Update data so that counting works and fills in gaps
final_dat <- complete_dat %>%
  left_join(dat, by = c("organization", "year")) %>%
  replace_na(list(count = 0)) %>%  # Replace NA with zeros
  group_by(organization, year) %>%
  arrange(organization, year) %>%  # Arrange by year so adding works
  group_by(organization) %>%
  mutate(aggcount = cumsum(count))
final_dat
#> # A tibble: 18 x 4
#> # Groups:   organization [3]
#>    organization  year count aggcount
#>    <chr>        <dbl> <dbl>    <dbl>
#>  1 a             1990     1        1
#>  2 a             1991     1        2
#>  3 a             1992     0        2
#>  4 a             1993     0        2
#>  5 a             1994     1        3
#>  6 a             1995     0        3
#>  7 b             1990     0        0
#>  8 b             1991     1        1
#>  9 b             1992     0        1
#> 10 b             1993     0        1
#> 11 b             1994     0        1
#> 12 b             1995     1        2
#> 13 c             1990     0        0
#> 14 c             1991     0        0
#> 15 c             1992     1        1
#> 16 c             1993     0        1
#> 17 c             1994     0        1
#> 18 c             1995     0        1

# Plot results
final_dat %>%
  ggplot(aes(x = year, y = aggcount, fill = organization)) +
  geom_area()

reprex 包于 2020-12-10 创建(v0.3.0)


推荐阅读