1
votes

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?

1
I would change the order of layers (by moving code rows up and down). Your sf layer should be below the point layer but on top of the tile layer and should have no fill, ie. fill = NA. - pasipasi
No, it did not solve it. And even I got a warning: "Coordinate system already present. Adding new coordinate system, which will replace the existing one". I even do not understand the warning since I made sure that the coordinate system were the same in the kriged dataframe and at the multipolygon one (+proj=longlat +datum=WGS84) ''' ggplot(data = world) + geom_point(data = roh, aes(x = LON, y = LAT)) + coord_sf(xlim = c(-12.3, 95), ylim = c(70, 22), expand = FALSE) + geom_sf(color="black",fill=NA) + geom_tile(data = myKrige, aes(x= x1, y= x2, fill =var1.pred))''' - Cebs
it helps a lot to be able to work with the same sort of data you have at hand. You did a great start exemplifying what you've done and what you want do to, but if you could provide a small example of the data, so that we could debug ourselves, it would be great. have a look here for instructions: reprex.tidyverse.org/articles/reprex-dos-and-donts.html - Marcelo Avila
to avoid the "Coordinate system already present" error, try to set the coord_sf() function to the end of the ggplot call. If you restrict your coordinate box further down in the ggplot call "change" the data, as you seem to do in geom_tile(data=...), it "restarts" the plotting area. Try to move the coord_sf() after that - Marcelo Avila
Thanks, Marcelo Avila I solved by changing the coord_sf(). - Cebs

1 Answers

2
votes

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()