I'm making a map using ggplot and I want to be able to center the map around the Pacific Ocean while plotting points on the map.
It turns out I can do the map shifting by using the wrap option of maps::map. However, I'm not sure exactly how to shift the points to get them to match to my new shifted map. I found how to do that when I recreate a Pacific centered map with wrap(0, 360) but I'm not sure how to accomplish this for arbitrary shift units. I'm sure this is pretty straightforward but I can't seem to figure it out. Any ideas?
library(maps)
library(tidyverse)
# Pacific centered map
shift_value_1 <- 0
shift_value_2 <- 360
# Regular map, how about new values of shift_value_1 and shift_value_2? (e.g. -20, 325)
shift_value_1 <- -180
shift_value_2 <- 180
map_world_df <- map_data('world', wrap=c(shift_value_1, shift_value_2)) %>%
dplyr::filter(region != "Antarctica")
country_shapes <- geom_polygon(data = map_world_df,
aes(x=long, y = lat, group = group),
fill = "gainsboro",
color = "gainsboro",
size = 0.15)
nodes <- data.frame(names = c("A", "B", "C", "D"),
lat = c(64.220241, 10.278386, 64.710869, 19.432564),
lon = c(135.75572, 34.33927, -151.20003, -99.13323))
nodes$lon[nodes$lon <0] <- nodes$lon[nodes$lon <0] + (shift_value_1 + shift_value_2)
ggplot() +
country_shapes +
geom_point(data = nodes, aes(x=lon, y = lat))
I would love to be able to have a way of selecting arbitrary longitude ranges and shift the points accordingly, instead I get incorrectly placed points.