【发布时间】:2021-08-14 02:15:32
【问题描述】:
我使用苏格兰的酿酒厂处理数据集,我想在地图上可视化它们的位置。 所以我用下面的代码来画苏格兰和它的周边是这样的:
library(SpatialEpi)
library(tmap)
library("sf")
library("rnaturalearth")
library("rnaturalearthdata")
library("rgeos")
library("ggspatial")
library("ggrepel")
world <- ne_countries(scale = "medium", returnclass = "sf")
ggplot(data = world) +
geom_sf() +
coord_sf(xlim = c(-13.68, 0.72), ylim = c(54.63, 60.85), expand = FALSE)
之后,我使用数据集威士忌的经度/纬度信息,并尝试将酿酒厂绘制为点(数据集可以在这里下载:whisky):
whisky <- read.csv("whisky_data.csv")
mapdata <- whisky[,c(1,15:16)]
mapdata$Latitude <- as.numeric(mapdata$Latitude)
mapdata$Longitude <- as.numeric(mapdata$Longitude)
coordinates(mapdata)<-~Longitude+Latitude
class(mapdata)
# does it have a projection/coordinate system assigned?
proj4string(mapdata) # Nope
# check coordinate system of world
st_crs(world) # +proj=longlat +datum=WGS84
# manually tell R what the coordinate system is
proj4string(mapdata)<-CRS("+proj=longlat +datum=WGS84")
ggplot(data = world) +
geom_sf() +
coord_sf(xlim = c(-13.68, 0.72), ylim = c(54.63, 60.85), expand = FALSE) +
geom_point(data=as.data.frame(mapdata), aes(x=Longitude, y=Latitude), color="red")
但是地图上没有显示任何点。 我想我没有正确转换坐标以绘制到世界地图中?
由于我是地图方面的绝对初学者,因此我非常感谢您对此提供的任何帮助! 非常感谢! :)
【问题讨论】:
标签: r coordinates sf