0

I have a map

library(maps)       
library(mapdata)    
map('worldHires', c('Ireland', 'UK'), xlim=c(-16,-5.5), ylim=c(51,56))    

And I have some tracking data that I can plot according to the identify of the animal ID

lat <- c(-11.385668, -11.389855,-12.142785,-11.94954,-11.17716, -10.456175)
lon <- c(53.543667, 53.561507, 52.687934, 52.855068, 52.803291, 52.858737)
ID <- c("A","A","B","B","C","C")
df = data.frame(lat, lon, ID);df

op <- par(mfrow = c(1,3))
sapply(split(df[1:2], df$ID), plot)

I'd like to be able to set up a function so that the original map is set down as a base layer for each of the 3 individual tracks.

adkane
  • 1,429
  • 14
  • 29

1 Answers1

1

Your question is not very clear on your final desired result. I found the ggmap library very useful for mapping problems. Here is an attempt to give some guidance for your problem. The geom_path function is very useful for connecting a series of locations. In this example I have only connected the points for ID==B, it should be simple to connect the remaining ID together as necessary.

#Sample Data
lat <- c(-11.385668, -11.389855,-12.142785,-11.94954,-11.17716, -10.456175)
lon <- c(53.543667, 53.561507, 52.687934, 52.855068, 52.803291, 52.858737)
ID <- c("A","A","B","B","C","C")
df = data.frame(lat, lon, ID)

library(ggmap)
library(RColorBrewer)

#locate the center of the map
center<-c(mean(range(df$lon)), mean(range(df$lat)))
#in this case zoom is set by trial and error
mymap<-qmap(location = center, zoom = 8, maptype= "terrain")
mymap<-mymap + geom_point(aes(x=lon, y=lat, color=ID), data=df)
mymap<-mymap + scale_size(range = c(2, 4)) + scale_color_brewer(palette = "Set1")
mymap<-mymap + geom_path(aes(x=lon, y=lat), data=df) 

mymap<-mymap + facet_wrap(~ID, nrow =2)
print(mymap)

You may need to see this question in order to get ggplot2 and ggmap to work correctly: ggmap Error: GeomRasterAnn was built with an incompatible version of ggproto

Community
  • 1
  • 1
Dave2e
  • 22,192
  • 18
  • 42
  • 50
  • Sorry, I should have been clearer. The idea is to create a panel plot with one map for each animal ID rather than them all on one map. I tried this, and for some reason it works on my full dataset but not the sample I provided! `mapFunc <- function(data) { map('worldHires', c('Ireland', 'UK'), xlim=c(-16,-5.5), ylim=c(51,56)) points(data$lon,data$lat,pch=16, cex=5, map.axes(cex.axis=0.8),title("Storm Petrels"), xlab="longitude",ylab="latitude") }` – adkane Dec 15 '16 at 19:18
  • @Manassa, Ok got it, I added facet_wrap function into the plot creation and it separated out. – Dave2e Dec 16 '16 at 00:51