【问题标题】:Getting lat and lon coordinates for ggmap in R在R中获取ggmap的纬度和经度坐标
【发布时间】:2015-02-27 22:38:38
【问题描述】:

我有来自 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}

我尝试了各种不成功的方法将位置数据放入单独的 lat 和 lon 列(示例如下):

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

【问题讨论】:

    标签: r ggmap


    【解决方案1】:

    你可以试试

    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))
    

    【讨论】:

    • 谢谢!!这完美地工作,唯一的例外是中心的计算。相反,我使用了模式: mode
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    • 2012-02-03
    相关资源
    最近更新 更多