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

时间:2023-01-28 22:59:17

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个单独轨道中每个轨道的基础层。

1 个解决方案

#1


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.

您的最终预期结果不是很明确。我发现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)

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版本构建的

#1


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.

您的最终预期结果不是很明确。我发现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)

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版本构建的