简体   繁体   中英

How Do I plot 2 pairs of variables on one plot in R?

Suppose I have V1, V2 and V3, V4. V1 and V2 form a time series, and V3 and V4 form a time series. V1 and V3 are the time variables and V2 and V4 are the dependent variables of same units. V1 and V3 are periods that overlap eachother, but they don't start/end at the same points.

First I plotted V1 and V2 with, plot(V1,V2, type="l") . But how would I plot these 2 pairs of variables on the same axis? I would like to merge V1 and V2 together into a data table and do the same with V3 and V4, calling them D1 and D2 respectively. Then I can maybe use plot(D1,D2..), but I don't know how to merge variables like that.

Update:

V1= c(6,7,8,9,10,11,12,13,14,15,16,17,18)
V2= c(27,53,68,45,75,35,72,35,25,27,27,26,52)
V3= c(2,3,4,5,6)
V4=c(54,23,86,43,26)
plot(V1,V2, type="l")

Suppose I have V1, V2 and V3, V4. V1 and V2 form a time series, and V3 and V4 form a time series. V1 and V3 are the time variables and V2 and V4 are the dependent variables of same units. V1 and V3 are periods that overlap eachother, but they don't start/end at the same points. ... [H]ow would I plot these 2 pairs of variables on the same axis?

You can do

set.seed(1)
V1 <- 1:10
V2 <- runif(length(V1))
V3 <- 3:7
V4 <- runif(length(V3))
plot(V1, V2, type="l", xlim = range(c(V1, V3)), ylim = range(c(V2, V4)))
lines(V3, V4, lty = "dashed")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM