在R中叠加多面板时间序列

时间:2022-06-15 14:56:21

I have four time-series, two of each observation type (two different methods). For visualization, I want to overlay the two corresponding methods. So, essentially I have two multi-panel time-series that I want to plot on top of each other. However, this isn't working. If I plot everything in just one panel, it works fine, but once I try it with multi-panel, the second graph of the second set of time-series is only plotted in the first panel. My time-series are zoo objects created with read.zoo.

我有四个时间序列,每种观察类型两种(两种不同的方法)。对于可视化,我想重叠两个相应的方法。所以,基本上我有两个多面板时间序列,我想在彼此之上绘制。但是,这不起作用。如果我只在一个面板中绘制所有内容,它可以正常工作,但是一旦我尝试使用多面板,第二组时间序列的第二个图形仅绘制在第一个面板中。我的时间序列是用read.zoo创建的动物园对象。

From the suggested questions I already tried mfg = c(1, 1), but that didn't help, either.

从建议的问题我已经尝试了mfg = c(1,1),但这也没有帮助。

PS: I know that the time-ranges don't match, yet, but that is easily fixed, later. Right now I really need to get them to plot on top of each other, first.

PS:我知道时间范围不匹配,但稍后很容易修复。现在我真的需要让他们首先在彼此之上进行绘图。

method1.ts
                2017      2018
2017-01-22        NA 12.833333
2017-01-24        NA 12.166667
2017-01-26        NA  9.666667
2017-01-27  7.666667        NA
2017-01-29        NA 13.000000
2017-01-31 13.666667 16.200000
2017-02-07        NA 25.800000
2017-02-08 12.833333        NA
2017-02-09        NA 14.333333
2017-02-12        NA 20.833333
2017-02-14        NA 14.500000
2017-02-15 14.500000        NA
2017-02-16        NA 21.800000
2017-02-17 18.400000        NA
2017-02-21        NA 11.333333
2017-02-23 11.000000 10.000000
2017-02-26        NA  9.000000
2017-02-28        NA  5.333333
2017-03-03 11.200000        NA
2017-03-06 14.000000  8.500000
2017-03-07 15.166667 11.000000
2017-03-08 18.500000        NA
2017-03-09 10.600000  6.400000
2017-03-10  5.666667        NA
2017-03-14        NA  7.833333
2017-03-16 17.166667  6.333333
2017-03-19        NA  6.200000
2017-03-20 10.666667        NA
2017-03-21        NA  4.833333
2017-03-23        NA  2.200000
2017-03-27        NA  5.000000
2017-03-29  5.000000        NA
2017-03-30  2.166667  3.500000
2017-03-31  2.166667        NA
2017-04-02        NA  4.333333
2017-04-04  2.666667        NA
2017-04-06        NA  5.666667
2017-04-09        NA  3.000000
2017-04-11  4.000000        NA

method2.ts

       2017SMA10 2018SMA10

2017-01-01              103.47929              105.55044
2017-01-02              103.65610              105.99537
2017-01-03              103.95392              106.34103
2017-01-04              104.46880              106.60617
2017-01-05              104.73571              107.03311
2017-01-06              105.22571              107.51610
2017-01-07              105.66239              107.86581
2017-01-08              106.32076              108.15234
2017-01-09              106.71721              108.23427
2017-01-10              107.13703              108.62571
2017-01-11              107.33828              108.96101
2017-01-12              108.00310              109.32104
2017-01-13              108.08786              109.60690
2017-01-14              108.39603              109.79566
2017-01-15              109.14699              109.78634
2017-01-16              109.59578              109.88837
2017-01-17              109.94503              110.04936
2017-01-18              110.01586              110.29642
2017-01-19              110.28466              110.35866
2017-01-20              110.62044              110.46795
2017-01-21              110.89312              110.42743
2017-01-22              110.95852              110.37680
2017-01-23              111.36118              110.09512
2017-01-24              111.05119              109.97177
2017-01-25              110.90067              109.92303
2017-01-26              110.62912              109.82026
2017-01-27              110.60603              109.60733
2017-01-28              110.47436              109.21956
2017-01-29              110.42441              109.12986
2017-01-30              110.51541              109.06790
2017-01-31              110.53018              109.22308
2017-02-01              110.73878              109.07460
2017-02-02              111.12024              109.19609
2017-02-03              111.73241              109.43983
2017-02-04              112.24936              109.59187
2017-02-05              112.62716              109.92450
2017-02-06              112.61923              110.30853
2017-02-07              112.97632              110.71683
2017-02-08              113.39098              110.97291
2017-02-09              113.45327              110.98421
2017-02-10              113.48387              111.02553
2017-02-11              113.25614              111.28360
2017-02-12              113.02449              111.19611
2017-02-13              112.92730              110.83878
2017-02-14              112.54101              110.91351
2017-02-15              112.71891              110.91647
2017-02-16              113.11793              110.92492
2017-02-17              113.19623              111.01643
2017-02-18              113.42685              111.03396
2017-02-19              113.45987              111.12398
2017-02-20              113.58438              111.27783
2017-02-21              113.75347              111.26295
2017-02-22              113.67686              111.55714
2017-02-23              113.61202              111.77674
2017-02-24              113.71818              111.84619
2017-02-25              113.53106              111.78978
2017-02-26              113.27395              111.52515
2017-02-27              113.37993              111.07457
2017-02-28              112.99659              110.72079
2017-03-01              113.03771              110.26982
2017-03-02              113.03668              109.62535
2017-03-03              113.01434              109.60159
2017-03-04              112.97362              109.40245
2017-03-05              113.11412              109.45290
2017-03-06              112.96689              109.24544
2017-03-07              112.98576              109.14174
2017-03-08              113.08641              109.07428
2017-03-09              112.70413              109.25517
2017-03-10              112.62559              109.46077
2017-03-11              112.19805              109.52448
2017-03-12              112.08765              109.75256
2017-03-13              111.81722              109.47225
2017-03-14              111.73174              109.22018
2017-03-15              111.55903              108.96538
2017-03-16              111.47345              108.71323
2017-03-17              111.47883              108.40790
2017-03-18              111.26682              107.86127
2017-03-19              111.29885              107.46722
2017-03-20              111.26148              106.55255
2017-03-21              111.16590              106.25858
2017-03-22              110.77000              105.54820
2017-03-23              110.32807              104.84036
2017-03-24              109.68646              104.49891
2017-03-25              108.74408              104.04718
2017-03-26              107.68592              103.63353
2017-03-27              106.52149              103.28782
2017-03-28              105.84684              103.04769
2017-03-29              105.07075              102.77137
2017-03-30              104.20832              103.04664
2017-03-31              103.63085              102.78385
2017-04-01              102.73122              102.84506
2017-04-02              101.91008              102.77019
2017-04-03              101.44941              102.51452
2017-04-04              101.24398              102.48821
2017-04-05              101.18613              102.32695
2017-04-06              101.54618              102.32948
2017-04-07              101.54394              102.40627
2017-04-08              101.33914              102.26635
2017-04-09              101.27709              101.93674
2017-04-10              101.11309              101.79369
2017-04-11              101.22372              101.64324
2017-04-12              101.36781              101.42651
2017-04-13              101.53346              101.14693
2017-04-14              101.21404              100.64058
2017-04-15              101.13418              100.27125
2017-04-16              100.57103               99.72244
2017-04-17              100.29517               99.24280
2017-04-18               99.70332               98.84724
2017-04-19               99.15901               98.52565
2017-04-20               98.64558               98.19044
2017-04-21               98.27425               97.76493
2017-04-22               98.12550               97.59279
2017-04-23               97.80408               97.47610
2017-04-24               97.89017               97.44518
2017-04-25               97.63646               97.41834
2017-04-26               96.95511               97.28475
2017-04-27               95.99498               97.31325
2017-04-28               95.63205               97.64434
2017-04-29               95.46168               97.63731
2017-04-30               95.41300               97.67666


 plot(method1.ts, type="h", lwd=3, lend=1, plot.type="multiple")
 par(new=T, mfrow=c(2,1))
 plot(method2.ts, lty=1, lwd=2, plot.type="multiple", xaxt="n", yaxt="n", xlab="", ylab="")

在R中叠加多面板时间序列

在R中叠加多面板时间序列

在R中叠加多面板时间序列

在R中叠加多面板时间序列

1 个解决方案

#1


1  

Multi-panel plots should be done all in one call. Merge the two zoo objects and then do it all at once. See ?plot.zoo

多面板图应该在一次调用中完成。合并两个动物园对象,然后一次完成所有操作。见?plot.zoo

library(zoo)

both <- merge(method1.ts, method2.ts, all = TRUE)
plot(both, screen = c("2017", "2018"), type = list("h", "h", "l", "l"))

在R中叠加多面板时间序列

The above plot line would also work with lattice by replacing plot with xyplot.

通过用xyplot替换plot,上面的绘图线也适用于晶格。

Note

Inputs in reproducible form:

可重复输入的输入:

Lines1 <- "Date                2017      2018
2017-01-22        NA 12.833333
2017-01-24        NA 12.166667
2017-01-26        NA  9.666667
2017-01-27  7.666667        NA
2017-01-29        NA 13.000000
2017-01-31 13.666667 16.200000
2017-02-07        NA 25.800000
2017-02-08 12.833333        NA
2017-02-09        NA 14.333333
2017-02-12        NA 20.833333
2017-02-14        NA 14.500000
2017-02-15 14.500000        NA
2017-02-16        NA 21.800000
2017-02-17 18.400000        NA
2017-02-21        NA 11.333333
2017-02-23 11.000000 10.000000
2017-02-26        NA  9.000000
2017-02-28        NA  5.333333
2017-03-03 11.200000        NA
2017-03-06 14.000000  8.500000
2017-03-07 15.166667 11.000000
2017-03-08 18.500000        NA
2017-03-09 10.600000  6.400000
2017-03-10  5.666667        NA
2017-03-14        NA  7.833333
2017-03-16 17.166667  6.333333
2017-03-19        NA  6.200000
2017-03-20 10.666667        NA
2017-03-21        NA  4.833333
2017-03-23        NA  2.200000
2017-03-27        NA  5.000000
2017-03-29  5.000000        NA
2017-03-30  2.166667  3.500000
2017-03-31  2.166667        NA
2017-04-02        NA  4.333333
2017-04-04  2.666667        NA
2017-04-06        NA  5.666667
2017-04-09        NA  3.000000
2017-04-11  4.000000        NA"

Lines2 <- "Date       2017SMA10 2018SMA10   
2017-01-01              103.47929              105.55044
2017-01-02              103.65610              105.99537
2017-01-03              103.95392              106.34103
2017-01-04              104.46880              106.60617
2017-01-05              104.73571              107.03311
2017-01-06              105.22571              107.51610
2017-01-07              105.66239              107.86581
2017-01-08              106.32076              108.15234
2017-01-09              106.71721              108.23427
2017-01-10              107.13703              108.62571
2017-01-11              107.33828              108.96101
2017-01-12              108.00310              109.32104
2017-01-13              108.08786              109.60690
2017-01-14              108.39603              109.79566
2017-01-15              109.14699              109.78634
2017-01-16              109.59578              109.88837
2017-01-17              109.94503              110.04936
2017-01-18              110.01586              110.29642
2017-01-19              110.28466              110.35866
2017-01-20              110.62044              110.46795
2017-01-21              110.89312              110.42743
2017-01-22              110.95852              110.37680
2017-01-23              111.36118              110.09512
2017-01-24              111.05119              109.97177
2017-01-25              110.90067              109.92303
2017-01-26              110.62912              109.82026
2017-01-27              110.60603              109.60733
2017-01-28              110.47436              109.21956
2017-01-29              110.42441              109.12986
2017-01-30              110.51541              109.06790
2017-01-31              110.53018              109.22308
2017-02-01              110.73878              109.07460
2017-02-02              111.12024              109.19609
2017-02-03              111.73241              109.43983
2017-02-04              112.24936              109.59187
2017-02-05              112.62716              109.92450
2017-02-06              112.61923              110.30853
2017-02-07              112.97632              110.71683
2017-02-08              113.39098              110.97291
2017-02-09              113.45327              110.98421
2017-02-10              113.48387              111.02553
2017-02-11              113.25614              111.28360
2017-02-12              113.02449              111.19611
2017-02-13              112.92730              110.83878
2017-02-14              112.54101              110.91351
2017-02-15              112.71891              110.91647
2017-02-16              113.11793              110.92492
2017-02-17              113.19623              111.01643
2017-02-18              113.42685              111.03396
2017-02-19              113.45987              111.12398
2017-02-20              113.58438              111.27783
2017-02-21              113.75347              111.26295
2017-02-22              113.67686              111.55714
2017-02-23              113.61202              111.77674
2017-02-24              113.71818              111.84619
2017-02-25              113.53106              111.78978
2017-02-26              113.27395              111.52515
2017-02-27              113.37993              111.07457
2017-02-28              112.99659              110.72079
2017-03-01              113.03771              110.26982
2017-03-02              113.03668              109.62535
2017-03-03              113.01434              109.60159
2017-03-04              112.97362              109.40245
2017-03-05              113.11412              109.45290
2017-03-06              112.96689              109.24544
2017-03-07              112.98576              109.14174
2017-03-08              113.08641              109.07428
2017-03-09              112.70413              109.25517
2017-03-10              112.62559              109.46077
2017-03-11              112.19805              109.52448
2017-03-12              112.08765              109.75256
2017-03-13              111.81722              109.47225
2017-03-14              111.73174              109.22018
2017-03-15              111.55903              108.96538
2017-03-16              111.47345              108.71323
2017-03-17              111.47883              108.40790
2017-03-18              111.26682              107.86127
2017-03-19              111.29885              107.46722
2017-03-20              111.26148              106.55255
2017-03-21              111.16590              106.25858
2017-03-22              110.77000              105.54820
2017-03-23              110.32807              104.84036
2017-03-24              109.68646              104.49891
2017-03-25              108.74408              104.04718
2017-03-26              107.68592              103.63353
2017-03-27              106.52149              103.28782
2017-03-28              105.84684              103.04769
2017-03-29              105.07075              102.77137
2017-03-30              104.20832              103.04664
2017-03-31              103.63085              102.78385
2017-04-01              102.73122              102.84506
2017-04-02              101.91008              102.77019
2017-04-03              101.44941              102.51452
2017-04-04              101.24398              102.48821
2017-04-05              101.18613              102.32695
2017-04-06              101.54618              102.32948
2017-04-07              101.54394              102.40627
2017-04-08              101.33914              102.26635
2017-04-09              101.27709              101.93674
2017-04-10              101.11309              101.79369
2017-04-11              101.22372              101.64324
2017-04-12              101.36781              101.42651
2017-04-13              101.53346              101.14693
2017-04-14              101.21404              100.64058
2017-04-15              101.13418              100.27125
2017-04-16              100.57103               99.72244
2017-04-17              100.29517               99.24280
2017-04-18               99.70332               98.84724
2017-04-19               99.15901               98.52565
2017-04-20               98.64558               98.19044
2017-04-21               98.27425               97.76493
2017-04-22               98.12550               97.59279
2017-04-23               97.80408               97.47610
2017-04-24               97.89017               97.44518
2017-04-25               97.63646               97.41834
2017-04-26               96.95511               97.28475
2017-04-27               95.99498               97.31325
2017-04-28               95.63205               97.64434
2017-04-29               95.46168               97.63731
2017-04-30               95.41300               97.67666"

library(zoo)

method1.ts <- read.zoo(text = Lines1, header = TRUE, check.names = FALSE)
method2.ts <- read.zoo(text = Lines2, header = TRUE, check.names = FALSE)

#1


1  

Multi-panel plots should be done all in one call. Merge the two zoo objects and then do it all at once. See ?plot.zoo

多面板图应该在一次调用中完成。合并两个动物园对象,然后一次完成所有操作。见?plot.zoo

library(zoo)

both <- merge(method1.ts, method2.ts, all = TRUE)
plot(both, screen = c("2017", "2018"), type = list("h", "h", "l", "l"))

在R中叠加多面板时间序列

The above plot line would also work with lattice by replacing plot with xyplot.

通过用xyplot替换plot,上面的绘图线也适用于晶格。

Note

Inputs in reproducible form:

可重复输入的输入:

Lines1 <- "Date                2017      2018
2017-01-22        NA 12.833333
2017-01-24        NA 12.166667
2017-01-26        NA  9.666667
2017-01-27  7.666667        NA
2017-01-29        NA 13.000000
2017-01-31 13.666667 16.200000
2017-02-07        NA 25.800000
2017-02-08 12.833333        NA
2017-02-09        NA 14.333333
2017-02-12        NA 20.833333
2017-02-14        NA 14.500000
2017-02-15 14.500000        NA
2017-02-16        NA 21.800000
2017-02-17 18.400000        NA
2017-02-21        NA 11.333333
2017-02-23 11.000000 10.000000
2017-02-26        NA  9.000000
2017-02-28        NA  5.333333
2017-03-03 11.200000        NA
2017-03-06 14.000000  8.500000
2017-03-07 15.166667 11.000000
2017-03-08 18.500000        NA
2017-03-09 10.600000  6.400000
2017-03-10  5.666667        NA
2017-03-14        NA  7.833333
2017-03-16 17.166667  6.333333
2017-03-19        NA  6.200000
2017-03-20 10.666667        NA
2017-03-21        NA  4.833333
2017-03-23        NA  2.200000
2017-03-27        NA  5.000000
2017-03-29  5.000000        NA
2017-03-30  2.166667  3.500000
2017-03-31  2.166667        NA
2017-04-02        NA  4.333333
2017-04-04  2.666667        NA
2017-04-06        NA  5.666667
2017-04-09        NA  3.000000
2017-04-11  4.000000        NA"

Lines2 <- "Date       2017SMA10 2018SMA10   
2017-01-01              103.47929              105.55044
2017-01-02              103.65610              105.99537
2017-01-03              103.95392              106.34103
2017-01-04              104.46880              106.60617
2017-01-05              104.73571              107.03311
2017-01-06              105.22571              107.51610
2017-01-07              105.66239              107.86581
2017-01-08              106.32076              108.15234
2017-01-09              106.71721              108.23427
2017-01-10              107.13703              108.62571
2017-01-11              107.33828              108.96101
2017-01-12              108.00310              109.32104
2017-01-13              108.08786              109.60690
2017-01-14              108.39603              109.79566
2017-01-15              109.14699              109.78634
2017-01-16              109.59578              109.88837
2017-01-17              109.94503              110.04936
2017-01-18              110.01586              110.29642
2017-01-19              110.28466              110.35866
2017-01-20              110.62044              110.46795
2017-01-21              110.89312              110.42743
2017-01-22              110.95852              110.37680
2017-01-23              111.36118              110.09512
2017-01-24              111.05119              109.97177
2017-01-25              110.90067              109.92303
2017-01-26              110.62912              109.82026
2017-01-27              110.60603              109.60733
2017-01-28              110.47436              109.21956
2017-01-29              110.42441              109.12986
2017-01-30              110.51541              109.06790
2017-01-31              110.53018              109.22308
2017-02-01              110.73878              109.07460
2017-02-02              111.12024              109.19609
2017-02-03              111.73241              109.43983
2017-02-04              112.24936              109.59187
2017-02-05              112.62716              109.92450
2017-02-06              112.61923              110.30853
2017-02-07              112.97632              110.71683
2017-02-08              113.39098              110.97291
2017-02-09              113.45327              110.98421
2017-02-10              113.48387              111.02553
2017-02-11              113.25614              111.28360
2017-02-12              113.02449              111.19611
2017-02-13              112.92730              110.83878
2017-02-14              112.54101              110.91351
2017-02-15              112.71891              110.91647
2017-02-16              113.11793              110.92492
2017-02-17              113.19623              111.01643
2017-02-18              113.42685              111.03396
2017-02-19              113.45987              111.12398
2017-02-20              113.58438              111.27783
2017-02-21              113.75347              111.26295
2017-02-22              113.67686              111.55714
2017-02-23              113.61202              111.77674
2017-02-24              113.71818              111.84619
2017-02-25              113.53106              111.78978
2017-02-26              113.27395              111.52515
2017-02-27              113.37993              111.07457
2017-02-28              112.99659              110.72079
2017-03-01              113.03771              110.26982
2017-03-02              113.03668              109.62535
2017-03-03              113.01434              109.60159
2017-03-04              112.97362              109.40245
2017-03-05              113.11412              109.45290
2017-03-06              112.96689              109.24544
2017-03-07              112.98576              109.14174
2017-03-08              113.08641              109.07428
2017-03-09              112.70413              109.25517
2017-03-10              112.62559              109.46077
2017-03-11              112.19805              109.52448
2017-03-12              112.08765              109.75256
2017-03-13              111.81722              109.47225
2017-03-14              111.73174              109.22018
2017-03-15              111.55903              108.96538
2017-03-16              111.47345              108.71323
2017-03-17              111.47883              108.40790
2017-03-18              111.26682              107.86127
2017-03-19              111.29885              107.46722
2017-03-20              111.26148              106.55255
2017-03-21              111.16590              106.25858
2017-03-22              110.77000              105.54820
2017-03-23              110.32807              104.84036
2017-03-24              109.68646              104.49891
2017-03-25              108.74408              104.04718
2017-03-26              107.68592              103.63353
2017-03-27              106.52149              103.28782
2017-03-28              105.84684              103.04769
2017-03-29              105.07075              102.77137
2017-03-30              104.20832              103.04664
2017-03-31              103.63085              102.78385
2017-04-01              102.73122              102.84506
2017-04-02              101.91008              102.77019
2017-04-03              101.44941              102.51452
2017-04-04              101.24398              102.48821
2017-04-05              101.18613              102.32695
2017-04-06              101.54618              102.32948
2017-04-07              101.54394              102.40627
2017-04-08              101.33914              102.26635
2017-04-09              101.27709              101.93674
2017-04-10              101.11309              101.79369
2017-04-11              101.22372              101.64324
2017-04-12              101.36781              101.42651
2017-04-13              101.53346              101.14693
2017-04-14              101.21404              100.64058
2017-04-15              101.13418              100.27125
2017-04-16              100.57103               99.72244
2017-04-17              100.29517               99.24280
2017-04-18               99.70332               98.84724
2017-04-19               99.15901               98.52565
2017-04-20               98.64558               98.19044
2017-04-21               98.27425               97.76493
2017-04-22               98.12550               97.59279
2017-04-23               97.80408               97.47610
2017-04-24               97.89017               97.44518
2017-04-25               97.63646               97.41834
2017-04-26               96.95511               97.28475
2017-04-27               95.99498               97.31325
2017-04-28               95.63205               97.64434
2017-04-29               95.46168               97.63731
2017-04-30               95.41300               97.67666"

library(zoo)

method1.ts <- read.zoo(text = Lines1, header = TRUE, check.names = FALSE)
method2.ts <- read.zoo(text = Lines2, header = TRUE, check.names = FALSE)