绘制重叠时间序列

时间:2014-04-19 08:42:26

标签: r plot time-series base

我想要绘制四个时间序列,最好使用基本图形。

year<-c(2000,2001,2002,2003,2004,2005,2006,2007) #x axis
a<-c(36.2,42.4,43.4,56.4,67.4,42.4,22.4,20.4) 
b<-c(NA,NA,NA,56.4,67.4,42.4,22.4,20.4) 
c<-c(36.4,42.3,43.7,56.4,67.2,88.3,99.3,99.9) 
d<-c(36.4,42.3,43.7,56.4,NA,NA,NA,NA) 

#using different thickness of lines to distinguish between them? Dunno
par(mar = c(4,4,4,4))
par(mfrow=c(1,2))
plot(year,a, xlab="Year", ylab="Whatever", type="l", lwd=6, ylim=c(20, 100))
lines(year,b, type="l", lwd=2, col="yellow")
plot(year,c, xlab="Year", ylab="Whatever", type="l", lwd=6, ylim=c(20, 100))
lines(year,d, type="l", lwd=2, col="yellow")

正如您所看到的,两个系列中的一个始终是另一个系列的一个子集,而系列a和b在它们的开头非常相似,因此在一个图(这个特征)中看起来不太好。 你有什么建议如何绘制这种数据吗?我不介意在一个情节中拥有它,但没有必要。 欢呼声。

2 个答案:

答案 0 :(得分:3)

这使用plot.zoo,而plot.zoo又使用基本图形并接受大多数基本图形图形参数。请注意,在plot.zoo中,collwd被回收。 screen参数指示每个系列所在的位置,以便将其全部放在一个面板中,将screen参数替换为screen = 1。根据需要添加main和其他参数。请参阅?plot.zoo,并在下面首先绘制。

library(zoo)
plot(zoo(cbind(a, b, c, d), year), screen = c(1, 1, 2, 2), 
  col = c("black", "yellow"), ylim = c(20, 100), lwd = c(6, 2))

另请注意,在这种情况下,只需添加library(lattice)并将plot更改为xyplot,我们就可以通过xyplot.zoo获得格子图(下面第二张图)。

plot.zoo plot

xyplot.zoo plot

答案 1 :(得分:0)

如下:

year <- c(2000,2001,2002,2003,2004,2005,2006,2007) #x axis
a <- c(36.2,42.4,43.4,56.4,67.4,42.4,22.4,20.4) 
b <- c(NA,NA,NA,56.4,67.4,42.4,22.4,20.4) 
c <- c(36.4,42.3,43.7,56.4,67.2,88.3,99.3,99.9) - 100
d <- c(36.4,42.3,43.7,56.4,NA,NA,NA,NA) - 100

#using different thickness of lines to distinguish between them? Dunno
par(mar = c(4,4,4,4))
par(mfrow=c(1,1))
plot(year,a, xlab="Year", ylab="Whatever", type="l", lwd=6, ylim=c(-100, 100))
lines(year,b, type="l", lwd=2, col="yellow")
lines(year,c, xlab="Year", ylab="Whatever", type="l", lwd=6, ylim=c(-100, 100))
lines(year,d, type="l", lwd=2, col="yellow")
相关问题