简体   繁体   English

如何在R中的一个绘图上绘制2对变量?

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

Suppose I have V1, V2 and V3, V4. 假设我有V1,V2和V3,V4。 V1 and V2 form a time series, and V3 and V4 form a time series. V1和V2形成一个时间序列,而V3和V4形成一个时间序列。 V1 and V3 are the time variables and V2 and V4 are the dependent variables of same units. V1和V3是时间变量,V2和V4是相同单位的因变量。 V1 and V3 are periods that overlap eachother, but they don't start/end at the same points. V1和V3是相互重叠的时间段,但它们不在同一点开始/结束。

First I plotted V1 and V2 with, plot(V1,V2, type="l") . 首先,我用plot(V1,V2, type="l")绘制V1和V2。 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. 我想将V1和V2合并到一个数据表中,并对V3和V4进行相同操作,分别将它们称为D1和D2。 Then I can maybe use plot(D1,D2..), but I don't know how to merge variables like that. 然后我也许可以使用plot(D1,D2 ..),但是我不知道如何合并这样的变量。

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,V2和V3,V4。 V1 and V2 form a time series, and V3 and V4 form a time series. V1和V2形成一个时间序列,而V3和V4形成一个时间序列。 V1 and V3 are the time variables and V2 and V4 are the dependent variables of same units. V1和V3是时间变量,V2和V4是相同单位的因变量。 V1 and V3 are periods that overlap eachother, but they don't start/end at the same points. V1和V3是相互重叠的时间段,但它们不在同一点开始/结束。 ... [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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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