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 = ",")

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

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


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

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

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

grid = st_as_stars(st_bbox(st_buffer(kerpensample_sf, 0.001)))
OK <- krige(
  as(kerpensample_sf, "Spatial"),
  model = v_mod_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


Here is a possible solution using akima & ggplot2.

#set working directory
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.

  • 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.