如何将基本地图添加到按因子级别划分的数据?

时间:2016-12-14 18:25:30

标签: r plot gis spatial

我有一张地图

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

我有一些跟踪数据可以根据动物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)

我希望能够设置一个功能,以便将原始地图设置为3个单独轨道中每个轨道的基础层。

1 个答案:

答案 0 :(得分:1)

您对最终预期结果的疑问不明确。我发现ggmap库对于映射问题非常有用。这是尝试为您的问题提供一些指导。 geom_path函数对于连接一系列位置非常有用。在这个例子中,我只连接了ID == B的点,根据需要将剩余的ID连接起来应该很简单。

#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)

您可能需要查看此问题才能让ggplot2和ggmap正常工作:ggmap Error: GeomRasterAnn was built with an incompatible version of ggproto