【问题标题】:Plotting Time Series in R by group and with multiple variables按组和多个变量在 R 中绘制时间序列
【发布时间】:2021-12-15 14:35:24
【问题描述】:

如何在 R 中获得最佳和简单的时间序列图:按 (7) 省分组,并显示 4 个变量(BI、PD、AP、COLL。)。我想单独绘制组。

我尝试了 timetk 包的 plot_time_series() 功能,但我一直在搞砸并没有给我想要的东西。

  • 这是我的数据:
dput(data)
structure(list(Province = c("Alberta", "Alberta", "Alberta", 
"Alberta", "Alberta", "Alberta", "Alberta", "Alberta", "Alberta", 
"Alberta", "Alberta", "Alberta", "Alberta", "Alberta", "Alberta", 
"Alberta", "Alberta", "Alberta", "NewBrunswick", "NewBrunswick", 
"NewBrunswick", "NewBrunswick", "NewBrunswick", "NewBrunswick", 
"NewBrunswick", "NewBrunswick", "NewBrunswick", "NewBrunswick", 
"NewBrunswick", "NewBrunswick", "NewBrunswick", "NewBrunswick", 
"NewBrunswick", "NewBrunswick", "NewBrunswick", "NewBrunswick", 
"NewfoundlandandLabrador", "NewfoundlandandLabrador", "NewfoundlandandLabrador", 
"NewfoundlandandLabrador", "NewfoundlandandLabrador", "NewfoundlandandLabrador", 
"NewfoundlandandLabrador", "NewfoundlandandLabrador", "NewfoundlandandLabrador", 
"NewfoundlandandLabrador", "NewfoundlandandLabrador", "NewfoundlandandLabrador", 
"NewfoundlandandLabrador", "NewfoundlandandLabrador", "NewfoundlandandLabrador", 
"NewfoundlandandLabrador", "NewfoundlandandLabrador", "NewfoundlandandLabrador", 
"NovaScotia", "NovaScotia", "NovaScotia", "NovaScotia", "NovaScotia", 
"NovaScotia", "NovaScotia", "NovaScotia", "NovaScotia", "NovaScotia", 
"NovaScotia", "NovaScotia", "NovaScotia", "NovaScotia", "NovaScotia", 
"NovaScotia", "NovaScotia", "NovaScotia", "PrinceEdwardIsland", 
"PrinceEdwardIsland", "PrinceEdwardIsland", "PrinceEdwardIsland", 
"PrinceEdwardIsland", "PrinceEdwardIsland", "PrinceEdwardIsland", 
"PrinceEdwardIsland", "PrinceEdwardIsland", "PrinceEdwardIsland", 
"PrinceEdwardIsland", "PrinceEdwardIsland", "PrinceEdwardIsland", 
"PrinceEdwardIsland", "PrinceEdwardIsland", "PrinceEdwardIsland", 
"PrinceEdwardIsland", "PrinceEdwardIsland", "Ontario", "Ontario", 
"Ontario", "Ontario", "Ontario", "Ontario", "Ontario", "Ontario", 
"Ontario", "Ontario", "Ontario", "Ontario", "Ontario", "Ontario", 
"Ontario", "Ontario", "Ontario", "Ontario", "Yukon,Nunavut,NWTerr", 
"Yukon,Nunavut,NWTerr", "Yukon,Nunavut,NWTerr", "Yukon,Nunavut,NWTerr", 
"Yukon,Nunavut,NWTerr", "Yukon,Nunavut,NWTerr", "Yukon,Nunavut,NWTerr", 
"Yukon,Nunavut,NWTerr", "Yukon,Nunavut,NWTerr", "Yukon,Nunavut,NWTerr", 
"Yukon,Nunavut,NWTerr", "Yukon,Nunavut,NWTerr", "Yukon,Nunavut,NWTerr", 
"Yukon,Nunavut,NWTerr", "Yukon,Nunavut,NWTerr", "Yukon,Nunavut,NWTerr", 
"Yukon,Nunavut,NWTerr", "Yukon,Nunavut,NWTerr"), AccPeriod = structure(c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), class = "Date"), 
    BI = c(1.092, 1.117, 1.177, 1.235, 1.331, 1.465, 1.645, 1.851, 
    1.048, 1.073, 1.107, 1.16, 1.245, 1.372, 1.572, 1.812, 2.097, 
    2.901, 1.09, 1.123, 1.144, 1.217, 1.334, 1.463, 1.586, 1.729, 
    1.089, 1.115, 1.14, 1.21, 1.289, 1.418, 1.604, 1.787, 1.946, 
    2.508, 1.012, 1.016, 1.031, 1.049, 1.091, 1.117, 1.162, 1.226, 
    1, 1, 0.998, 1.008, 1.025, 1.059, 1.113, 1.176, 1.285, 2.055, 
    1.025, 1.042, 1.072, 1.12, 1.185, 1.276, 1.391, 1.495, 1.04, 
    1.062, 1.094, 1.128, 1.202, 1.292, 1.412, 1.559, 1.771, 2.361, 
    1.056, 1.063, 1.131, 1.149, 1.211, 1.308, 1.407, 1.452, 1.025, 
    1.037, 1.07, 1.072, 1.146, 1.254, 1.366, 1.468, 1.574, 2.045, 
    1.009, 1.012, 1.024, 1.054, 1.105, 1.192, 1.371, 1.552, 0.995, 
    1.003, 1.017, 1.053, 1.11, 1.214, 1.398, 1.627, 1.893, 2.76, 
    1.092, 1.117, 1.177, 1.235, 1.331, 1.465, 1.645, 1.851, 1.048, 
    1.073, 1.107, 1.16, 1.245, 1.372, 1.572, 1.812, 2.097, 2.901
    ), PD = c(1, 1, 1, 1.002, 1.001, 1.001, 1.005, 1.02, 1, 1, 
    1, 1, 1.001, 1.001, 1.005, 1.027, 1.059, 1.311, 0.999, 0.998, 
    0.998, 0.999, 1.003, 1, 1.004, 1.033, 1.002, 1.002, 1.002, 
    0.998, 0.999, 1.009, 1.035, 1.092, 1.187, 1.195, 1, 1, 1, 
    0.997, 0.994, 0.995, 0.988, 0.996, 1.001, 0.997, 0.998, 0.998, 
    0.999, 0.999, 1.005, 1.019, 1.059, 1.398, 1, 1, 1, 1, 1.002, 
    1.003, 1.005, 1.01, 1, 1, 1, 1.007, 1.008, 1.007, 1.013, 
    1.091, 1.147, 1.048, 1, 1, 1, 1, 1, 1, 1.005, 1.018, 1, 1, 
    1, 1, 1, 1, 1.054, 1.116, 1.136, 1.144, 1, 1, 1, 1, 1.002, 
    1.012, 1.077, 1.179, 1, 1, 1, 1.004, 1.01, 1.03, 1.099, 1.225, 
    1.479, 2.077, 1, 1, 1, 1.002, 1.001, 1.001, 1.005, 1.02, 
    1, 1, 1, 1, 1.001, 1.001, 1.005, 1.027, 1.059, 1.311), Coll. = c(1, 
    1, 1, 1, 0.999, 0.996, 0.975, 0.932, 1, 1, 1, 1, 1, 0.998, 
    0.989, 0.964, 0.885, 0.652, 1, 1, 1, 1, 1, 1, 1, 0.998, 1, 
    1, 1, 1, 1, 1, 1, 1, 0.996, 0.974, 1, 1, 1, 1, 1, 0.997, 
    0.989, 0.974, 1, 1, 1, 1, 1, 0.999, 0.991, 0.972, 0.903, 
    0.792, 1, 1, 1, 1, 1, 0.999, 0.997, 0.993, 1, 1, 1, 1, 1, 
    1, 1, 0.999, 0.995, 0.984, 1, 1, 1, 1, 0.999, 0.998, 0.991, 
    0.977, 1, 1, 1, 1, 1, 0.999, 0.999, 0.998, 0.99, 0.968, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1.001, 1.002, 1.004, 
    1.042, 1, 1, 1, 1, 0.999, 0.996, 0.975, 0.932, 1, 1, 1, 1, 
    1, 0.998, 0.989, 0.964, 0.885, 0.652), AllPerils = c(1, 1, 
    1, 1, 1, 1.001, 0.995, 0.974, 1, 1, 1, 1, 0.995, 0.996, 0.986, 
    0.965, 0.905, 0.842, 1, 1, 1, 1, 1, 1, 0.997, 0.996, 1, 1, 
    1, 1, 1, 1, 1, 1, 0.999, 0.998, 1, 1, 1, 1, 1.002, 0.996, 
    0.987, 0.979, 1, 1, 1, 1, 0.999, 0.997, 0.99, 0.963, 0.917, 
    0.786, 1, 1, 1, 1, 0.998, 0.999, 0.997, 0.988, 1, 1, 1, 1, 
    1, 0.999, 1, 0.999, 0.994, 0.996, 1, 1, 1, 1, 1, 1, 0.998, 
    0.991, 1, 1, 1, 1, 1, 1, 1.001, 0.997, 1, 1.023, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 0.999, 0.999, 0.999, 0.999, 1, 
    1.045, 1, 1, 1, 1, 1, 1.001, 0.995, 0.974, 1, 1, 1, 1, 0.995, 
    0.996, 0.986, 0.965, 0.905, 0.842)), row.names = c(NA, -126L
), class = c("tbl_df", "tbl", "data.frame"))

【问题讨论】:

  • 看看一些ggplot2-tutorials
  • 如果您在问题中包含我们可以处理的示例数据,这将有助于我们为您提供帮助。数据的图片是没有用的,因为我们必须转录它来展示一个可行的解决方案。即使那样,您的图片中也只显示了一个省份,因此我们无法演示多省解决方案。如果可以,请通过将dput(AmountOfClaims) 的输出复制/粘贴为text 在此处共享数据。谢谢。
  • Allan,Wimpel,我粘贴了我正在处理的数据。我对编程世界很陌生。我潜入学习 R 并且不知何故我一直在弄乱我的代码并结束观看没有给我想要的输出的教程。我希望你能帮助我给我最好的做法。谢谢。
  • 您好,您能再试一次输入数据吗?看来您的 AccPeriod 变量都是 NA

标签: r time-series


【解决方案1】:

在您的数据中,AccPeriod 数据缺失。因此,根据您的屏幕截图,我快速重新创建了从 2012 年到 2021 年的两年期数据。

library(tidyverse)
data %>% 
  group_by(Province) %>% 
  mutate(AccPeriod = as.Date(c("2012-01-01", "2012-07-01", "2013-01-01", "2013-07-01", "2014-01-01", 
                       "2014-07-01", "2015-01-01", "2015-07-01", "2016-01-01", "2016-07-01", 
                       "2017-01-01", "2017-07-01", "2018-01-01", "2018-07-01", "2019-01-01", 
                       "2019-07-01", "2020-01-01", "2020-07-01"))) %>% 
  ungroup -> data

现在我们有了需要的数据,我们首先需要将其重新格式化为一个长数据框:

# reformat to long format
data %>% 
  pivot_longer(-c(AccPeriod, Province)) -> data

现在我们可以绘制它(注意paste0(Province,name),它只是确保我们按每行的名称(即变量和省)对每一行进行分组):

ggplot(data) + 
  aes(x = AccPeriod, y = value, group = paste0(Province,name)) + 
  geom_line()

这可行,但结果图不是很好。

让我们来点有趣的:我们首先确保每个变量都有自己的小块,每个省都有自己的颜色:

ggplot(data) + 
  aes(x = AccPeriod, y = value, group = paste0(Province,name), color = Province) + 
  geom_line() + 
  facet_wrap(~name, scales = "free") # scales free as the units are different (makes it look nicer)

或者反过来:

ggplot(data) + 
  aes(x = AccPeriod, y = value, group = paste0(Province,name), color = name) + 
  geom_line() + 
  facet_wrap(~Province) # scales fixed as the units are the same (makes it more comparable)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-31
    • 2020-03-30
    • 2020-08-15
    • 2017-03-21
    • 1970-01-01
    • 2022-01-07
    • 2011-12-09
    • 1970-01-01
    相关资源
    最近更新 更多