在R语言正交投影地球图中添加数据点异常问题求助
问题根源
你创建closures这个sf对象时犯了个关键错误:你的x(经度)和y(纬度)是WGS84坐标系(EPSG:4326)下的经纬度数据,但你直接把crs参数设成了正射投影的crs_string,这会让R误以为这些数值是正射投影下的平面坐标,自然会把点映射到投影中心附近。
修正步骤
- 第一步:创建sf对象时,先指定经纬度对应的CRS(EPSG:4326)
- 第二步:将sf对象转换为正射投影(
crs_string),和地图的坐标系保持一致 - 额外注意:如果转换后某些点不在当前正射投影的可视范围内(比如地球背面的点),这些点会被自动过滤掉,你可以检查转换后的
closures对象确认
修正后的完整代码
library(sf) library(giscoR) # for the countries dataset only library(ggplot2) # projection string used for the polygons & ocean background crs_string <- "+proj=ortho +lon_0=-30 +lat_0=50" # background for the globe - center buffered by earth radius ocean <- st_point(x = c(0,0)) %>% st_buffer(dist = 6371000) %>% st_sfc(crs = crs_string) # country polygons, cut to size world <- gisco_countries %>% st_intersection(ocean %>% st_transform(4326)) %>% # select visible area only st_transform(crs = crs_string) # reproject to ortho # one of the visible ones red (don't really matter which one :) world$fill_color <- ifelse(world$ISO3_CODE == "DEU", "interesting", "dull") closures <- data.frame( x = c(-30,-40), y = c(40,50), label = c("1","2") ) # 修正:先指定经纬度的CRS为4326,再转换到正射投影 closures <- sf::st_as_sf(closures, coords = c("x", "y"), crs = 4326) %>% # 这里改成4326 st_transform(crs = crs_string) # 转换到正射投影 # now the action! ggplot(data = world) + geom_sf(data = ocean, fill = "aliceblue", color = NA) + # background first geom_sf(aes(fill = fill_color), lwd = .1) + # now land over the oceans scale_fill_manual(values = c("interesting" = "lightyellow", #change colour here to highlight a specific country "dull" = "lightyellow"), guide = "none") + geom_sf(data = closures, aes(geometry = geometry), size = 7, shape = 21, color = "white", fill="red") + theme_void()
内容的提问来源于stack exchange,提问作者Always_confused
相关产品推荐
相关产品推荐

