2
votes

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.

enter image description here

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))

enter image description here

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.

1

1 Answers

2
votes

First of all, when using wrapping with two limits, you should make sure they add up correctly. Wrapping is not the same as just setting boundaries. For instance, in you're comment to the code you are asking the map to continue from -25 to 320, which is inconsistent. The resulting map will have some strange artefacts (more visible if you try larger errors, e.g. (-25, 150) ). You should always have shift_value2 - shift_value1 == 360, so for instance (-25, 335). Shifting longitudes by any other value will always give errors. You can add xlim=c(-25, 320) to the map() call if you wish, of course. But this should be done in a separate map() call (as explained in the documentation: xlim is applied before the wrapping, so combining them results in part of the map being dropped). So for a limited map, you should probably do

mymap <- map(wrap=c(-25, 335), fill=TRUE, plot=FALSE)
map(mymap, xlim=c(-25, 250),...)

But that is not necessary when using ggplot2 for plotting the map, because those map limits are applied after the call to map() and the wrapping.

Shifting an arbitrary point just means adding (or subtracting) 360 until it falls between the two values. In most cases, the following should work:

lon[lon < shift_value1] <- lon[lon < shift_value1] + 360
lon[lon > shift_value2] <- lon[lon > shift_value2] - 360