0

I have this dataframe, df that represents the locations of 3 people:

df <- read.csv(text = "name,lat,long
Tom,42,-73
Sally,41,-72
Harry,41,-74")

I have some code that produces a map per person, centred on the person's location:

Map(function(x, y, z) {
  ggmap(get_map(location = c(lon = x, lat = y), zoom = 15, color = "color", source = "google", scale = 2),size = c(1900, 1900), extent = "device") +
    geom_point(data = df[z,], aes(x = long, y = lat), color = "red") +
    ggtitle(df$name[z]) +
    theme(plot.title = element_text(hjust = 0.5)) 
}, df$long, df$lat, seq_along(df))

This works just fine for producing the maps in the R console, but I'd like to save each one out using ggsave. I'm unsure how to do this, although I have tried to specify as high-res, 1900 x 1900 and scale = 2 . I'd also like to name each of the files with the corresponding value of df$name and save into a directory relative to the working directory of /Maps/ .

nycrefugee
  • 1,629
  • 1
  • 10
  • 23
  • Why not simplify your code to be a function for a single map and then use `ggsave` on what is returned? – Kevin Arseneau Sep 12 '17 at 22:25
  • my first thought is because of the practicality of having a far greater number of observations in the frame. I will want to use this for several hundred observations. – nycrefugee Sep 12 '17 at 22:28
  • 1
    The approach in this [accepted answer](https://stackoverflow.com/questions/20500706/saving-multiple-ggplots-from-ls-into-one-and-separate-files-in-r) seems to address your requirement. This question may be a duplicate. – Kevin Arseneau Sep 12 '17 at 22:36
  • You're correct @kevin.arseneau - I managed to use this other response. – nycrefugee Sep 13 '17 at 09:33

0 Answers0