Kriging with metR

[This article was first published on Code R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Say you have data measured at different weather stations, which in Argentina might look something like this

estaciones[data, on = c("nombre" = "station")] |> 
   ggplot(aes(lon, lat)) +
   geom_point(aes(color = t)) +
   geom_sf(data = argentina_provincias, inherit.aes = FALSE, fill = NA) + 
   scale_color_viridis_c()

Because this is not a regular grid, it’s not possible to visualise this data with contours as is. Instead, it’s necessary to interpolate it into a regular grid.

There are many ways to go from irregular measurement locations to a regular grid and it comes with a bunch of assumptions. But for quick an dirty visualisations, metR::geom_contour_fill() can use kriging by setting kriging = TRUE

estaciones[data, on = c("nombre" = "station")] |> 
   ggplot(aes(lon, lat)) +
   metR::geom_contour_fill(aes(z = t), 
                           kriging = TRUE) +
   geom_point(aes(fill = t), shape = 21) +
   geom_sf(data = argentina_provincias, inherit.aes = FALSE, fill = NA) + 
   scale_color_viridis_c(aesthetics = c("fill", "colour")) 

One big problem with this is that by default it estimates values in the bounding box of the data, which in this case includes a bunch of the Atlantic Ocean. So it would be nice to be able to only show the contours over land.

In a desperate attempt to procrastinate from writing my thesis I implemented this functionality. Now, the clip argument takes a polygon which clips the contours.

estaciones[data, on = c("nombre" = "station")] |> 
   ggplot(aes(lon, lat)) +
   metR::geom_contour_fill(aes(z = t), 
                           kriging = TRUE, 
                           clip = argentina_provincias_sin_malvinas) +
   geom_point(aes(fill = t), shape = 21) +
   geom_sf(data = argentina_provincias, inherit.aes = FALSE, fill = NA) + 
   scale_color_viridis_c(aesthetics = c("fill", "colour")) 

Another small issue is that the default interpolates to a 40-pixel wide grid, which is a bit too coarse and doesn’t reach the top corners of the map. The kriging argument now can take a numeric, which defines number of gridpoints each direction.

estaciones[data, on = c("nombre" = "station")] |> 
   ggplot(aes(lon, lat)) +
   metR::geom_contour_fill(aes(z = t), 
                           kriging = 100, 
                           clip = argentina_provincias_sin_malvinas) +
   geom_point(aes(fill = t), shape = 21) +
   geom_sf(data = argentina_provincias, inherit.aes = FALSE, fill = NA) + 
   scale_color_viridis_c(aesthetics = c("fill", "colour")) 

Much better!

You can install the development version of metR with

install.packages("metR", repos = c("https://eliocamp.github.io/metR", getOption("repos")))
To leave a comment for the author, please follow the link and comment on their blog: Code R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)