2

I would like to make a map in R that colours in the FAO Fishing Areas according to a data set (in my case, length data of shark species).

FAO Fishing Areas

I would prefer to do a choropleth map in ggplot but other types of maps are also fine. Worst case scenario a base map of FAO areas that I can add bubbles to. Even just an existing base map of FAO areas would be great. Any suggestions welcome!

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • I got a little farther by looking into how to upload JSON files into R, e.g. [choropleth maping](https://www.r-graph-gallery.com/327-chloropleth-map-from-geojson-with-ggplot2.html), and the data that I want is indeed in the link you suggested. I can get JSON files into R from other sources but I haven't been able to make the FAO resources work. What I'm still confused about is how to upload the links provided in the FAO map catalog into R as json files (or even as a dataframe would be ok). I've tried using the packages jsonlite and geojsonio but no success. Any more pointers appreciated! – user3857954 Jul 26 '21 at 20:12

1 Answers1

3

I went to this page and clicked through to find this link to retrieve a GeoJSON file:

download.file("http://www.fao.org/fishery/geoserver/fifao/ows?service=WFS&request=GetFeature&version=1.0.0&typeName=fifao:FAO_AREAS_CWP&outputFormat=json", dest="FAO.json")

From here on, I was following this example from the R graph gallery, with a little help from this SO question and these notes:

library(geojsonio)
library(sp)
library(broom)
library(ggplot2)
library(dplyr)    ## for joining values to map

spdf <- geojson_read("FAO.json",  what = "sp")

At this point, plot(spdf) will bring up a plain (base-R) plot of the regions.

spdf_fortified <- tidy(spdf)
## make up some data to go with ...
fake_fish <- data.frame(id = as.character(1:324), value = rnorm(324))
spdf2 <- spdf_fortified %>% left_join(fake_fish, by = "id")
ggplot() +
    geom_polygon(data = spdf2, aes( x = long, y = lat, group = group,
                                            fill = value), color="grey") +
    scale_fill_viridis_c() +
    theme_void() +
    theme(plot.background = element_rect(fill = 'lightgray', colour = NA)) +
    coord_map() +
    coord_sf(crs = "+proj=cea +lon_0=0 +lat_ts=45") ## Gall projection

ggsave("FAO.png")

Gall projection

notes

  • some of the steps are slow, it might be worth looking up how to coarsen/lower resolution of a spatial polygons object (if you just want to show the picture, the level of resolution might be overkill)
  • to be honest the default sequential colour scheme might be better but all the cool kids seem to like "viridis" these days so ...
  • There are probably better ways to do a lot of these pieces (e.g. set map projection, fill in background colour for land masses, ... ?)
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • If this solved your problem you are encouraged to click on the check-mark to accept it. – Ben Bolker Jul 27 '21 at 13:59
  • 1
    A few comments that may help speed: `sp` is now superseded by `sf`, and I believe `ggplot2` has been improved to better handle (in terms of speed) `sf` objects. So this may be an improvement you could make. And `sf` can read geojson directly, so no need for `geojsonio` (which again can be quite slow depending on the size of object). – SymbolixAU Jul 29 '21 at 23:27