0

我正在尝试将两个时间序列绘制到 R 中的同一个图中。

我的数据如下:

系列一:

       dates            values
1 2012-09-01 12:00:00   33.6
2 2012-09-05 13:00:00   32.0
3 2012-09-06 15:30:00   30.0
4 2012-09-07 12:45:00   30.0
5 2012-09-08 21:15:00   30.0
6 2012-09-11 15:00:00   28.4

系列2:

       dates           values
1 2012-09-03 14:05:00  15.6
2 2012-09-05 08:00:00  23.0
3 2012-09-09 15:55:00  19.0
4 2012-09-11 23:00:00  22.0
5 2012-09-14 02:40:00  34.0
6 2012-09-15 12:09:00  29.4

我拥有的代码是:

var1<-var1[,c("Sampling_Time","Value")]
var2<-var2[,c("Sampling_Time","Value")]

var1$Sampling_Time<- as.POSIXct(var1$Sampling_Time, format="%Y-%m-%d %H:%M:%S")
var2$Sampling_Time<- as.POSIXct(var2$Sampling_Time, format="%Y-%m-%d %H:%M:%S")

plot(var1$Sampling_Time, var1$Value, type= "p" , xlim= NULL, col = "red", size =1,
xlab= "Time",ylab= "Value", main= "Graphic",format="%Y-%b-%d")
par(new=TRUE)
plot(var2$Sampling_Time, var2$Value, type= "p" , xlim= NULL, col = "blue", size =1,
xlab= "Time",ylab= "Value", main= "Graphic",format="%Y-%b-%d")

我想用相同的 x 轴按时间顺序绘制这两个系列,它们的比例相同,我的意思是在一个独特的按时间顺序排列的 x 轴上。

我怎样才能最好地在 R 中实现这一点?

提前致谢。

4

2 回答 2

3

您需要在绘图中的 x 和 y 范围的限制之前定义,并且您需要对每个绘图具有相同的限制。

var1S <- c("2012-09-01", "2012-09-05", "2012-09-06", "2012-09-07", "2012-09-08", "2012-09-11")
var1S <- as.Date(var1S, "%Y-%m-%d")
var1T <- c(33.6,32.0,30.0,30.0,30.0,28.4)
var2S <- c("2012-09-03", "2012-09-05", "2012-09-09", "2012-09-11", "2012-09-14", "2012-09-15")
var2S <- as.Date(var2S, "%Y-%m-%d")
var2T <- c(15.6,23.0,19.0,22.0,34.0,29.4)
plot(var1T ~ var1S, type="l",col="red", xlim=range(c(var1S,var2S)), ylim=range(c(var1T,var2T)))
par(new=T)
plot(var2T ~ var2S, type="l",col="green", xlim=range(c(var1S,var2S)), ylim=range(c(var1T,var2T)))
于 2013-01-17T16:28:34.953 回答
0
plot(var1T ~ var1S, type="l",col="red", xlim=c(min(var1S, var2S), max(var1S, var2S)), ylim=c(min(var1T, var2T), max(var1T, var2T)), xlab=NA, ylab=NA)
par(new=TRUE)
plot(var2T~ var2S, type="l", col="green", xlim=c(min(var1S, var2S), max(var1S, var2S)), ylim=c(min(var1T, var2T), max(var1T, var2T)), xlab=NA, ylab=NA)
于 2013-01-17T16:53:05.010 回答