繁体   English   中英

在R中获取ggmap的纬度和经度坐标

[英]Getting lat and lon coordinates for ggmap in R

我有来自iOS Moves应用程序的数据,其格式为:

timeAtSite   location
800.52        {"lat": 38.87212, "lon": -94.61764}
116.40        {"lat": 38.91571, "lon": -94.64835}
14.48         {"lat": 38.91461, "lon": -94.64795}

我尝试了各种不成功的方法来将位置数据放入单独的纬度和经度列(以下示例):

al1 = get_map(location = c(lon: -94.61764, lat: 38.87212), zoom = 2, maptype = 'roadmap')
al1MAP = ggmap(al1)
al1MAP
al1MAP <- ggmap(al1)+ geom_point(data=c(moves$Location["lat"],moves$Location["lon"]))

TIA

你可以试试

library(stringr)
df[c('lat', 'lon')] <- do.call(rbind,lapply(str_extract_all(df$location,
                                                  '[-0-9.]+'), as.numeric))

要么

library(tidyr)
df1 <- extract(df, location, c('lat', 'lon'), '([-0-9.]+)[^-0-9.]+([-0-9.]+)', 
                    convert=TRUE)
df1
#  timeAtSite      lat       lon
#1     800.52 38.87212 -94.61764
#2     116.40 38.91571 -94.64835
#3      14.48 38.91461 -94.64795

提取位置后,

center <-  paste(min(df1$lat)+(max(df1$lat)-min(df1$lat))/2,
            min(df1$lon)+(max(df1$lon)-min(df1$lon))/2, sep=" ")

df1$id <- 1:3
library(ggmap)
al1 <- get_map(location = center, zoom = 11, maptype = "roadmap" )

p <- ggmap(al1)


p <- p + geom_text(data=df1,aes(x = lon, y = lat, label=id),
     colour="red",size=4,hjust=0, vjust=0)+
      theme(legend.position = "none") 

p <- p + geom_point(data=df1,aes(x=lon, y=lat),colour="black",size=2)
p

数据

df <- structure(list(timeAtSite = c(800.52, 116.4, 14.48), location =
 c("{\"lat\": 38.87212, \"lon\": -94.61764}", "{\"lat\": 38.91571,
  \"lon\": -94.64835}", "{\"lat\": 38.91461, \"lon\": -94.64795}"
 )), .Names = c("timeAtSite", "location"), class = "data.frame", row.names =
 c(NA, -3L))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM