All Projects → Chrisjb → basemapR

Chrisjb / basemapR

Licence: other
Contains functions to add base maps to ggplot2 maps

Programming Languages

r
7636 projects

Projects that are alternatives of or similar to basemapR

value-investing-studies
Data Analysis Studies on Value Investing
Stars: ✭ 66 (+371.43%)
Mutual labels:  ggplot2
ggHoriPlot
A user-friendly, highly customizable R package for building horizon plots in ggplot2
Stars: ✭ 115 (+721.43%)
Mutual labels:  ggplot2
TDAstats
R pipeline for computing persistent homology in topological data analysis. See https://doi.org/10.21105/joss.00860 for more details.
Stars: ✭ 26 (+85.71%)
Mutual labels:  ggplot2
PlotTwist
PlotTwist - a web app for plotting and annotating time-series data
Stars: ✭ 21 (+50%)
Mutual labels:  ggplot2
NBA-Shot-Charts
Create NBA shot charts using data scrapped from stats.nba.com and R package ggplot2.
Stars: ✭ 33 (+135.71%)
Mutual labels:  ggplot2
TidyTuesday
📊 Collection of #TidyTuesday Visualisations! 📈
Stars: ✭ 59 (+321.43%)
Mutual labels:  ggplot2
Manu
This package provides colour palettes derived from birds native to New Zealand
Stars: ✭ 70 (+400%)
Mutual labels:  ggplot2
rockthemes
R colour palettes based on classic rock album covers.
Stars: ✭ 30 (+114.29%)
Mutual labels:  ggplot2
ggbg
Miscellaneous Ggplot2 Extensions
Stars: ✭ 21 (+50%)
Mutual labels:  ggplot2
ggdogs
The geom you always wished for adding dogs to ggplot2
Stars: ✭ 28 (+100%)
Mutual labels:  ggplot2
gameofthrones
🎨 Game of Thrones inspired palette for R
Stars: ✭ 69 (+392.86%)
Mutual labels:  ggplot2
stmprinter
Print multiple stm model dashboards to a pdf file for inspection
Stars: ✭ 34 (+142.86%)
Mutual labels:  ggplot2
ggwaffle
Creating waffle charts in a ggplot friendly way
Stars: ✭ 45 (+221.43%)
Mutual labels:  ggplot2
r-whatsapp-analysis-parte1
Análisis de texto y visualización de datos con R, de conversaciones de WhatsApp, primer parte. Uso de librería rwhatsapp.
Stars: ✭ 22 (+57.14%)
Mutual labels:  ggplot2
mapr
Map species occurrence data
Stars: ✭ 34 (+142.86%)
Mutual labels:  ggplot2
gganonymize
Anonymize the labels and text in a ggplot2
Stars: ✭ 42 (+200%)
Mutual labels:  ggplot2
FunnelPlotR
Funnel plots for comparing institutional performance, with overdispersion adjustment
Stars: ✭ 39 (+178.57%)
Mutual labels:  ggplot2
ggtrack
restlessdata.com.au/ggtrack
Stars: ✭ 39 (+178.57%)
Mutual labels:  ggplot2
R4Econ
R Code Examples Multi-dimensional/Panel Data
Stars: ✭ 16 (+14.29%)
Mutual labels:  ggplot2
imprint
Create Customized 'ggplot2' and 'R Markdown' Themes for Your Organization
Stars: ✭ 24 (+71.43%)
Mutual labels:  ggplot2

basemapR

07/01/2020

Installing basemapR

Install from github using devtools:

library(devtools)
install_github('Chrisjb/basemapR')

Credits

A lot of credit has to go to Hiroakki Yutani for his blog post from which I borrowed a lot of code for the base_map function. I hope they don’t mind!

Adding a basemap to ggplot2

the base_map function can be added to a ggplot2 call as follows:

library(ggplot2)
library(sf)
library(basemapR)

ggplot() +
  base_map(st_bbox(localauth_data), increase_zoom = 2) +
  geom_sf(data = localauth_data, fill = NA)

bbox

A bounding box created using st_bbox or the basemapR function expand_bbox (see below). The bounding box defines the extents over which we want the base map to be returned.

This will normally be the largest layer on our map. Sometimes we will have two distinct layers neither of which covers the full extent of the map on their own. For example:

library(dplyr)
library(basemapR)
library(sf)


camden <- localauth_data %>% 
  filter(Name  == 'Camden')

wandsworth <- localauth_data %>% 
  filter(Name  == 'Wandsworth')


ggplot() +
  geom_sf(data = camden) +
  geom_sf(data = wandsworth)

We need to create a bbox that combines both layers for the base map to cover the full extents of the canvas:

# create bbox polygons for each and union and then convert back to bbox object
my_bbox <- st_bbox(camden) %>%
  st_as_sfc() %>%
  st_union(st_as_sfc(st_bbox(wandsworth))) %>%
  st_bbox()
ggplot() +
  base_map(bbox = my_bbox, basemap = 'hydda', increase_zoom = 2) +
  geom_sf(data = camden, fill = NA) +
  geom_sf(data = wandsworth, fill = NA) +
  ggthemes::theme_map()
## attribution: Tiles courtesy of http://openstreetmap.se/ OpenStreetMap Sweden; Map data &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors

increase_zoom

The zoom parameter is calculated automatically. It can be increased by setting increase_zoom to an integer value. In the first example we set increase_zoom = 2. We can play around with this value as per our desired aesthetic.

basemap

Various options for base maps. The attribution for the base layer should be included on the maps and is returned as a message from the function.

dark

attribution: © OpenStreetMap contributors © CARTO

hydda

positron attribution: Tiles courtesy of http://openstreetmap.se/ OpenStreetMap Sweden; Map data © OpenStreetMap contributors

voyager attribution: © OpenStreetMap contributors © CARTO

wikimedia please see attribution details: https://wikimediafoundation.org/wiki/Maps_Terms_of_Use

mapnik attribution: © OpenStreetMap contributors

nolabels

If nolabels=TRUE the function will fetch the basemap without street or place labels. This option is only available for some basemaps and will return a message if unavailable.

expand_bbox

This function takes in a bbox object and expands it by a set number of meters in the X and Y directions. If we want an asymmetric expansion (100m to the east but 1km to the west) we can also specify the parameters X2 and Y2.

# define a single point
point <- data.frame(x = -0.086543 ,
                    y= 51.504567) %>%
  st_as_sf(coords= c('x','y'), crs = 4326)

# standard bbox for our point
my_bbox_1 <- st_bbox(point)

# expand the bbox by 1000m
my_bbox_2 <- expand_bbox(my_bbox_1, X = 1000, Y = 1000)

We will have to also set the x and y limits ourselves in coord_sf to clip the canvas to our basemap:

ggplot() + 
  base_map(my_bbox_2, increase_zoom = 1, basemap = 'positron')+
  geom_sf(data = point) +
  coord_sf(xlim = c(my_bbox_2['xmin'], my_bbox_2['xmax']),
           ylim = c(my_bbox_2['ymin'],my_bbox_2['ymax']),crs = 4326)
## https://basemaps.cartocdn.com/light_all/14/8187/5448.pnghttps://basemaps.cartocdn.com/light_all/14/8188/5448.pnghttps://basemaps.cartocdn.com/light_all/14/8187/5447.pnghttps://basemaps.cartocdn.com/light_all/14/8188/5447.png

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].