Home > Mobile >  Zooming into region in Pacific
Zooming into region in Pacific

Time:08-08

I am trying to zoom into a specific region of the Marquesas Islands in French Polynesia using R and I am not sure how to find the coordinates for this region. I've only seen examples for France and Europe so any help would be greatly appreciated!

library(ggplot2)
install.packages("sf")
library(sf)
install.packages("rnaturalearthdata")
library(rnaturalearthdata)

worldmap <- ne_countries(scale = 'medium', type = 'map_units',
                         returnclass = 'sf')
# have a look at these two columns only
head(worldmap[c('name', 'continent')])

ggplot()   geom_sf(data = worldmap)   theme_bw()
ggplot()   geom_sf(data = worldmap)  
  coord_sf(xlim = c(?, ?), ylim = c(?, ?), expand = FALSE)  
  theme_bw()

CodePudding user response:

I believe that in your use case you will find the enter image description here

You can then use the bounding box to either clip your global data at presentation level (using xlim and ylim of your ggplot2::coord_sf() call) or at data level (using sf::st_intersection() with your desired bounding box as a polygon).

Edit: to give a more specific example consider this piece of code:

library(sf)
library(dplyr)
library(ggplot2)

# my favorite iteration of the World dataset; 
# there are others around and all work about the same...
world <- giscoR::gisco_countries

# create a polygon from the coordinates from bboxfinder.com as a base for clipping
# for more info check over here on sister site:
# https://gis.stackexchange.com/questions/403977/sf-create-polygon-from-minimum-x-and-y-coordinates/404001#404001

poly <- data.frame(lon = c(-196.7047, -191.4477),
                   lat = c(-23.0797, -19.3577)) %>% 
  st_as_sf(coords = c("lon", "lat"), 
           crs = 4326) %>% 
  st_bbox() %>% 
  st_as_sfc()

# a visual check of polygon
mapview::mapview(poly)

enter image description here

# clip world dataset to the shape of the poly polygon
clipped_world <- world %>% 
  st_intersection(poly)

# plot New Caledonia from the clipped result
ggplot()  
  geom_sf(data = clipped_world)

enter image description here

  • Related