我想在ggplot2中添加路径到使用geom_sf
生成的 map 中.
我熟悉使用以格林威治为中心的标准 map .但当我改变 map 设计时(?crs
.通常的方法失败了.
"正常" map 运行良好
point_df=data.frame(
location=c('Tokyo','Brasilia','France'),
lat=c(35.65,-15.79,48.86),
long=c(139.84,-47.88,2.35)
)
point_df2=data.frame(
from_location=c('Tokyo','Brasilia'),
from_lat=c(35.65,-15.79),
from_long=c(139.84,-47.88),
to_location=c('France'),
to_lat=c(48.86),
to_long=c(2.35)
)
library("rnaturalearth")
world <- ne_countries(scale = "medium", returnclass = "sf")
transpoint = st_as_sf(point_df,coords=c("long","lat"),crs=4326)
world_sf <- st_transform(world, crs=4326)
ggplot(world_sf)+geom_sf()+
geom_point(data=transpoint,aes(geometry=geometry,color=location),
stat="sf_coordinates")+
geom_text_repel(data=transpoint,aes(geometry=geometry,label=location),
stat="sf_coordinates")+
geom_curve(data=point_df2,
aes(x=from_long,y=from_lat,xend=to_long,yend=to_lat),
colour='blue')
不起作用: map 使用不同的CRS或不在格林威治中心
但是,如果我更喜欢以太平洋为中心的 map ,或者椭圆形 map ,它们就失败了.
pacific centred map with sf::st_break_antimeridian()
对于太平洋中心的 map ,我使用了一个代码,
注意geom_point()
很好第geom_segment()
章完全失败了
robinson <- "+proj=robin +lon_0=-90 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"
world_pac <- world_sf %>%
st_break_antimeridian(lon_0 = -90) %>%
st_transform(crs = robinson)
transpoint = st_as_sf(point_df,coords=c("long","lat"),crs=4326)
dtran = st_transform(transpoint,robinson)
library(ggrepel)
ggplot(world_pac)+geom_sf()+
geom_point(data=dtran,aes(geometry=geometry,color=location),stat="sf_coordinates")+
geom_text_repel(data=dtran,aes(geometry=geometry,label=location),stat="sf_coordinates")+
geom_segment(data=point_df2, aes(x=from_long,y=from_lat,xend=to_long,yend=to_lat),
colour='blue')
ggspatial::geom_spatial_segment()
显示曲线/片段,但它们很奇怪.
ggplot(world_pac)+geom_sf()+
geom_point(data=dtran,aes(geometry=geometry,color=location),stat="sf_coordinates")+
geom_text_repel(data=dtran,aes(geometry=geometry,label=location),stat="sf_coordinates")+
geom_spatial_segment(data=point_df2, aes(x=from_long,y=from_lat,xend=to_long,yend=to_lat),crs=4326,
wrap_dateline = FALSE,
colour='blue')
Ellipsis map
我也无法使geom_segment()
或geom_spatial_segment()
工作,如果我转换为crs=2163
.
world_ellipse <- world_sf %>%
st_transform(crs=2163)
transpoint = st_as_sf(point_df,coords=c("long","lat"),crs=4326)
dtran = st_transform(transpoint,crs=2163)
ggplot(world_ellipse)+geom_sf()+
geom_point(data=dtran,aes(geometry=geometry,color=location),stat="sf_coordinates")+
geom_text_repel(data=dtran,aes(geometry=geometry,label=location),stat="sf_coordinates")
geom_spatial_segment(data=point_df2,
aes(from_long, from_lat, xend = to_long, yend = to_lat),crs = 2163,
wrap_dateline = FALSE)
我错过了什么?
当我们使用不同的CRS时,如何使用geom_segment()
和/或geom_spatial_segment()
与ggplot2::geom_sf()
?