我正在使用一个数据集,该数据集包含来自洞穴内不同位置的化学分析,每个分析均按站点编号排序,并按站点的经度和纬度排序。第一张图像是我最初使用ggplot最初完成的。Map of site data, colored by N concentration
但是我想做的是使用从中获取数据的洞穴系统的shapefile,并通过在系统上绘制点然后通过集中为它们着色来进行类似的操作。这是我上传的Cave系统shapefile的shapefileCave system shapefile
因此,基本上,我希望能够从用于映射第一个图形的数据集中映射到化学数据,但要在shapefile的映射上。最初,它一直说不能在其上面进行绘图。因此,我认为必须将纬度和经度转换为空间坐标,然后才能将其映射到shapefile中。
Master_Cave_data <- Master_cave_data %>%
st_as_sf(MastMaster_cave_data, agr = "identity", coord = Lat_DD)
这是我曾想用来将数值纬度坐标转换为空间数据的方法。
我假设您的坐标在WSG84投影系统中(crs代码4326)。您可以通过以下方式创建sf
对象:
Master_Cave_data <- st_as_sf(MastMaster_cave_data, coords = c('lon', 'lat'), crs = 4326)
将lon
和lat
列更改为相关名称。要使用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")