1

I have developed a genetic algorithm for estimating the probability of observing an animal, given its genotype, across a regular grid of locations, here in south-east England. Using ggplot2 I can easily generate either a probability contour plot or a land-only (polygon-filled) map, but what I want is a map where the contour plot is restricted to land:

(component plots (left, middle) and desired outcome (right))

The desired outcome is generated by adding a black mask to the contour plot in Powerpoint, a tedious procedure that is impractical for generating the hundreds I need. I am sure there must be a simple way to do this.

I generate the contour plot using:

v <- ggplot(data, aes(Lat, Lng, z = P))
v + geom_contour(bins = 20)

and the map using:

ggplot(data = world) +
    geom_sf(color = "black", fill = "gray") +
    coord_sf(xlim = c(-2.3, 1.9), ylim = c(50.9, 53.5), expand = FALSE)

my input file comprises all locations in 0.05 increments of longitude and latitude in the intervals specified. It is large but I would happily add it if this helps. I have looked online and cannot see any examples that match what I want.

I have tried adding one component to the other as an extra layer but I struggle to understand what is needed and what the syntax are. For example:

layer(geom = "contour", stat = "identity", data = data, mapping = aes(Lng,Lat,P))

Error: Attempted to create layer with no position. but even if this works it does not mask the sea area.

tjebo
  • 21,977
  • 7
  • 58
  • 94
Bill Amos
  • 21
  • 1
  • It's easier to help if we would have a proper reproducible example. This doesn't necessarily need to be your actual data, and it doesn't need to be the UK. – tjebo Jan 02 '23 at 10:50
  • I think the easiest way to solve your problem would be to fill the polygons that are left when subtracting the land polygons. I guess there might be different approaches to calculate those. Here a few links that might help. This package for example: https://github.com/MikkoVihtakari/ggOceanMaps, suggested in this thread https://stackoverflow.com/questions/47684540/plot-a-small-region-of-a-map-with-ocean-in-ggplot2 – tjebo Jan 02 '23 at 10:55
  • or https://stackoverflow.com/questions/67615318/how-to-colour-the-ocean or (not ggplot2) https://stackoverflow.com/questions/16328438/how-can-i-color-the-ocean-blue-in-a-map-of-the-us – tjebo Jan 02 '23 at 10:56
  • if you follow [this thread](https://stackoverflow.com/questions/68219487/how-to-make-the-great-lakes-the-same-color-as-the-ocean-in-r), using the package {rnaturalearth} might be your solution but the maps would be `type = "ocean"`, as in https://stackoverflow.com/questions/42462721/colouring-oceans-using-naturalearth-data-issues-with-rendering – tjebo Jan 02 '23 at 10:59
  • I think those should hopefully be enough threads to help you. Please try them, and if it doesn't work, update your question to a fully reproducible example and give us a shout, and also explain what does not work with those suggestions. Also, please don't delete this question please as it will link other people with the same question to those threads. Thanks – tjebo Jan 02 '23 at 11:00

1 Answers1

2

Here's a worked example with some made-up data:

library(rnaturalearth)
library(ggplot2)

sea <- ne_download(scale = 10, type = 'ocean', category = "physical",
                   returnclass = "sf")

ggplot(data) + 
  geom_contour_filled(aes(Lng, Lat, z = P), bins = 20, color = "black") +
  guides(fill = "none") +
  geom_sf(data = sea, fill = "black") +
  coord_sf(ylim = c(51, 53.5), xlim = c(-2.2, 1.8), expand = FALSE)

enter image description here


Data used

set.seed(1)

a <- MASS::kde2d(rnorm(100), rnorm(100, 53), n = 100, 
           lims = c(-2.2, 1.8, 51, 53.5))
b <- MASS::kde2d(rnorm(25, 0.5), rnorm(25, 52), n = 100, 
           lims = c(-2.2, 1.8, 51, 53.5))
a$z <- b$z - a$z + max(a$z)

data <- cbind(expand.grid(Lng = a$x, Lat = a$y), P = c(a$z))

Created on 2023-01-02 with reprex v2.0.2

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87