[R] Attempting to plot two different time series together

clark richards clark.richards at gmail.com
Fri Nov 6 20:42:42 CET 2015


Hi Ezra,

Since each dataset is for a different variable, I think your best bet is to
make a "two-panel" plot, but stacked so the time axes line up. This doesn't
require anything fancy to do with the fact the series are sampled at
different intervals. You may need to decimate or interpolate for other
analysis, but at least for plotting, I'd do something like the following.

Note my example uses the `oce` package both for specific plotting methods
(met and sealevel objects) as well as a couple of example datasets. For the
wind plot, I wrote a simple little function that makes an "arrow" plot,
where the length of the arrow is the wind speed and the angle is the
direction (be careful of meteorological vs oceanographic convention on
direction):

library(oce)
data(sealevelTuktoyaktuk)
data(met)

windArrow <- function(x, scale=1, xlim, ylim, ylab, type=1, cex=0.1, ...) {
    oce.plot.ts(x[['time']], x[['v']], type='n',
                ylab=if (missing(ylab)) 'Wind speed' else ylab,
                xlim=xlim, ylim=ylim, xaxs='i')
    drawDirectionField(x[['time']], rep(0, length(x[['time']])),
                       x[['u']], x[['v']], scaley=scale, add=TRUE,
                       type=type, cex=cex, ...)
}

## change the met times to line up with sealevel
met[['time']] <- met[['time']] - met[['time']][1] +
sealevelTuktoyaktuk[['time']][1]

par(mfrow=c(2, 1))
windArrow(met)
plot(sealevelTuktoyaktuk, which=1, xlim=range(met[['time']]))



> Message: 5
> Date: Thu, 5 Nov 2015 09:00:18 -0600
> From: Ezra Boyd <ezgis76 at gmail.com>
> To: r-help at r-project.org
> Subject: [R] Attempting to plot two different time series together
> Message-ID:
>         <CAKa8D7RVg-TfD0PipNA2Q9LixahLMF21cEQ0axHYq1KyWH6=
> Qg at mail.gmail.com>
> Content-Type: text/plain; charset="UTF-8"
>
> Hello,
>
> I am trying to create a plot (I guess two plots) using two different time
> series datasets, but I'm not sure of the best approach.  The data is from a
> tidal surge due to a hurricane and I would like to show the relationship
> between stage and windspeed/direction.
>
> One dataset is the tidal stage and it is sampled in 30 minutes intervals.
> The other dataset is windspeed and direction and it is sampled every 6
> minutes.  I would like to display the tidal hydrograph and then also show
> windspeed and direction as a banner above it.  They would be two separate
> plots, but with identical x-axis (as opposed to one plot that has both tide
> & wind.)
>
> Should I combine the two time series into one dataframe (with lots of NAs)
> so that I can create the plots together, for example using pairs or
> ggpairs? Or should I keep them separate, make the individual plots, and
> then work on the layout in graphic design software? I would prefer the
> former, but I just can't figure out how to make that work out.
>
> Also, is it possible to depict wind direction (which is given in degree
> clockwise from due north) with an arrow?
>
> Thank you very much,
>
> Ezra
>
> --
> Ezra Boyd, PhD
> DisasterMap.net, LLC <http://DisasterMap.net>
> ezgis <ezgis76 at gmail.com>76 at gmail.com <ezgis76 at gmail.com>
> (504)533-4447
>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list