-1

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")
Nate P
  • 21
  • 6
  • Please give a sample of your data with `dput()` so that your problem is reproducible. This will help people help you. – Richard Telford Jan 08 '17 at 09:28
  • Also any attempt would have been welcome. look any tuto about loop in R – timat Jan 08 '17 at 10:00
  • My data are observations appended to a shapefile, so there are about 16,000 indv. data points. I'm appending my .Names. – Nate P Jan 08 '17 at 10:02
  • @NateP http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – timat Jan 08 '17 at 10:51
  • 1
    You could always supply us with `dput(head(plotData, 50))`, as having the data will get you a better answer. Aside from your loop question, you might also want to look into reshaping your data. – Jake Kaupp Jan 08 '17 at 18:22
  • @timat Thank you for that link! I am trying to become a better SE questioner. – Nate P Jan 08 '17 at 20:37

1 Answers1

2

In programming, when one is thinking of a series of similarly-named variables, it's usually time to combine them somehow into one, perhaps using an array. In R, the appropriate structure for combining complex and/or different variables is a list.

For-loops in R are as simple as for (var in sequence) { loop body with var }.

A simple way to append new data to lists in R is keyed assignment: list[[key]] <- value. We will use years as keys, but will convert them to strings. Otherwise, assignment like list[[2002]] <- data will cause the list to contain 2001 empty elements.

Since your data is stored in data.frame called plotData in columns named y{year}, we will pass the variable column names as strings to aes_string function and will use get function to access data by string variable name inside with calls.

Therefore,

map <- list()
for (year in 2001:2015) {
  year.ch <- as.character(year)
  year.cur <- paste0("y",year)
  map[[year.ch]] <- ggplot() +
    geom_polygon(data = plotData, aes_string(x = "long", y = "lat", group = "group", fill = year.cur)) +
    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 = year.ch, direction = 1, breaks = with(plotData, c(min(get(year.cur)), mean(get(year.cur)), max(get(year.cur)))), labels = c("", "Avg.", "")) +
    theme_nothing(legend = TRUE) +
    labs(title = "Rate of blah blah blah", fill = "")
}
aitap
  • 325
  • 1
  • 9
  • 1
    I don't think this will work because of the non-standard evaluation in `aes`. Perhaps you could get `aes_string` to work. – Richard Telford Jan 08 '17 at 09:42
  • 1
    or `get(year.cur)` might work into the normal aes. or create a temporary variable: plotData$temp_year <- plotData[[year.cur]] at the beggining of the loop and use this variable instead in the aes – timat Jan 08 '17 at 09:59
  • That is an extremely elegant solution, thank you. I will be playing around with it. As is, I keep getting the error: `Error in get(year.cur) : object 'y2001' not found` – Nate P Jan 08 '17 at 10:20
  • @NateP sorry for get, you can try with `eval(year.cur)` instead. But you won't need it with the temporary variable – timat Jan 08 '17 at 10:53
  • 1
    @timat I thought I have removed `get` from `aes` calls. Also, it seems to me now that @NateP's data is inside `plotData` data.frame. I'll fix my answer accordingly. – aitap Jan 08 '17 at 11:00
  • Not only elegant, worked perfectly! Thank you. I wish I had asked before doing each individually. – Nate P Jan 08 '17 at 20:44