Consider the following:
set.seed(1)
RandData <- rnorm(100,sd=20)
Locations <- rep(c('England','Wales'),each=50)
today <- Sys.Date()
dseq <- (seq(today, by = "1 days", length = 100))
Date <- as.POSIXct(dseq, format = "%Y-%m-%d")
Final <- cbind(Loc = Locations, Doy = as.numeric(format(Date,format = "%j")), Temp = RandData)
In this example how is it possible to produce two plots in the same figure window, where the first plot shows the temperature in England against Doy and the second shows temperature in Wales against Doy?
Note that your data is a character matrix. Better if the
Finalobject is created via:With that, the code below draws two plots on the one window, side by side. I use the formula interface to
plot()to make use of it’ssubsetargument, which works like thesubset()function.Which produces this plot:
If you want them both on the same scale then we modify it a bit:
which produces this version of the plot
For a line-plot you’d need to get the data in
Doyorder and then addtype = "l"to theplot()calls.For completeness, @Justin has shown how to use one of the high level plotting packages to achieve something similar but with less user-effort via ggplot2. The lattice package is another major high-level plotting package in R. You can achieve the same plot using lattice via:
The latter produces
Use
type = "p"for just points andtype = "l"for just lines. As you can see, the higher-level packages make producing these plots a bit easier than with the base graphics package.