[英]How can I add a base map to data split by a factor level?
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 我有一些跟踪数据,可以根据动物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. 我希望能够设置一个功能,以便将原始地图设置为3条单独轨道中每条轨道的基础层。
Your question is not very clear on your final desired result. 您对最终期望的结果的疑问不是很清楚。 I found the ggmap library very useful for mapping problems.
我发现ggmap库对于映射问题非常有用。 Here is an attempt to give some guidance for your problem.
这是尝试为您的问题提供一些指导。 The geom_path function is very useful for connecting a series of locations.
geom_path函数对于连接一系列位置非常有用。 In this example I have only connected the points for ID==B, it should be simple to connect the remaining ID together as necessary.
在此示例中,我仅连接了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)
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 您可能需要查看此问题才能使ggplot2和ggmap正常工作: ggmap错误:GeomRasterAnn是使用ggproto的不兼容版本构建的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.