knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-"
)

ggplot2 and tidy Swiss data centric suite

Suite of convenience functions for geospatial mapping of Swiss data. It integrates geographical data at different levels (municipality, cantons, ...), cities. It also incorporates various muncipality & cantons socio-economic and voting indicators. Focus is on low levels functions for maximum flexibility

It relies on bleeding edge R packages, primarily sf and tidyverse.

Installation

It will probably never be on CRAN. To be installed from github:

# install.packages("devtools")
devtools::install_github("d-qn/taMap")

TODO

Clean up

Features

library(esri2sf)
url <- 'https://ge.ch/sitgags1/rest/services/VECTOR/SITG_OPENDATA_02/MapServer/6186'
df <- esri2sf(url)

Examples

Show the %foreigners vs % vote UDC by municipalities

require(tidyverse)
library(tamMap)
communeData <- loadCommunesCHportraits()
colnames(communeData)

# Select only "surface" indicators
colIdx <- which(attr(communeData, "indicatorGroup") == "Surface")
head(communeData[,colIdx])

## Show the %foreigners vs % vote UDC
g1 <- ggplot(data = as.data.frame(communeData), aes(x = `Etrangers en %`, y = UDC)) + 
  geom_point(aes(size = Habitants), alpha = 0.5)
g1

A long example to show a map with a zoom inlet map of Geneva

require(sf)

require(rmapshaper)

shp_ch_paths_2018 <- shp_path(2018)
# loop and load the geo data in a named list
shp_ch_geodata <- shp_ch_paths_2018 %>% map(function(x) {
  layerName <- st_layers(x)
  st_read(x, layer = layerName$name, 
          options = "ENCODING=latin1", stringsAsFactors = F) %>% 
  select(ends_with("NR"), ends_with("NAME"))
})

# shift and scale up canton Geneva for the inlet map
geo_subset <- shp_ch_geodata$municipalities %>% filter(KTNR == 25)
inlet_geo <- shiftScale_geo(geo = geo_subset, scaleF = 4.4)
# Create circles sf to encompass the inlet map and its original location
geo_subset_coord <- encircle_coord(geo_subset)
inlet_coord <- encircle_coord(inlet_geo)
cone_sfc <- cone_geo(geo_subset_coord, inlet_coord)
circle_zoom_sfc <- encircle_geo(geo_subset_coord)
circle_inlet_sfc <- encircle_geo(inlet_coord)

# simplify muni geo, under 0.7 simplification and the lakes will look odd
shp_ch_geodata$municipalities <- 
  shp_ch_geodata$municipalities %>% ms_simplify(keep_shapes=T, keep = 0.75)

# plot
gp <- ggplot() +
  geom_sf(data = cone_sfc, fill = "lightgrey", lwd = 0, alpha = 0.2) +  
  geom_sf(data = circle_zoom_sfc, fill = "lightgrey", lwd = 0, alpha = 0.5) +
  geom_sf(data = circle_inlet_sfc, fill = "lightgrey", lwd = 0, alpha = 0.5) +
  geom_sf(data = shp_ch_geodata$municipalities, aes(fill = GMDNR), 
    lwd = 0.05, colour = "#0d0d0d") +
  #geom_sf(data = eauGe, fill = "blue") +
  geom_sf(data = inlet_geo, aes(fill = GMDNR), lwd = 0.1, colour = "#0d0d0d") +
  geom_sf(data = shp_ch_geodata$cantons, lwd = 0.15, colour = "#333333", fill = NA) +
  geom_sf(data = shp_ch_geodata$country, lwd = 0.25, colour = "#000d1a", fill = NA) +
  geom_sf(data = shp_ch_geodata$lakes  %>% 
            filter(SEENAME != "Lago di Como"), lwd = 0, fill = "#0066cc")

  gp + 
    theme_map() +
    scale_fill_viridis_c()  +
    coord_sf(datum = NA, expand = F)
  ## Same as above but interactive !
  library(ggiraph)
  require(htmltools)
  # repalce quote signs

  shp_ch_geodata$municipalities$GMDNAME <- 
    gsub("'", "_", shp_ch_geodata$municipalities$GMDNAME)
  inlet_geo$GMDNAME <- gsub("'", "_", inlet_geo$GMDNAME)

  gpi <- ggplot() +
    geom_sf(data = cone_sfc, fill = "lightgrey", 
            lwd = 0, alpha = 0.2,  colour = "transparent") +  
    geom_sf(data = circle_zoom_sfc, fill = "lightgrey", 
            lwd = 0, alpha = 0.5, colour = "transparent") +
    geom_sf(data = circle_inlet_sfc, fill = "lightgrey", 
            lwd = 0, alpha = 0.5, colour = "transparent") +
    geom_sf_interactive(
      data = shp_ch_geodata$municipalities, 
      aes(fill = GMDNR, tooltip = GMDNAME, data_id = GMDNAME), 
      lwd = 0.05, colour = "#0d0d0d"
    ) +
    geom_sf_interactive(
      data = inlet_geo, 
      aes(fill = GMDNR, tooltip = GMDNAME, data_id = GMDNAME),
      lwd = 0.1, colour = "#0d0d0d"
    ) +
  geom_sf(data = shp_ch_geodata$cantons, lwd = 0.15, 
          colour = "#333333", fill = NA) +
  geom_sf(data = shp_ch_geodata$country, 
          lwd = 0.25, colour = "#000d1a", fill = NA) +
  geom_sf(data = shp_ch_geodata$lakes %>% filter(SEENAME != "Lago di Como"), 
          lwd = 0, fill = "#0066cc", colour = "transparent")
  gpi <- gpi + 
    theme_map() +
    scale_fill_viridis_c()  +
    coord_sf(datum = NA, expand = F)

  ggiraph( ggobj = gpi, width = 1)

Geneva map only with water bodies

gva <- shp_ch_geodata$municipalities %>% filter(KTNR == 25)

# get water bodies (river & lake) from SITG
library(esri2sf)
url <- 'https://ge.ch/sitgags1/rest/services/VECTOR/SITG_OPENDATA_02/MapServer/6186'
water <- esri2sf(url)

ggplot() + 
 geom_sf(
   data = gva,
   aes(fill = GMDNR), lwd = 0, colour = "#0d0d0d"
 ) + 
 geom_sf(
   data = water, lwd = 0.65, fill = "lightblue", 
   alpha = 1, colour = "lightblue"
 ) +
 theme_map() +
 coord_sf(datum = NA, expand = F)
# gva <- shp_ch_geodata$municipalities %>% filter(KTNR == 25)
# get Geneva's city sectors/quartiers
shp_quartiers <- shp_path(dirGeo = "CH/quartiers")
geq <- st_read(shp_quartiers, layer = "quart17") %>% 
  filter(GMDE == 6621) %>% 
  select(NR, NAME, GMDE) 

inlet_geo <- shiftScale_geo(geo = geq, scaleF = 3, shiftM = c(-11000, 16000))
# Create circles sf to encompass the inlet map and its original location
geo_subset_coord <- encircle_coord(geq)
inlet_coord <- encircle_coord(inlet_geo)
cone_sfc <- cone_geo(geo_subset_coord, inlet_coord)
circle_zoom_sfc <- encircle_geo(geo_subset_coord)
circle_inlet_sfc <- encircle_geo(inlet_coord)


# plot
gp <- ggplot() +
  geom_sf(data = cone_sfc, fill = "lightgrey", lwd = 0, alpha = 0.2) +  
  geom_sf(data = circle_inlet_sfc, fill = "lightgrey", lwd = 0, alpha = 0.5) +
  geom_sf(data = gva, aes(fill = GMDNAME), 
    lwd = 0.05, colour = "#0d0d0d") +
  geom_sf(data = gva %>% filter(GMDNR == 6621), 
          fill = NA, lwd = 0.5, colour = "#0d0d0d") +
  geom_sf(data = inlet_geo, aes(fill = NAME), lwd = 0.1, colour = "#0d0d0d") +
   geom_sf(
   data = water, lwd = 0.65, fill = "lightblue", 
   alpha = 1, colour = "lightblue"
 )  +
    geom_sf(data = circle_zoom_sfc, fill = NA, lwd = 0.3, alpha = 0, colour = "grey")

  gp + 
    theme_map() +
    theme(legend.position = "none") +
    scale_fill_viridis_d()  +
    coord_sf(datum = NA, expand = F)


d-qn/taMap documentation built on Jan. 29, 2020, 2:07 a.m.