rggplot2kriging

Representing different layers in a ggplot map! R


M intention is to represent 3 different layers of information in a ggplot map: 1. The map itself using a MULTIPOLYGON file 2. Kriging estimations using geom_tile() 3. datapoints using geom_point()

I used the following script:

library(rnaturalearth)
library(rnaturalearthdata)
world <- ne_countries(scale = "medium", returnclass = "sf")
windows()
ggplot(data = world) +
  geom_sf(color="black",fill="grey90") + 
  theme(panel.background = element_blank()) +
  coord_sf(xlim = c(-12.3, 95), ylim = c(70, 22), expand = FALSE) +
  geom_tile(data = myKrige, aes(x= x1, y= x2, fill =var1.pred)) +
  geom_point(data = roh, aes(x = LON, y = LAT))

In this script I used three datasets: world (a MULTIPOLYGON obtained from rnaturalearthdata), myKrige (data frame obtained from a spatialPointsDataFrame) and roh (data frame with latitude and longitude data points).

This is the figure my script produces: enter image description here

As you can see the different layers are on top of each other. But I would like to merge nicely the geom_tile with the base plot.

Any idea how can I do it easily. Or should I rethink the complete figure?


Solution

  • Here is starting point, following an example from https://rpubs.com/nabilabd/118172

    library(rnaturalearth)
    library(rnaturalearthdata)
    library(ggplot2)
    library(sf)
    
    world <- ne_countries(scale = "medium", returnclass = "sf")
    df <- data.frame(x=rnorm(10,sd = 3), 
                     y=rnorm(10,sd = 3))
    df <- sf::st_as_sf(df, coords=c("x","y"), crs = 4326, agr = "constant", remove = F)
    ggplot(data = world) +
      geom_sf() +
      geom_sf(data=df) +
      # The idea would then to add add a scale_fill_gradient() such 
      # as in https://rpubs.com/nabilabd/118172 , but I dont know
      # how the kring data should look like. 
      coord_sf(xlim = c(-10,10), ylim=c(-10,10))
    
    # example
    lzn.kriged %>% as.data.frame %>%
      ggplot(aes(x=x, y=y)) + geom_tile(aes(fill=var1.pred)) + coord_equal() +
      scale_fill_gradient(low = "yellow", high="red") +
      scale_x_continuous(labels=comma) + scale_y_continuous(labels=comma) +
      theme_bw()