繁体   English   中英

如何在 R 中对形状文件进行 plot 坐标?

[英]How to plot coordinates over a shape file in R?

对此有点陌生,因此将不胜感激任何帮助。 我已经加载了世界上所有国家的 shapefile 并绘制了它。 但是,我在将一堆点从坐标添加到 plot 时遇到了问题。非常感谢任何有关如何解决此问题的帮助。

到目前为止的代码

MeteoriteData <- read.csv("C:/Users/ChaseDickson_/Desktop/College/AERO 689/Semester Project/Meteorite Landings.csv")
MeteoriteData$lat <- as.factor(MeteoriteData$lat)
MeteoriteData$long <- as.factor(MeteoriteData$long)

world <- st_read("C:/Users/ChaseDickson_/Desktop/College/AERO 689/Semester Project/TM_WORLD_BORDERS_SIMPL-0.3/TM_WORLD_BORDERS_SIMPL-0.3.shp")

world <- st_transform(world, crs = 4326)
ggplot() +
  geom_sf(data = world) +
  theme_bw()

这将返回附加的 plot。

在此处输入图像描述

我试过使用

ggplot() +
  geom_sf(data = world) +
  theme_bw() +
  geom_point(data = MeteoriteData, aes(x = lat, y = long)

但这似乎不起作用,因为它给了我这个错误

Error in `calc_limits_bbox()`:
! Scale limits cannot be mapped onto spatial coordinates in `coord_sf()`
ℹ Consider setting `lims_method = "geometry_bbox"` or `default_crs = NULL`.

我可以从您的代码中看到三个问题:

1/

MeteoriteData$lat <- as.factor(MeteoriteData$lat)

您需要将纬度和经度数据转换为因数吗? 我以前从未见过这样做,我怀疑这只会导致麻烦。

2/

geom_point(data = MeteoriteData, aes(x = lat, y = long)

看起来你的 x/y 和 long/lat 方向错了。 x 应该是 long,y 应该是 lat。

3/

正如 r2evans 所说,你需要在这一行加上一个右括号:

geom_point(data = MeteoriteData, aes(x = lat, y = long)

试试这个代码:

require('sf')

# Read in data from .csv file
MeteoriteData <- read.csv("C:/Users/ChaseDickson_/Desktop/College/AERO 689/Semester Project/Meteorite Landings.csv")

# Convert these points to an SF object, specifying the X and Y
#  column names, and supplying the CRS as 4326 (which is WGS84)
MeteoriteData <- st_as_sf(MeteoriteData, coords=c('long', 'lat'), crs=4326)

# Read in the world shape file and convert it to the same CRS
world <- st_read("C:/Users/ChaseDickson_/Desktop/College/AERO 689/Semester Project/TM_WORLD_BORDERS_SIMPL-0.3/TM_WORLD_BORDERS_SIMPL-0.3.shp")
world <- st_transform(world, crs = 4326)

# Plot it
ggplot() +
  geom_sf(data = world) +
  geom_sf(data = MeteoriteData) +
  theme_bw()

注意:我无权访问您的数据文件,因此此代码来自 memory 并且未经测试,但应该能让您走上正确的道路。

暂无
暂无

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

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