Skip to content

Instantly share code, notes, and snippets.

@SimonCoulombe
Created November 9, 2020 04:26
Show Gist options
  • Save SimonCoulombe/3a276c3cf3f61646faee581e522e9a01 to your computer and use it in GitHub Desktop.
Save SimonCoulombe/3a276c3cf3f61646faee581e522e9a01 to your computer and use it in GitHub Desktop.
#http://www.intactforests.org/data.ifl.html
library(osmdata)
library(mapview)
library(spData)
library(sf)
library(tidyverse)
library(conflicted)
library(cowplot)
library(snapbox)
intact_forests <- read_sf("data/downloads/ifl_2016.shp")
Sys.setenv(MAPBOX_ACCESS_TOKEN = Sys.getenv("mapbox"))
# on crée un bounding box pour south america
area_south_america <- st_bbox(
c(xmin = -83, ymin = -56.65, xmax = -33.75, ymax = 14),
crs = 4326
)
# bbox to geometry https://github.com/r-spatial/sf/issues/572
area_square <- st_as_sfc(area_south_america)
## on peut utiliser un custom style!!!
# https://jechave.com/post/helsinki-city-bikes-a-summer-love-story/
# mon style https://studio.mapbox.com/styles/morglum/ckh9zdps22yx919qhnlw5zglg/edit/#4.74/-22.34/-56.76
#https://docs.mapbox.com/help/tutorials/create-a-custom-style/
map_base <- ggplot() +
layer_mapbox(
map_style = "mapbox://styles/morglum/ckh9zo92o2zao19qhqv3r0lrh",
area_south_america,
scale_ratio = 0.5)
mymap <- map_base +
ggthemes::theme_map()+
geom_sf(data=intact_forests %>% head(100), fill = "#228B22", inherit.aes = FALSE)+
theme(panel.grid.major = element_blank(),
axis.text = element_blank(),
axis.title = element_blank()) +
labs(
title = "Intact forest in South America",
subtitle = "(trying to implements ideas from the map by @RichardZimerman and @cristinapoiata)",
caption = "data from http://www.intactforests.org/data.ifl.html"
) +
coord_sf(crs = 4236, # crop to south america
xlim = c(area_south_america$xmin,area_south_america$xmax),
ylim = c(area_south_america$ymin, area_south_america$ymax),
expand = FALSE)
# TODO: how to remove white space to left and right of map?
area_world <- st_bbox(
c(xmin = -179, ymin = -80, xmax = 179, ymax = 80),
crs = 4326
)
world_map <- ggplot() +
layer_mapbox(
#map_style = stylebox:::mapbox_light(),
map_style = "mapbox://styles/morglum/ckh9zo92o2zao19qhqv3r0lrh",
area_world,
scale_ratio = 0.5,
attribution = FALSE,
mapbox_logo = FALSE) +
ggthemes::theme_map() +
geom_sf(data=area_square, color="black", fill = NA)+
coord_sf(expand = FALSE)
# http://upgo.lab.mcgill.ca/2019/12/13/making-beautiful-maps/
ggdraw(mymap) +
draw_plot(
{
world_map
},
# The distance along a (0,1) x-axis to draw the left edge of the plot
x = 0.5,
# The distance along a (0,1) y-axis to draw the bottom edge of the plot
y = 0.05,
# The width and height of the plot expressed as proportion of the entire ggdraw object
width = 0.2,
height = 0.2)
ggsave("test.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment