[英]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.