對此有點陌生,因此將不勝感激任何幫助。我已經加載了世界上所有國家的 shapefile 并繪制了它。但是,我無法將一堆點從坐標添加到繪圖中。任何有關如何解決此問題的幫助將不勝感激。
到目前為止的代碼
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()
這將回傳附加的圖。

我試過使用
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`.
uj5u.com熱心網友回復:
我可以從您的代碼中看到三個問題:
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()
注意:我無權訪問您的資料檔案,因此此代碼來自記憶體且未經測驗,但應該讓您走上正確的道路。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/536944.html
