0

I want to make a global raster map in the rounded shape (hatano projection). I have raster from ERA5 data which is in GCS coordinate system.

I applied following code:

library(raster) 
library(ggplot2) 
library(viridis) 
library(tidyterra) 
library(terra) 
setwd("C:/Users/usman/Desktop/a") 
r= raster("SH.tif") 
p = projectRaster(r, crs = "+proj=hatano",method = "bilinear")
g = graticule(60, 45, "+proj=hatano") 
plot(g, background="azure", mar=c(.2,.2,.2,4), lab.cex=0.5, col="light gray") 
myplot = plot(p, add=TRUE, axes=FALSE, plg=list(shrink=.8), col=viridis(25)) 
ggsave("tile_plot2.png", plot=myplot, height=4, width=6.5, dpi=150)`

And my output is

enter image description here

However, I want this

enter image description here

part of the code and the reference image is taken from the following thread R ggplot plotting map raster with rounded shape - How to remove data outside projected area?

Also, when I tried to save using a blank file is generated.

Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
usman
  • 21
  • 4

2 Answers2

0

Here is a reproducible example

library(terra)
library(geodata)
library(viridis)

tavg <- worldclim_global("tavg", 10, ".")
tavg <- mean(tavg)
tavg <- project(tavg, "+proj=hatano", mask=TRUE)

g <- graticule(60, 45, "+proj=hatano")

plot(g, background="azure", mar=c(.2,.2,.2,4), lab.cex=0.5, col="light gray")
plot(tavg, add=TRUE, axes=FALSE, plg=list(shrink=.8), col=viridis(25))

enter image description here

If you want to save this to a png file you can do

png("test.png", 1000, 450, pointsize=24)
plot(g, background="azure", mar=c(.2,.2,.2,4), lab.cex=0.5, col="light gray")
plot(tavg, add=TRUE, axes=FALSE, plg=list(shrink=.8), col=viridis(25))
dev.off()

enter image description here

One thing to keep in mind is that you need to size the canvas to match the size of the map you are making.

Your question and code are not very clear. You mention ggplot2 but you do not use it. See the original post for how to do the above with ggplot2

You use raster::projectRaster where terra::project should be used. You create a graticule but you do not use it.

Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
0

My take on this. For some reason ggplot2 works specially bad when labelling the axis of +proj=hatano (ggplot2 axis labels for world maps are specially messy, see this question), so I needed to place them manually at the starting point of the graticule (computed with lwgeom::st_startpoint():


library(terra)
library(tidyterra)
library(ggplot2)
library(geodata)
# For graticules, etc
library(sf)

r <- worldclim_global("tavg", 10, ".") |>
  mean() |>
  # Crop
  project("+proj=hatano", mask = TRUE)


# Discretize for better plotting after projection
g <- st_graticule(ndiscr = 500) |> st_transform(st_crs(r))
border <- st_graticule() |>
  st_bbox() |>
  st_as_sfc() |>
  st_transform(3857) |>
  st_segmentize(500000) |>
  st_transform(st_crs(r)) |>
  st_cast("POLYGON")

# Get label placement,
# This is the hardest part
library(dplyr)
labels_x_init <- g %>%
  filter(type == "N") %>%
  mutate(lab = paste0(degree, "°"))

labels_x <- st_as_sf(st_drop_geometry(labels_x_init), lwgeom::st_startpoint(labels_x_init))


labels_y_init <- g %>%
  filter(type == "E") %>%
  mutate(lab = paste0(degree, "°"))

labels_y <- st_as_sf(st_drop_geometry(labels_y_init), lwgeom::st_startpoint(labels_y_init))


# Plot
ggplot() +
  geom_sf(data = border, fill = "azure", color = "lightgray", linewidth = 1) +
  geom_sf(data = g, color = "lightgray") +
  geom_spatraster(data = r) +
  scale_fill_whitebox_c(palette = "viridi") +
  geom_sf_text(data = labels_x, aes(label = lab), nudge_x = -1000000, size = 3) +
  geom_sf_text(data = labels_y, aes(label = lab), nudge_y = -1000000, size = 3) +
  theme_void() +
  labs(x = "", y = "", fill = "Temp")

enter image description here

dieghernan
  • 2,690
  • 8
  • 16