ggmap 应用于数据框列表

问题描述 投票:0回答:0

我想为每个数据框在单独地图上的数据框列表中映射位置。例如,如果我有一个包含 10 个数据框的列表,我希望有 10 个单独的地图,每个地图都有映射位置。这是一个简单的例子:

library(purrr)
library(ggmap)
library(ggplot2)

#Participant1
Lat <- c(1.295824, 1.295824, 1.295826, 1.295828, 1.295830, 1.295830)
Lon <- c(103.8494, 103.8494, 103.8494, 103.8494, 103.8494, 103.8494)
P1 <- data.frame(Lat, Lon)


#Participant2
Lat <- c(1.295995, 1.295977, 1.295995, 1.295992, 1.295987, 1.295992)
Lon <- c(103.8492, 103.8492, 103.8492, 103.8492, 103.8492, 103.8492)
P2 <- data.frame(Lat, Lon)

#Participant3
P3 <- data.frame(Lat, Lon)
Lat <- c(1.295773, 1.295770, 1.295769, 1.295769, 1.295772,  1.295769)
Lon <- c(103.8493, 103.8493, 103.8493, 103.8493, 103.8493, 103.8493)

#Creating a list of df from participant data
ListP <- list(P1, P2, P3)

#Creating background map
register_google(key = "insert key")
has_google_key()

Loc <- c(lon = 103.8502, lat = 1.2974)
map_1 <- get_googlemap(Loc, zoom = 17, maptype = 'roadmap', style='feature:all|element:labels|visibility:off', size = c(1000, 1000),)
ggmap(map_1)

到目前为止,我已经设法将它们全部绘制在一张地图上。我想应该有一个简单的语法修复来为列表中的每个数据框创建一个地图,但由于我的知识有限,我无法弄清楚。

Route <- ggmap(map_1) + map(listP, ~ geom_point(.x,  mapping = aes(x = Lon, y = Lat)))
Route
list purrr ggmap
© www.soinside.com 2019 - 2024. All rights reserved.