【问题标题】:Cumulative stacked area plot for counts in ggplot with Rggplot中计数的累积堆积面积图与R
【发布时间】:2021-03-19 06:48:34
【问题描述】:

我有一个过去几十年采用的政策的时间序列,并且想要制作具有累积政策计数的堆叠区域图,因为它们在采用后仍然有效。我希望将它们按组织分组,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

非常感谢任何帮助!!!

【问题讨论】:

  • 请提供您的数据的代表性样本,您添加的样本创建了一个空图。
  • 你试过用数字交换你的count变量中的字符向量吗?
  • 抱歉,刚做了代表性样品!
  • 是的,我将向量更改为数字,但这似乎不起作用...

标签: r ggplot2 plot area stacked


【解决方案1】:

每个组织都需要代表每一年,即使该组织有 0 个。然后将 mutate 添加到您的代码中并绘制累计总数。

为每个组织重新制作数据示例,使其每年都有一个数字,有些为 0

df = data.frame(time = rep(c(1990,1991,1992),3),
org = c("a","a","a","b","b","b","c","c","c"),
num = c(1,0,1,0,0,1,1,1,1))


  df%>%
  group_by(org, time) %>%
  summarise(total = sum(num)) %>%
  mutate(newtot = cumsum(total))%>%
  ggplot(aes(x= time, y= newtot,fill=org)) +
  geom_area()

【讨论】:

    【解决方案2】:

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

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

    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 package (v0.3.0) 于 2020 年 12 月 10 日创建

    【讨论】:

    • 这很好用!我可以通过第一部分,但是当我去绘图时,我收到通知:'错误:美学不能随丝带而变化'知道这可能指的是什么吗?
    • 您传递给fill = 的内容可能有问题。这是变化的审美。所以它应该是一些分类数据,而不是数字。我不确定它是否会有很大帮助,但这个问题已经在其他地方被问过,如果这个问题仍然存在,可能有助于开始深入研究stackoverflow.com/questions/57333161
    • 如果这个答案能解决你的问题,请随时接受这个答案:)
    • 我怀疑您的计数总和实际上并不是根据您的原始帖子累积的。这将导致请查看我共享的代码,并注意我是如何使用arrange(organization, year) %&gt;% group_by(organization) %&gt;% mutate(aggcount = cumsum(count)) 计算累积和的。并确保您的数据中有第一年和最后几年的数据点。没有它们,这些区域将无法连接并显示随时间推移的实际累积总和。我希望这会有所帮助。
    • 感谢您的图片。有了这个并再次查看情节,看起来情节并不完全准确。例如,我的示例 A 组织的某些年份不准确并且丢失了一些计数。我已经更新了代码,以便为组织添加所有年份。那应该填补你看到的那些空白。并确保您正确计算累积总和。您分享的图片似乎没有正确添加。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多