Я использую sf для построения серии шейп-файлов и точечных данных. Эти данные охватывают линию даты с долготой = / - 180.
sf::st_shift_longitude()
обрабатывает эту ситуацию и отображает точки, как и ожидалось. Однако ggplot ведет себя странно при назначении меток долготы. В приведенном ниже коде приведен один пример с двумя точками на одной стороне линии даты. Примечание. Ggplot добавляет логические отметки для долготы. Во втором примере точки охватывают линию даты.
Редактировать: Добавлен третий случай с расширенными пределами х вручную. Когда они становятся достаточно большими, сетка строится, как и ожидалось. Тем не менее, я нашел «достаточно большим» только экспериментально с xlim
.
library(sf)
library(rnaturalearth)
library(ggplot2)
#get basic world map
world = ne_coastline(scale = "medium", returnclass = "sf")
#example 1: without dateline
#minimal data- two points on one side of dateline
sites = data.frame(longitude = c(-173.9793, -177.7405), latitude = c(52.21415, 51.98994))
#convert to sf object
sites = st_as_sf(sites, coords = c("longitude", "latitude"), crs = 4326)
#plot with ggplot
ggplot()+
geom_sf(data = world, fill = 'transparent')+
geom_sf(data = sites)+
#set the limits for the plot
coord_sf(crs = 4326,
xlim = c(min(st_coordinates(sites)[,1]) -1, max(st_coordinates(sites)[,1])+1),
ylim = c(min(st_coordinates(sites)[,2]) -1, max(st_coordinates(sites)[,2])+1))+
labs(title = 'data on one side of dateline only- looks good')+
theme_bw()
#example 2: with dateline
#deal with dateline using st_shift_longitude
world_2 = st_shift_longitude(world)
#minimal data- a point on each side of dateline
sites_2 = data.frame(longitude = c(-173.9793, 177.7405), latitude = c(52.21415, 51.98994))
#convert to sf object
sites_2 = st_as_sf(sites_2, coords = c("longitude", "latitude"), crs = 4326)
#and deal with dateline using st_shift_longitude
sites_2 = st_shift_longitude(sites_2)
#plot with ggplot
ggplot()+
geom_sf(data = world_2, fill = 'transparent')+
geom_sf(data = sites_2)+
#set the limits for the plot
coord_sf(crs = 4326,
xlim = c(min(st_coordinates(sites_2)[,1]) -1, max(st_coordinates(sites_2)[,1])+1),
ylim = c(min(st_coordinates(sites_2)[,2]) -1, max(st_coordinates(sites_2)[,2])+1))+
labs(title = 'data on both sides of dateline - grid wrong')+
theme_bw()
#plot with manually expanded limits- graticule works
ggplot()+
geom_sf(data = world_2, fill = 'transparent')+
geom_sf(data = sites_2)+
#set the limits for the plot
coord_sf(crs = 4326,
xlim = c(175, 195),
ylim = c(min(st_coordinates(sites_2)[,2]) -1, max(st_coordinates(sites_2)[,2])+1))+
labs(title = 'data on both sides of dateline - manually expand x lims')+
theme_bw()
Есть ли у вас какие-либо идеи по работе с линией дат, чтобы не получить такое поведение при печати с ggplot?
Спасибо!