-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
153 lines (123 loc) · 4.65 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
library(sf, quietly = TRUE)
set.seed(2023)
```
# ggmapinset
<!-- badges: start -->
[![r-universe status](https://cidm-ph.r-universe.dev/badges/ggmapinset)](https://cidm-ph.r-universe.dev)
[![CRAN status](https://www.r-pkg.org/badges/version/ggmapinset)](https://CRAN.R-project.org/package=ggmapinset)
[![R-CMD-check](https://github.com/cidm-ph/ggmapinset/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/cidm-ph/ggmapinset/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
Add zoomed inset panels to your ggplot maps.
## Installation
You can install ggmapinset like so:
``` r
# CRAN release
install.packages('ggmapinset')
# development version
install.packages('ggmapinset', repos = c('https://cidm-ph.r-universe.dev', 'https://cloud.r-project.org'))
```
## Replacing 'ggplot2' sf layers
`{ggmapinset}` provides drop-in replacements for each of the `{sf}`-related layers
from `{ggplot2}`:
| `ggplot2` function | `ggmapinset` replacement |
|:------------------------|:------------------------------|
| `geom_sf()` | `geom_sf_inset()` |
| `geom_sf_text()` | `geom_sf_text_inset()` |
| `geom_sf_label()` | `geom_sf_label_inset()` |
| `stat_sf()` | `stat_sf_inset()` |
| `stat_sf_coordinates()` | `stat_sf_coordinates_inset()` |
| `coord_sf()` | `coord_sf_inset()` |
The replacements work the same as their normal versions but copy, zoom, and clip
the layers to make the inset work. The stats can be used to add inset support to
geoms from third-party packages. For extension developers, tools are provided to
make `{sf}`-based layers inset-aware (see `{ggautomap}` for examples).
## Example
This example adds an inset to the first example from `ggplot2::geom_sf()`.
The inset area is defined as a circle centred on the named county, with radius
50 miles. The inset is enlarged by a factor of 2 and shifted to an empty part
of the map.
```{r}
library(ggmapinset)
library(ggplot2)
# load the North Carolina map example shipped with sf
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
inset_cfg <- configure_inset(
shape_circle(
centre = sf::st_centroid(nc[nc$NAME == "Yancey",]),
radius = 50
),
scale = 2,
units = "mi",
translation = c(70, -180)
)
# pick some counties to label
labelled_counties <- sample(nc$NAME, 10)
data_subset <- function(df) df[df$NAME %in% labelled_counties,]
```
<div style="display: flex; gap: 1em;">
<div style="flex-grow: 1">
```{r, eval=FALSE}
# base ggplot
ggplot(nc) +
geom_sf(aes(fill = AREA)) +
geom_sf_label(
aes(label = NAME),
data = data_subset
) +
coord_sf()
```
</div>
<div style="flex-grow: 1">
```{r, eval=FALSE}
# with inset added
ggplot(nc) +
geom_sf_inset(aes(fill = AREA)) +
geom_inset_frame() +
geom_sf_label_inset(
aes(label = NAME),
data = data_subset
) +
coord_sf_inset(inset_cfg)
```
</div>
</div>
```{r example, echo=FALSE, fig.width=7, fig.height=4}
ggplot(nc) +
geom_sf_inset(aes(fill = AREA)) +
geom_inset_frame() +
geom_sf_label_inset(
aes(label = NAME),
data = data_subset
) +
coord_sf_inset(inset_cfg)
```
For more information, see the [online documentation](https://cidm-ph.github.io/ggmapinset/) and `vignette("ggmapinset")`.
## Limitations
The package implements insets by duplicating and transforming spatial data within a single
coordinate system. That means that you don't get separate grid lines for the inset panel
and, more significantly, the inset can be distorted by the projection of the base map if
you move it too far. This tends not to be a problem in practice if you choose a
coordinate system that isn't too distorted over the area of the base map.
## Alternatives
Other packages implement different approaches:
* [`facet_zoom()`](https://ggforce.data-imaginist.com/reference/facet_zoom.html)
from [ggforce](https://cran.r-project.org/package=ggforce)
* [`geom_magnify()`](https://hughjonesd.github.io/ggmagnify/reference/geom_magnify.html)
from [ggmagnify](https://github.com/hughjonesd/ggmagnify/)
* [`mf_inset_on()`](https://riatelab.github.io/mapsf/reference/mf_inset_on.html)
from [mapsf](https://cran.r-project.org/package=mapsf)
There are also several articles describing more manual ways to achieve different insets:
* https://www.datawim.com/post/inset-map-in-r/
* https://dieghernan.github.io/202203_insetmaps/
* https://upgo.lab.mcgill.ca/2019/12/13/making-beautiful-maps/