1

I performed Kriging in R. But I have trouble getting a plot of a colored heatmap showing the resulted interpolated area along with a lon/lat values at the side and on the button.

Here is an example, what I want:

enter image description here

Here is my code so far:

kerpensample_df <- read.csv("C:\\Users\\49151\\Desktop\\kerpentest0909x.csv",
                         header=TRUE, stringsAsFactors=FALSE, sep = ",")
                       
head(kerpensample_df)

kerpensample_sf = st_as_sf(kerpensample_df, coords = c("X", "Y"), crs =32632)

v_emp_OK <- gstat::variogram(
  Z~1,
  as(kerpensample_sf, "Spatial")
)

plot(v_emp_OK)

v_mod_OK = autofitVariogram(Z~1, as(kerpensample_sf, "Spatial"))$var_model

plot(autofitVariogram(Z~1, as(kerpensample_sf, "Spatial")))

ggplot(
  data = kerpensample_df,
  mapping = aes(x = X, y =Y, color = Z)
) +
  geom_point(size = 3) +
  scale_color_viridis_b() +
  theme_classic()

grid = st_as_stars(st_bbox(st_buffer(kerpensample_sf, 0.001)))
OK <- krige(
  Z~1,
  as(kerpensample_sf, "Spatial"),
  grid, 
  model = v_mod_OK
)

plot(OK)

Here is my csv: https://ufile.io/l1wmw1te

It can probably be done with ggplot function, but I have no clue.

1 Answer 1

2

Here is a possible solution using akima & ggplot2.

#dependencies
library(ggplot2)
library(tidyr)
library(akima)
#set working directory
setwd('C:/Path/to/data')
df <- read.csv('kerpentest0909x.csv')
#interpolate missing values
my.df.interp <- interp(x = df$X, y = df$Y, z = df$Z, nx = 200, ny = 200) 
df_new <- as.data.frame(interp2xyz(my.df.interp))
#rename columns
names(df_new) <- c("X", "Y", "Z")
#compute mean Z
mean_Z <- mean(df_new$Z, na.rm = TRUE)
#replace NA with mean Z value
df_new <- df_new %>% dplyr::mutate(Z = replace_na(Z, mean_Z))
#create list of colors
colors = c('deeppink', 'blue3', 'aquamarine', 'chartreuse', 'yellow', 'darkorange1', 'red')
#create plot
ggplot(df_new, aes(X, Y, z = Z)) +
  stat_summary_2d(geom = "raster", bins = 150) +
  scale_fill_gradientn(colours = colors) 

Output: enter image description here

Adapted from the answers here and here.

4
  • couple of questions, hope you don't mind. a) what are these grey lines in the plot forming a cross? b) could you explain how you set up your target grid? c) why did you convert Z to integers? I would need the actual values(negatives) for the kriging. Sep 14, 2021 at 10:33
  • @simonericmoon a.) the grey lines are gone now that I reduced the bin number from 200 to 150. b.) the interp() function from package akima lets you specify the dimensions of the new dataset and interpolates the values. The remaining NA values were replaced with the mean Z value (bottom right corner) c.) to reduce file size. removed this bit and changed the values back to negative
    – Kartograaf
    Sep 14, 2021 at 14:59
  • thanks for clearing things up. in the docs I read that the interp() function can be used for spline interpolation, which is very nice. but in akima I don't see a use for diffrent kriging models, right? I.e. I'd like to compare diffrent types of kriging(ordinary and simple). Can this be done with akima, too? Sep 14, 2021 at 17:35
  • Sure, I think you may want to start by creating similar plots using the outputs from your various kriging operations and compare them side by side. Other types of data analysis may be more appropriate if you are looking for a statistical (rather than purely visual) representations of how your modeling methods differ (i.e., regression).
    – Kartograaf
    Sep 14, 2021 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.