Beth Bennett
Beth Bennett

Reputation: 21

Creating vectors of equal length from R dataset and plotting them

R has some built in datasets, namely I'm using "lynx" and "LakeHuron" which are of different lengths. "lynx" contains data on annual lynx trappings from 1821-1934, and "LakeHuron" contains annual water level from 1872-1975.

I need to plot LakeHuron data on the y-axis, and lynx data on the x-axis, but only for the years 1875-1934 inclusive. I created two vectors:

lynx.years = c(lynx)
huron.years = c(LakeHuron)

I am stuck at the point of trying to only make a plot for the specified year range. Can someone help me figure out how to plot the data from the two vectors for only the years 1875-1934?

Thank you!

Upvotes: 1

Views: 216

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 269481

1) The question did not specify what sort of plot was desired so assume it is a two panel plot with one series in each panel with years on the X axis such that only the range of years mentioned is shown. That range of years is the intersection of the years of the two series so:

plot(na.omit(cbind(LakeHuron, lynx)))

Drop the na.omit if you want to plot the entirety of the two series.

2) If what is wanted is to rescale the two series so that their shapes can be shown on a single panel despite vastly different ranges:

ts.plot(scale(na.omit(cbind(LakeHuron, lynx))), col = 1:2)

Again, we could drop the na.omit if the entire series were desired.

3) If what is wanted is to plot one vs. the other then:

plot(unclass(cbind(LakeHuron, lynx)))

Upvotes: 1

Related Questions