I am trying to plot a heat map of gridded data on a curvilinear grid. From what I have read the stars
package provides this functionality using geom_stars
on a stars object created using st_as_stars
with a curvilinear
option. There is a nice blog post on it here. However, when I try to use this method it fails. Simple reprex below:
library(stars)
library(ggplot2)
lng <- rbind(c(-2.099361, -2.098536, -2.097710, -2.096885),
c(-2.099350, -2.098525, -2.097699, -2.096874), c(-2.099339, -2.098514,
-2.097688, -2.096863), c(-2.099328, -2.098503, -2.097677, -2.096852))
lat <- rbind(c(57.10963, 57.10963, 57.10962, 57.10962), c(57.11008,
57.11008, 57.11007, 57.11007),c(57.11053, 57.11053, 57.11052, 57.11051), c(57.11098, 57.11098, 57.11097, 57.11096))
arr <- rbind(1:4, 5:8, 9:12, 13:16)
arr_star <- st_as_stars(arr, curvilinear = list(west_east = lng,
south_north = lat))
ggplot() + geom_stars(data = arr_star, alpha = 0.75)
The error message is:
Error:
! Tibble columns must have compatible sizes.
• Size 2: Column `curvilinear`.
• Size 16: Columns `X1`, `X2`, and `X`.
ℹ Only values of size one are recycled.
Run `rlang::last_error()` to see where the error occurred.
Looking at the code for geom_stars
, it seems to be trying to use dplyr::as_tibble
on arr_star
and it makes sense that this would fail. Any idea where I'm going wrong?
Also don't think that this is desired behavior. Here's a working approach:
Transform your data matrix to a stars
object using st_as_stars()
.
arr <- st_as_stars(
rbind(1:4, 5:8, 9:12, 13:16)
)
Add coordinates to arr
as you've done in your example code:
arr <- st_as_stars(
arr, curvilinear = list(X1 = lng, X2 = lat)
)
Now, arr
has a curvilinear grid with WGS 84 reference system:
print(arr)
stars object with 2 dimensions and 1 attribute
attribute(s):
Min. 1st Qu. Median Mean 3rd Qu. Max.
A1 1 4.75 8.5 8.5 12.25 16
dimension(s):
from to refsys point values x/y
X1 1 4 WGS 84 FALSE [4x4] -2.09936,...,-2.09685 [x]
X2 1 4 WGS 84 FALSE [4x4] 57.1096,...,57.111 [y]
curvilinear grid
Plot using ggplot.
ggplot() +
geom_stars(data = arr, alpha = 0.75)