---
title: "`ggswissmaps` Intro"
author: "gibo"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{`ggswissmaps` Intro}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  dev = "png"
)
```



## Examples

### Using the data

```{r}
library(ggswissmaps)

data("shp_df")
class(shp_df)
length(shp_df)
names(shp_df)
```

```{r eval=FALSE}
# Data description
?shp_df
```


### Some maps

```{r}
names(maps2)

# By name
maps2[["g1k15"]]

# By index
maps2[[5]]
```

The objects contained in `maps2` are `ggplot` objects. They have been created with `ggplot2::ggplot` plus a `ggplot2::geom_path` layer with the data in `shp_df`. As an example, the previous map is the same as:

```{r}
library(ggplot2)
ggplot(shp_df[["g1k15"]], aes(x = long, y = lat, group = group)) +
  geom_path() +
  coord_equal() +
  theme_white_f() +
  labs(x = NULL, y = NULL, caption = "Boundaries: BFS GEOSTAT / swisstopo")
```


### Extract a subset of a territory and make a map

The `maps2` object, used above, is a list with some maps of swiss territory at various levels (grand regions, cantons, districts, ...).

What if one wants to draw a map with a sub-territory? For example, what if I want to have a map with the districts of two cantons? First, I have to select the desired subset from the `shp_df` data, and then will apply the `maps2_` function to it. 

```{r message=FALSE}
# Data frame with the coordinates of all swiss districts
d <- shp_df[["g1b15"]]

# Look at the structure of the data frame
str(d)

# The cantons are identified by the KTNR column

# Extract from this data the districts of two cantons
library(dplyr)
d <- d %>% dplyr::filter(KTNR %in% c(18, 21))

# And draw the map
maps2_(d)
```





