I am using sf to plot a series of shapefiles and point data. This data spans the dateline at =/-180 longitude.
sf::st_shift_longitude() handles this situation and plots points as expected. However, ggplot is behaving strangely when assigning the longitude tick marks. Code below provides one example with two points on one side of dateline- note ggplot adds logical tick marks for longitude. In second example, points span dateline.
Edit: Added third case, with manually expanded x limits. When these get large enough, the graticule plots as expected. However, I only found 'large enough' by experimentation with 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()
Do you have any ideas on working around the dateline to not get this behavior when plotting with ggplot?
Thanks!



