我正在尝试将两个时间序列绘制到R中的相同图形中。
我的数据如下:
系列1:
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中完成这项工作?
提前致谢。
答案 0 :(得分: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)))
答案 1 :(得分: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)