简体   繁体   English

如何将底图添加到按因子级别拆分的数据?

[英]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.

相关问题 我如何在没有循环的情况下通过数据帧中该级别中另一个因子的子集来操作因子级别内的数据 - How can i manipulate data within a factor level by a subset of another factor in that level in a dataframe without loops 如何将按因子级别划分的列表重新组合到原始数据框? - How do I recombine a list split by factor level to the original dataframe? 我的数据有两个标题(变量类型和分组因子)。 如何拆分标题并将分组因子转换为列? - My data has two headers (variable type and grouping factor). How can I split the header and turn the grouping factor into a column? 如何在因子中添加“TOTAL”级别,以便可以在条形图中显示? - How to add a “TOTAL” level in a factor so it can be shown in a barplot? 如何添加列为因子水平比例的列 - How do I add a column that is the proportion of a factor level 在R中,如何访问因子的每个级别的第一个元素? - In R, how can I access the first element of each level of a factor? mgcv:我可以降低因子“ by”变量的水平吗? - mgcv: can I drop a level for factor 'by' variable? 如何将回归线添加到具有多个数据系列的图中,这些数据系列由一个因子进行颜色编码? - How can I add regression lines to a plot that has multiple data series that are colour coded by a factor? 如何按因子将 data.frame 拆分为数组? - How to split a data.frame into an array by a factor? 如何在因子水平之间切换? - How do I switch between factor level?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM