How about this?
require(ggplot2)
require(plyr)
require(gridExtra)
pl <- dlply(df, .(Loc), function(dat) {
ggplot(data = dat, aes(x = Year, y = dir)) + geom_line() +
geom_point() + xlab("x-label") + ylab("y-label") +
geom_smooth(method = "lm")
})
ml <- do.call(marrangeGrob, c(pl, list(nrow = 2, ncol = 2)))
ggsave("my_plots.pdf", ml, height = 7, width = 13, units = "in")
The idea: First split the data by Loc
and create the plot for each subset. The splitting part is done using plyr
function dlply
that basically takes a data.frame
as input and provides a list
as output. The plot element is stored in each element of the list corresponding to the subset. Then, we use gridExtra
package's marrangeGrob
function to arrange multiple plots (which also has the very useful nrow
and ncol
arguments to set the argument). Then, you can save it using ggsave
from ggplot2
.
I'll leave you to any additional tweaks you may require.