【问题标题】:Is there an R function to convert numerical values into coordinates?是否有将数值转换为坐标的 R 函数?
【发布时间】:2020-08-12 17:59:36
【问题描述】:

我正在使用一个数据集,其中包含来自洞穴内不同位置的化学分析,每个分析都按站点编号和站点纬度和经度排序。这第一张图片是我最初使用 ggplot 所做的。 Map of site data, colored by N concentration

但我想要做的是使用数据来源的洞穴系统的 shapefile,并通过在系统上绘制点然后通过浓度对它们进行着色来做类似的事情。下面是我上传的Cave系统shapefile的shapefile Cave system shapefile

所以基本上我希望能够从用于映射第一个图形的数​​据集中映射化学数据,但在 shapefile 的映射上。最初它一直说它不能在它上面绘图。所以我想我必须将纬度和经度转换为空间坐标,然后才能映射到 shapefile。

    Master_Cave_data <- Master_cave_data %>%
    st_as_sf(MastMaster_cave_data, agr = "identity", coord = Lat_DD)

这是我想用来将数字纬度坐标转换为空间数据的方法。

【问题讨论】:

    标签: r spatial sf


    【解决方案1】:

    我假设您的坐标位于 WSG84 投影系统中(crs 代码 4326)。您可以通过以下方式创建 sf 对象:

    Master_Cave_data <- st_as_sf(MastMaster_cave_data, coords = c('lon', 'lat'), crs = 4326)
    

    lonlat 列更改为相关名称。要使用 shapefile 绘制点,您需要将它们都放在同一个投影系统中,以便在需要时重新投影:

    Master_Cave_data <- Master_cave_data %>% st_transform(st_crs(shapefile))
    

    示例

    借自there

    df <- data.frame(place = "London", 
           lat = 51.5074, lon = 0.1278,
           population = 8500000) # just to add some value that is plotable
    crs <- 4326
    df <- st_as_sf(x = df,                         
               coords = c("lon", "lat"),
               crs = crs)
    

    你可以看看地图:

    library(tmap)
    data("World")    
    tm_shape(World[World$iso_a3 == "GBR", ]) + tm_polygons("pop_est") + 
        tm_shape(df) + tm_bubbles("population")
    

    【讨论】:

    • 您好,感谢您的帮助。但是,当我尝试运行它时,出现以下错误:st_sf(x, ..., agr = agr, sf_column_name = sf_column_name) 中的错误:不存在简单的特征几何列
    • 我更正了,我认为管道是不必要的。如果它不起作用,您可以编辑答案以共享数据结构吗? (例如dput()
    • 所以转换似乎对我不起作用。不断收到此错误 UseMethod("st_transform") 中的错误:没有适用于“st_transform”的方法应用于“data.frame”类的对象
    • class(Master_cave_data)Master_Cave_data &lt;- st_as_sf(Master_cave_data, coords = c('lon', 'lat'), crs = 4326) 之后是什么?看起来它没有启动 sf 对象?或者可能是 shapefile 不是 sf 对象
    • 所以 [1] "sf" "tbl_df" "tbl" "data.frame" 是返回的内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多