plotting from data frame with 2 fixed variables

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?

Answers


Note that your data is a character matrix. Better if the Final object is created via:

Final <- data.frame(Loc = Locations,
                    Doy = as.numeric(format(Date,format = "%j")),
                    Temp = RandData)

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's subset argument, which works like the subset() function.

ylab <- "Temperature"
xlab <- "Day of year"

layout(matrix(1:2, ncol = 2))
plot(Temp ~ Doy, data = Final, subset = Loc == "England", main = "England",
     ylab = ylab, xlab = xlab)
plot(Temp ~ Doy, data = Final, subset = Loc == "Wales", main = "Wales",
     ylab = ylab, xlab = xlab)
layout(1)

Which produces this plot:

If you want them both on the same scale then we modify it a bit:

ylab <- "Temperature"
xlab <- "Day of year"
xlim <- with(Final, range(Doy))
ylim <- with(Final, range(Temp))
layout(matrix(1:2, ncol = 2))
plot(Temp ~ Doy, data = Final, subset = Loc == "England", main = "England",
     ylab = ylab, xlab = xlab, xlim = xlim, ylim = ylim)
plot(Temp ~ Doy, data = Final, subset = Loc == "Wales", main = "Wales",
     ylab = ylab, xlab = xlab, xlim = xlim, ylim = ylim)
layout(1)

which produces this version of the plot

For a line-plot you'd need to get the data in Doy order and then add type = "l" to the plot() 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:

require(lattice)
xyplot(Temp ~ Doy | Loc, data = Final, type = c("l","p")

The latter produces

Use type = "p" for just points and type = "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.


by using cbind to create your data, they are all coerced to character. instead use data.frame()

Final <- data.frame(Loc = Locations, 
                    Doy = as.numeric(format(Date,format = "%j")), 
                    Temp = RandData)

ggplot does things like this very nicely.

library(ggplot2)
ggplot(Final, aes(x=Doy, y=Temp)) + geom_path() + facet_wrap( ~ Loc)

Or you can use coloring:

ggplot(Final, aes(x=Doy, y=Temp, color=Loc)) + geom_path()

Need Your Help

.addclass is not working when page is loaded

jquery html css load addclass

I am trying to run an animation once my main page is loaded but I have a preloading system that loads and has a loading animation and hides the contents of the main page. This loads all of the sites

Good example of Reactive Extensions Use

c# .net system.reactive

I understand the basics of Rx. Where I'm struggling is how you would actually use this beyond academic examples? What are some common, simple real-world scenarios where Rx is a much better solution