Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为streamgraph制作表格_R_Data Visualization_Tidyverse - Fatal编程技术网

为streamgraph制作表格

为streamgraph制作表格,r,data-visualization,tidyverse,R,Data Visualization,Tidyverse,大家好,我正在尝试使用以下链接中的数据绘制流图:。 我的目标是对变量gname的每个恐怖组织的恐怖袭击频率进行流程图绘制,但我的问题是,我不知道如何过滤数据帧,以便获得绘制流程图所需的所有参数,这些参数是数据,键,值,日期 我试图通过使用以下代码获得原始数据帧的子集 str <- terror %>% filter(gname != "Unknown") %>% group_by(gname) %>% summarise(total=n()) %&

大家好,我正在尝试使用以下链接中的数据绘制流图:。 我的目标是对变量
gname
的每个恐怖组织的恐怖袭击频率进行流程图绘制,但我的问题是,我不知道如何过滤数据帧,以便获得绘制流程图所需的所有参数,这些参数是
数据
日期

我试图通过使用以下代码获得原始数据帧的子集

str <- terror %>%
    filter(gname != "Unknown") %>%
    group_by(gname) %>%
    summarise(total=n()) %>%
    arrange(desc(total)) %>%
    head(20)
str%
过滤器(gname!=“未知”)%>%
分组依据(gname)%>%
汇总(总计=n())%>%
排列(描述(总计))%>%
总目(20)
但我只得到了每个恐怖组织的袭击频率,而没有得到每年的袭击次数。 你能提出什么办法吗?那太棒了!
谢谢大家的阅读和帮助。

达里奥和肯特说得对。您需要在group_by函数中添加iyear变量:

terror %>%
  filter(gname != "Unknown") %>%
  group_by(gname, iyear) %>%
  summarise(total=n()) %>%
  arrange(desc(total)) %>%
  head(20) -> str
str
    # A tibble: 20 x 3
    # Groups:   gname [7]
       gname                                           iyear total
       <chr>                                           <int> <int>
     1 Islamic State of Iraq and the Levant (ISIL)      2016  1454
     2 Islamic State of Iraq and the Levant (ISIL)      2017  1315
     3 Islamic State of Iraq and the Levant (ISIL)      2014  1249
     4 Taliban                                          2015  1249
     5 Islamic State of Iraq and the Levant (ISIL)      2015  1221
     6 Taliban                                          2016  1065
     7 Taliban                                          2014  1035
     8 Taliban                                          2017   894
     9 Al-Shabaab                                       2014   871
    10 Taliban                                          2012   800
    11 Taliban                                          2013   775
    12 Al-Shabaab                                       2017   570
    13 Al-Shabaab                                       2016   564
    14 Boko Haram                                       2015   540
    15 Shining Path (SL)                                1989   509
    16 Communist Party of India - Maoist (CPI-Maoist)   2010   505
    17 Shining Path (SL)                                1984   502
    18 Boko Haram                                       2014   495
    19 Shining Path (SL)                                1983   493
    20 Farabundo Marti National Liberation Front (FML~  1991   492
我一直很难对这些图形进行注释,据我所知,必须手动完成:

str %>% streamgraph("gname", "total", "iyear") %>%
  sg_annotate(label="ISIL", x=as.Date("2016-01-01"), y=1454, size=14)

如果您添加了一个服务,我们将更容易为您提供帮助。关于你的问题,请以易于复制的形式添加一些数据(参见上面的链接):group_by(gname)、mutate和group_by(gname,year)的组合可能会奏效(获取每个gname的总计和每个gname year组合的总计。要获取按组和年份的计数,你应该
group_by(gname,IEAR)
。非常感谢,我还有一个小问题。输入您提供的代码后,将不显示任何输出。您知道可能的原因吗?输出保存到一个名为str的R对象。然后将其用作流图的数据。如果希望在屏幕上看到输出,请键入对象的名称。
str %>% streamgraph("gname", "total", "iyear") %>%
  sg_annotate(label="ISIL", x=as.Date("2016-01-01"), y=1454, size=14)