Barnaby Barnaby - 2 months ago 8
R Question

Plot two series with the same scale of different lengths

Plot two series with different lengths x=61 data instances and y=46

x<-c(0.1800, 0.1000, -0.0560 -0.1400, -0.0420, 0.0060, -0.0460, -0.0060, 0.0190,
-0.0420, 0.0610, 0.0530, 0.0610, -0.0040, -0.0700, -0.0450, -0.0250, 0.3120,
-0.0540, 0.1400, 0.0730, 0.0840, -0.0170, -0.0530, -0.1340, 0.2440, 0.0380,
-0.0260, 0.1080, 0.0440, 0.0920, 0.0060, 0.1260, 0.2690, -0.0710, 0.1780,
0.1680, 0.0650, 0.0150, 0.0420, -0.0480, 0.2750, 0.0760, 0.0580, 0.0360,
-0.0290, 0.0772, -0.0896, -0.0831, -0.2352, 0.0239, -0.0571, 0.1050, 0.0694,
-0.0423, 0.0158, 0.0793, -0.0546, -0.0351, -0.0388, -0.0137)

y<-c(0.4973210021, 0.3171289371, 0.3524071954, 0.5445361214, 0.5045483757,
0.4791933558, 0.5961864772, 0.6655933714, 0.6616273005, 0.5633990851,
0.2486985634, 0.2624092943, 0.4624224429, 0.3630349240, 0.3613897096,
0.4500961318, 0.6074131070, 0.4924340721, 0.4887700647, 0.3886799286,
0.6301570294, 0.7013215936, 0.7296446438, 0.6717751410, 0.5550540756,
0.3225643827, 0.2157955431, -0.1095368716, 0.3128888137, 0.3291106325,
0.3341775845, 0.3872107617, 0.4021650438, 0.5742174861, 0.9327782888,
0.5588426942, 0.7909656688, 0.8510349467, 0.7665466023, 0.8021267456,
0.8162197984, 0.9820833976 , 0.8577352252, 0.6558680486, 0.6534645172,
0.6546571333)


Would like to plot the two series in one chart starting y (the shorter series) after 15 data instances of x (both series end up at the same time) without missing the first 15 data instances of x in the chart.

Thank you

Bernabe

Answer

Create time series from each and then plot them:

x.ts <- ts(x)
y.ts <- ts(y, start = 15)
ts.plot(x.ts, y.ts, col = 1:2, lty = 1:2)

legend("topleft", c("x", "y"), col = 1:2, lty = 1:2) # optional

enter image description here

Comments