I recently finished a project that involved taking a dataset that covered 15 years and making each year a slide. I manually converted each year from the data into a plot. I am convinced that I could have, given the knowledge, written a loop or a function that would have done this in a fraction of the time. Especially due to the fact that stakeholders kept changing their minds, which necessitated a whole 'nother round of coding.
Here's the complete r code I was using:
> packages <- c(“ggplot2”, “rgdal”, “scales”, “ggmap”, “dplyr”, “Cairo”, “maptools”, “gpclib”)
> lapply(packages, require, character.only = TRUE)
> gpclibPermit()
## read in shapefiles; subset to state level
> us <- readOGR(dsn = “Data”, layer = “cb_2015_us_county_500k”)
> state <- subset(us, STATEFP == 41)
> state <- fortify(state, region = “NAME”)
> county <- readOGR(dsn = “Data”, layer = “cb_2015_us_county_500k”)
> county <- subset(county, STATEFP == 41)
> county <- fortify(county, region = “NAME”)
## read in data; attach to shapefile
> data <- read.csv(file = “Data/data.csv”, head = TRUE, sep = “,”)
> data$id <- as.character(data$id)
> plotData_n <- left_join(state, data)
## create plot for individual year; save as png; REPEAT FOR EACH YEAR
map2001 <- ggplot() +
+ geom_polygon(data = plotData, aes(x = long, y = lat, group = group, fill = y2001)) +
+ geom_polygon(data = county, aes(x = long, y = lat, group = group), fill = NA, color = “black”, size = 0.25) +
+ coord_map() +
+ scale_fill_distiller(palette = “Blues”, name = “2001”, direction = 1, breaks = with(plotData, c(min(y2001), mean(y2001), max(y2001))), labels = c(“”, “Avg.”, “”)) +
+ remAxes <- theme_nothing(legend = TRUE) +
+ addTitle <- labs(title = “Rate of blah, blah, blah”, fill = “”)
> ggsave(map2001, file = "map2001.png", width = 5, height = 4.5, type = "cairo-png")
How would I go about automating this process in r? How would I write a function or loop that would do this and return an object that I could then use to save the image?
Here's a dput() of the top 50 lines of the data:
structure(list(long = c(-118.518058, -118.517858, -118.518357,
-118.516857, -118.517157, -118.519257, -118.518557, -118.510057,
-118.504057, -118.502169, -118.501909, -118.49966, -118.498453,
-118.498245, -118.498157, -118.497757, -118.497408, -118.497257,
-118.496857, -118.496457, -118.495957, -118.495804, -118.495368,
-118.492357, -118.491357, -118.488257, -118.483757, -118.476457,
-118.472357, -118.470257, -118.468257, -118.464256, -118.463556,
-118.461656, -118.460656, -118.461657, -118.460357, -118.459157,
-118.449256, -118.445556, -118.443056, -118.437956, -118.433856,
-118.431756, -118.430856, -118.424256, -118.422756, -118.419156,
-118.420056, -118.417856), lat = c(44.693275, 44.695275, 44.698574,
44.701974, 44.703774, 44.706374, 44.706674, 44.706774, 44.706474,
44.707169, 44.708337, 44.70918, 44.710691, 44.710785, 44.710874,
44.711174, 44.711312, 44.711374, 44.711774, 44.712074, 44.712474,
44.712574, 44.712866, 44.713774, 44.712574, 44.711074, 44.710074,
44.709174, 44.704774, 44.704274, 44.705674, 44.706274, 44.707274,
44.707374, 44.709174, 44.711774, 44.712874, 44.715774, 44.720973,
44.723473, 44.724273, 44.721773, 44.720873, 44.721273, 44.719373,
44.718774, 44.719573, 44.718674, 44.721873, 44.722873), order = 1:50,
hole = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), piece = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = c("1", "2", "3", "4", "5"), class = "factor"),
id = c("Baker", "Baker", "Baker", "Baker", "Baker", "Baker",
"Baker", "Baker", "Baker", "Baker", "Baker", "Baker", "Baker",
"Baker", "Baker", "Baker", "Baker", "Baker", "Baker", "Baker",
"Baker", "Baker", "Baker", "Baker", "Baker", "Baker", "Baker",
"Baker", "Baker", "Baker", "Baker", "Baker", "Baker", "Baker",
"Baker", "Baker", "Baker", "Baker", "Baker", "Baker", "Baker",
"Baker", "Baker", "Baker", "Baker", "Baker", "Baker", "Baker",
"Baker", "Baker"), group = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("Baker.1", "Benton.1", "Clackamas.1", "Clatsop.1",
"Clatsop.2", "Clatsop.3", "Clatsop.4", "Clatsop.5", "Columbia.1",
"Coos.1", "Crook.1", "Curry.1", "Curry.2", "Curry.3", "Curry.4",
"Deschutes.1", "Douglas.1", "Gilliam.1", "Grant.1", "Harney.1",
"Hood River.1", "Jackson.1", "Jefferson.1", "Josephine.1",
"Klamath.1", "Lake.1", "Lane.1", "Lincoln.1", "Linn.1", "Malheur.1",
"Marion.1", "Morrow.1", "Multnomah.1", "Polk.1", "Sherman.1",
"Tillamook.1", "Umatilla.1", "Union.1", "Wallowa.1", "Wasco.1",
"Washington.1", "Wheeler.1", "Yamhill.1"), class = "factor"),
y2001 = c(0.467065868, 0.467065868, 0.467065868, 0.467065868,
0.467065868, 0.467065868, 0.467065868, 0.467065868, 0.467065868,
0.467065868, 0.467065868, 0.467065868, 0.467065868, 0.467065868,
0.467065868, 0.467065868, 0.467065868, 0.467065868, 0.467065868,
0.467065868, 0.467065868, 0.467065868, 0.467065868, 0.467065868,
0.467065868, 0.467065868, 0.467065868, 0.467065868, 0.467065868,
0.467065868, 0.467065868, 0.467065868, 0.467065868, 0.467065868,
0.467065868, 0.467065868, 0.467065868, 0.467065868, 0.467065868,
0.467065868, 0.467065868, 0.467065868, 0.467065868, 0.467065868,
0.467065868, 0.467065868, 0.467065868, 0.467065868, 0.467065868,
0.467065868), y2002 = c(0.383337472, 0.383337472, 0.383337472,
0.383337472, 0.383337472, 0.383337472, 0.383337472, 0.383337472,
0.383337472, 0.383337472, 0.383337472, 0.383337472, 0.383337472,
0.383337472, 0.383337472, 0.383337472, 0.383337472, 0.383337472,
0.383337472, 0.383337472, 0.383337472, 0.383337472, 0.383337472,
0.383337472, 0.383337472, 0.383337472, 0.383337472, 0.383337472,
0.383337472, 0.383337472, 0.383337472, 0.383337472, 0.383337472,
0.383337472, 0.383337472, 0.383337472, 0.383337472, 0.383337472,
0.383337472, 0.383337472, 0.383337472, 0.383337472, 0.383337472,
0.383337472, 0.383337472, 0.383337472, 0.383337472, 0.383337472,
0.383337472, 0.383337472), y2003 = c(0.769961724, 0.769961724,
0.769961724, 0.769961724, 0.769961724, 0.769961724, 0.769961724,
0.769961724, 0.769961724, 0.769961724, 0.769961724, 0.769961724,
0.769961724, 0.769961724, 0.769961724, 0.769961724, 0.769961724,
0.769961724, 0.769961724, 0.769961724, 0.769961724, 0.769961724,
0.769961724, 0.769961724, 0.769961724, 0.769961724, 0.769961724,
0.769961724, 0.769961724, 0.769961724, 0.769961724, 0.769961724,
0.769961724, 0.769961724, 0.769961724, 0.769961724, 0.769961724,
0.769961724, 0.769961724, 0.769961724, 0.769961724, 0.769961724,
0.769961724, 0.769961724, 0.769961724, 0.769961724, 0.769961724,
0.769961724, 0.769961724, 0.769961724), y2004 = c(0.674740033,
0.674740033, 0.674740033, 0.674740033, 0.674740033, 0.674740033,
0.674740033, 0.674740033, 0.674740033, 0.674740033, 0.674740033,
0.674740033, 0.674740033, 0.674740033, 0.674740033, 0.674740033,
0.674740033, 0.674740033, 0.674740033, 0.674740033, 0.674740033,
0.674740033, 0.674740033, 0.674740033, 0.674740033, 0.674740033,
0.674740033, 0.674740033, 0.674740033, 0.674740033, 0.674740033,
0.674740033, 0.674740033, 0.674740033, 0.674740033, 0.674740033,
0.674740033, 0.674740033, 0.674740033, 0.674740033, 0.674740033,
0.674740033, 0.674740033, 0.674740033, 0.674740033, 0.674740033,
0.674740033, 0.674740033, 0.674740033, 0.674740033), y2005 = c(0.712707334,
0.712707334, 0.712707334, 0.712707334, 0.712707334, 0.712707334,
0.712707334, 0.712707334, 0.712707334, 0.712707334, 0.712707334,
0.712707334, 0.712707334, 0.712707334, 0.712707334, 0.712707334,
0.712707334, 0.712707334, 0.712707334, 0.712707334, 0.712707334,
0.712707334, 0.712707334, 0.712707334, 0.712707334, 0.712707334,
0.712707334, 0.712707334, 0.712707334, 0.712707334, 0.712707334,
0.712707334, 0.712707334, 0.712707334, 0.712707334, 0.712707334,
0.712707334, 0.712707334, 0.712707334, 0.712707334, 0.712707334,
0.712707334, 0.712707334, 0.712707334, 0.712707334, 0.712707334,
0.712707334, 0.712707334, 0.712707334, 0.712707334), y2006 = c(0.727825718,
0.727825718, 0.727825718, 0.727825718, 0.727825718, 0.727825718,
0.727825718, 0.727825718, 0.727825718, 0.727825718, 0.727825718,
0.727825718, 0.727825718, 0.727825718, 0.727825718, 0.727825718,
0.727825718, 0.727825718, 0.727825718, 0.727825718, 0.727825718,
0.727825718, 0.727825718, 0.727825718, 0.727825718, 0.727825718,
0.727825718, 0.727825718, 0.727825718, 0.727825718, 0.727825718,
0.727825718, 0.727825718, 0.727825718, 0.727825718, 0.727825718,
0.727825718, 0.727825718, 0.727825718, 0.727825718, 0.727825718,
0.727825718, 0.727825718, 0.727825718, 0.727825718, 0.727825718,
0.727825718, 0.727825718, 0.727825718, 0.727825718), y2007 = c(0.701523911,
0.701523911, 0.701523911, 0.701523911, 0.701523911, 0.701523911,
0.701523911, 0.701523911, 0.701523911, 0.701523911, 0.701523911,
0.701523911, 0.701523911, 0.701523911, 0.701523911, 0.701523911,
0.701523911, 0.701523911, 0.701523911, 0.701523911, 0.701523911,
0.701523911, 0.701523911, 0.701523911, 0.701523911, 0.701523911,
0.701523911, 0.701523911, 0.701523911, 0.701523911, 0.701523911,
0.701523911, 0.701523911, 0.701523911, 0.701523911, 0.701523911,
0.701523911, 0.701523911, 0.701523911, 0.701523911, 0.701523911,
0.701523911, 0.701523911, 0.701523911, 0.701523911, 0.701523911,
0.701523911, 0.701523911, 0.701523911, 0.701523911), y2008 = c(0.799278072,
0.799278072, 0.799278072, 0.799278072, 0.799278072, 0.799278072,
0.799278072, 0.799278072, 0.799278072, 0.799278072, 0.799278072,
0.799278072, 0.799278072, 0.799278072, 0.799278072, 0.799278072,
0.799278072, 0.799278072, 0.799278072, 0.799278072, 0.799278072,
0.799278072, 0.799278072, 0.799278072, 0.799278072, 0.799278072,
0.799278072, 0.799278072, 0.799278072, 0.799278072, 0.799278072,
0.799278072, 0.799278072, 0.799278072, 0.799278072, 0.799278072,
0.799278072, 0.799278072, 0.799278072, 0.799278072, 0.799278072,
0.799278072, 0.799278072, 0.799278072, 0.799278072, 0.799278072,
0.799278072, 0.799278072, 0.799278072, 0.799278072), y2009 = c(0.780469272,
0.780469272, 0.780469272, 0.780469272, 0.780469272, 0.780469272,
0.780469272, 0.780469272, 0.780469272, 0.780469272, 0.780469272,
0.780469272, 0.780469272, 0.780469272, 0.780469272, 0.780469272,
0.780469272, 0.780469272, 0.780469272, 0.780469272, 0.780469272,
0.780469272, 0.780469272, 0.780469272, 0.780469272, 0.780469272,
0.780469272, 0.780469272, 0.780469272, 0.780469272, 0.780469272,
0.780469272, 0.780469272, 0.780469272, 0.780469272, 0.780469272,
0.780469272, 0.780469272, 0.780469272, 0.780469272, 0.780469272,
0.780469272, 0.780469272, 0.780469272, 0.780469272, 0.780469272,
0.780469272, 0.780469272, 0.780469272, 0.780469272), y2010 = c(0.727604813,
0.727604813, 0.727604813, 0.727604813, 0.727604813, 0.727604813,
0.727604813, 0.727604813, 0.727604813, 0.727604813, 0.727604813,
0.727604813, 0.727604813, 0.727604813, 0.727604813, 0.727604813,
0.727604813, 0.727604813, 0.727604813, 0.727604813, 0.727604813,
0.727604813, 0.727604813, 0.727604813, 0.727604813, 0.727604813,
0.727604813, 0.727604813, 0.727604813, 0.727604813, 0.727604813,
0.727604813, 0.727604813, 0.727604813, 0.727604813, 0.727604813,
0.727604813, 0.727604813, 0.727604813, 0.727604813, 0.727604813,
0.727604813, 0.727604813, 0.727604813, 0.727604813, 0.727604813,
0.727604813, 0.727604813, 0.727604813, 0.727604813), y2011 = c(0.626036585,
0.626036585, 0.626036585, 0.626036585, 0.626036585, 0.626036585,
0.626036585, 0.626036585, 0.626036585, 0.626036585, 0.626036585,
0.626036585, 0.626036585, 0.626036585, 0.626036585, 0.626036585,
0.626036585, 0.626036585, 0.626036585, 0.626036585, 0.626036585,
0.626036585, 0.626036585, 0.626036585, 0.626036585, 0.626036585,
0.626036585, 0.626036585, 0.626036585, 0.626036585, 0.626036585,
0.626036585, 0.626036585, 0.626036585, 0.626036585, 0.626036585,
0.626036585, 0.626036585, 0.626036585, 0.626036585, 0.626036585,
0.626036585, 0.626036585, 0.626036585, 0.626036585, 0.626036585,
0.626036585, 0.626036585, 0.626036585, 0.626036585), y2012 = c(0.59350608,
0.59350608, 0.59350608, 0.59350608, 0.59350608, 0.59350608,
0.59350608, 0.59350608, 0.59350608, 0.59350608, 0.59350608,
0.59350608, 0.59350608, 0.59350608, 0.59350608, 0.59350608,
0.59350608, 0.59350608, 0.59350608, 0.59350608, 0.59350608,
0.59350608, 0.59350608, 0.59350608, 0.59350608, 0.59350608,
0.59350608, 0.59350608, 0.59350608, 0.59350608, 0.59350608,
0.59350608, 0.59350608, 0.59350608, 0.59350608, 0.59350608,
0.59350608, 0.59350608, 0.59350608, 0.59350608, 0.59350608,
0.59350608, 0.59350608, 0.59350608, 0.59350608, 0.59350608,
0.59350608, 0.59350608, 0.59350608, 0.59350608), y2013 = c(0.573933154,
0.573933154, 0.573933154, 0.573933154, 0.573933154, 0.573933154,
0.573933154, 0.573933154, 0.573933154, 0.573933154, 0.573933154,
0.573933154, 0.573933154, 0.573933154, 0.573933154, 0.573933154,
0.573933154, 0.573933154, 0.573933154, 0.573933154, 0.573933154,
0.573933154, 0.573933154, 0.573933154, 0.573933154, 0.573933154,
0.573933154, 0.573933154, 0.573933154, 0.573933154, 0.573933154,
0.573933154, 0.573933154, 0.573933154, 0.573933154, 0.573933154,
0.573933154, 0.573933154, 0.573933154, 0.573933154, 0.573933154,
0.573933154, 0.573933154, 0.573933154, 0.573933154, 0.573933154,
0.573933154, 0.573933154, 0.573933154, 0.573933154), y2014 = c(0.644163844,
0.644163844, 0.644163844, 0.644163844, 0.644163844, 0.644163844,
0.644163844, 0.644163844, 0.644163844, 0.644163844, 0.644163844,
0.644163844, 0.644163844, 0.644163844, 0.644163844, 0.644163844,
0.644163844, 0.644163844, 0.644163844, 0.644163844, 0.644163844,
0.644163844, 0.644163844, 0.644163844, 0.644163844, 0.644163844,
0.644163844, 0.644163844, 0.644163844, 0.644163844, 0.644163844,
0.644163844, 0.644163844, 0.644163844, 0.644163844, 0.644163844,
0.644163844, 0.644163844, 0.644163844, 0.644163844, 0.644163844,
0.644163844, 0.644163844, 0.644163844, 0.644163844, 0.644163844,
0.644163844, 0.644163844, 0.644163844, 0.644163844), y2015 = c(0.665388683,
0.665388683, 0.665388683, 0.665388683, 0.665388683, 0.665388683,
0.665388683, 0.665388683, 0.665388683, 0.665388683, 0.665388683,
0.665388683, 0.665388683, 0.665388683, 0.665388683, 0.665388683,
0.665388683, 0.665388683, 0.665388683, 0.665388683, 0.665388683,
0.665388683, 0.665388683, 0.665388683, 0.665388683, 0.665388683,
0.665388683, 0.665388683, 0.665388683, 0.665388683, 0.665388683,
0.665388683, 0.665388683, 0.665388683, 0.665388683, 0.665388683,
0.665388683, 0.665388683, 0.665388683, 0.665388683, 0.665388683,
0.665388683, 0.665388683, 0.665388683, 0.665388683, 0.665388683,
0.665388683, 0.665388683, 0.665388683, 0.665388683)), .Names = c("long",
"lat", "order", "hole", "piece", "id", "group", "y2001", "y2002",
"y2003", "y2004", "y2005", "y2006", "y2007", "y2008", "y2009",
"y2010", "y2011", "y2012", "y2013", "y2014", "y2015"), row.names = c(NA,
50L), class = "data.frame")