Alex
Alex

Reputation: 504

Plot global projected raster in R

I'm trying to plot a global (lon:-180- 180; lat -90- 90) raster in Equal Earth projection (doesn't matter - could be Winkel Tripel or Robinson) but the boundaries duplicate on both sides (see figure). How can I avoid this?

This SO question and this thread give an answer that only works for plotting in Mollweide, but no other projection.

Here's a reproducible example.

library(maptools)
library(raster)
data("wrld_simpl")
r <- raster(ncol=180, nrow=90) 
r <- rasterize(wrld_simpl, r, field="UN") 
world_ext = projectExtent(wrld_simpl, crs = '+proj=longlat +datum=WGS84 +no_defs ')
r <- crop(x = r, y = world_ext, snap= 'in')
r <- projectRaster(r, crs = crs("+proj=wintri"), over = T)
plot(r)

Many thanks!

enter image description here

Upvotes: 0

Views: 338

Answers (1)

Robert Hijmans
Robert Hijmans

Reputation: 47326

You can use the mask=TRUE argument in terra::project for this

library(maptools)
library(terra)
data("wrld_simpl")
w <- vect(wrld_simpl)
r <- rast(ncol=180, nrow=90) 
r <- rasterize(w, r, field="UN") 
x <- project(r, "+proj=wintri", mask=TRUE)

Upvotes: 1

Related Questions