3

I have point data for a number of individuals and generate maps for each individual using a for loop. Some individuals travel much further than others on a daily basis so the extent/zoom for each individual needs to be different. Within a loop I have been using the centroid for each individual to generate a map with qmap but need to also get a unique extent/zoom for each individual.

I have generated a reproducible example below where IndID BBB has a small home range (does not travel far) compared to IndID AAA, who is wide ranging. For each individual I export a map, although below I have written the resulting maps to a list object for reproducibility sake.

library(ggmap)
set.seed(123)
dat1 <- data.frame(IndID = rep("AAA", 100),
                   Lat = sample(seq(45.500,45.900, 0.001), 100,  replace = T),
                   Long = sample(seq(111.2,111.805, 0.001), 100, replace = T)*-1)


dat2 <- data.frame(IndID = rep("BBB", 100),
                   Lat = sample(seq(45.655,45.700, 0.001), 100,  replace = T),
                   Long = sample(seq(111.4,111.5, 0.001), 100, replace = T)*-1)


dat <- rbind(dat1, dat2)

###Look at the Points
MeanLat <- mean(dat$Lat, na.rm = TRUE) 
MeanLon <- mean(dat$Long, na.rm = TRUE) 

BaseMap <- qmap(location = c(lon = MeanLon, lat = MeanLat), zoom = 10,  maptype = "terrain")

BaseMap + geom_point(aes(x = Long, y = Lat, color = IndID), data = dat, size = 4)+
  scale_colour_manual(values = c("darkblue","red"),drop=FALSE) 

###Make list and run loop
Maps <- list()

for( i in unique(dat$IndID)){
  #Subset data to IndID i
  IndDat <- subset(dat, IndID == i)

  #Get mean lat and long for IndID i
  MeanLat <- mean(IndDat$Lat, na.rm = TRUE) 
  MeanLon <- mean(IndDat$Long, na.rm = TRUE) 

  #Get base map
  BaseMap <- qmap(location = c(lon = MeanLon, lat = MeanLat), zoom = 10,  maptype = "terrain")

  #plot points on base map 
  FnlMap <- BaseMap + geom_point(aes(x = Long, y = Lat, color = IndID), data = IndDat, size = 4)+
    scale_colour_manual(values = c("darkblue","red"),drop=FALSE) 

  #Store in list object
  Maps[[i]] <- FnlMap
}

Maps$AAA
Maps$BBB

In this instance the zoom argument to qmap is set to 10. This is appropriate for AAA, but not for BBB, which would be better viewed with a zoom of 13.

I am generating maps for many individuals and want to define an appropriate zoom/extent for each individual within the loop. Rather than relying on the zoom argument I wanted to use the min and max lat and long values to set the extent but this does not seem possible in qmap.

Any suggestions would be appreciated.

aosmith
  • 34,856
  • 9
  • 84
  • 118
B. Davis
  • 3,391
  • 5
  • 42
  • 78

0 Answers0