1

I am trying to create a New Zealand map using this script with ggplot2 in R.

library(sf) #'simple features' package
library(leaflet) # web-embeddable interactive maps
library(ggplot2) # general purpose plotting
library(rnaturalearth) # map data
library(rnaturalearthdata)# map data
library(ggspatial) # scale bars and north arrows

#the package below was installed from source through github: 
# devtools::install_github("ropensci/rnaturalearthhires"))
library(rnaturalearthhires)# map data 

theme_set(theme_bw())

newzealand <- ne_countries(country="New Zealand", type="countries", scale='large', returnclass = "sf")

ggplot(data = newzealand) +
    geom_sf()

Unfortunately, it is not working and all I get is a small version on the low right side of the plot (seems like it is on a scale of the world planisphere, see picture attached). If I tweak the script with other countries it works perfectly. Can someone help me? I have the feeling the map I am looking for is not present in the database the script use.

Thanks to everyone who can help.

Miriam

enter image description here

I tried to run the script but it didn't work

UseR10085
  • 7,120
  • 3
  • 24
  • 54
Miriam
  • 11
  • 1

2 Answers2

1

You can set xlim and ylim to have proper map like

library(sf) #'simple features' package
library(leaflet) # web-embeddable interactive maps
library(ggplot2) # general purpose plotting
library(rnaturalearth) # map data
library(rnaturalearthdata)# map data
library(ggspatial) # scale bars and north arrows

#the package below was installed from source through github: 
# devtools::install_github("ropensci/rnaturalearthhires"))
library(rnaturalearthhires)# map data 

theme_set(theme_bw())

newzealand <- ne_countries(country="New Zealand", type="countries", scale = 'large',  
                           returnclass = "sf")

ggplot(data = newzealand) +
  geom_sf() +
  xlim(166, 179) +
  ylim(-48, -34)

enter image description here

UseR10085
  • 7,120
  • 3
  • 24
  • 54
1

I think the problem is that the NZ polygon contains areas with longitudes both to the east and west of -180; thus, they get split when you try to plot them in its CRS (WGS 84, whose longitude goes from -180 to 180). A workaround might be to transform the polygons to a custom CRS, setting 180 as the central meridian.

# Bounding box
st_bbox(newzealand)
# X values extending from -177 to 178 
# Using proj string to center map on lon = 180
crs_cent180 <- st_crs("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +lon_0=180")
nz2 <- st_transform(newzealand, crs_cent180)
ggplot(data = nz2) +
  geom_sf() 

map