Skip to content

Commit df94ec5

Browse files
committed
Improve documentation
1 parent 6e70062 commit df94ec5

File tree

7 files changed

+116
-39
lines changed

7 files changed

+116
-39
lines changed

NEWS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# ggmapinset 0.3.0
22

3-
* Added `transform_to_inset()` helper for applying the inset transformation to
4-
arbitrary geometries.
53
* Replaced the confusing `inset_clip` and `inset_copy` parameters of
64
`geom_sf_inset()` with the new `map_base` and `map_inset` parameters.
75
* Inset frame aesthetics can now be specified consistently.
@@ -11,6 +9,8 @@
119
coordinate limits and computes some inset-related variables for downstream use.
1210
* The new `stat_sf_coordinates_inset()`, `stat_sf_text_inset()`, and
1311
`stat_sf_label_inset()` labels complete the coverage of sf-related layers.
12+
* Added `transform_to_inset()` helper for applying the inset transformation to
13+
arbitrary geometries (for extension developers).
1414

1515
# ggmapinset 0.2.3
1616

R/coord_sf_inset.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#' @param inset Inset configuration; see [configure_inset()].
99
#' @param ... Arguments passed to [ggplot2::coord_sf()]
1010
#'
11+
#' @returns A ggplot coordinate object to be added to a plot.
1112
#' @seealso [geom_sf_inset()]
1213
#' @export
1314
#'

README.Rmd

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,27 @@ install.packages('ggmapinset', repos = c('https://cidm-ph.r-universe.dev', 'http
3838

3939
## Replacing 'ggplot2' sf layers
4040

41-
`{ggmapinset}` provides replacements for each of the sf-related layers from
42-
`{ggplot2}` with the suffix `_inset` added to the names.
43-
For the most part they work the same way as the standard layers, but they can
44-
understand the inset configuration and make small adjustments accordingly such as
45-
expanding axes to fit the inset frame or duplicating the layer and transforming it
46-
to fit into the inset viewport.
41+
`{ggmapinset}` provides drop-in replacements for each of the `{sf}`-related layers
42+
from `{ggplot2}`:
43+
44+
| ‘ggplot2’ function | ‘ggmapinset’ replacement |
45+
|:------------------------|:------------------------------|
46+
| `geom_sf()` | `geom_sf_inset()` |
47+
| `geom_sf_text()` | `geom_sf_text_inset()` |
48+
| `geom_sf_label()` | `geom_sf_label_inset()` |
49+
| `stat_sf()` | `stat_sf_inset()` |
50+
| `stat_sf_coordinates()` | `stat_sf_coordinates_inset()` |
51+
| `coord_sf()` | `coord_sf_inset()` |
52+
53+
The replacements work the same as their normal versions but copy, zoom, and clip
54+
the layers to make the inset work. The stats can be used to add inset support to
55+
geoms from third-party packages. For extension developers, tools are provided to
56+
make `{sf}`-based layers inset-aware (see `{ggautomap}` for examples).
57+
4758

4859
## Example
4960

50-
This example adds an inset to the first example from `ggplot2::geom_sf`.
61+
This example adds an inset to the first example from `ggplot2::geom_sf()`.
5162
The inset area is defined as a circle centred on the named county, with radius
5263
50 miles. The inset is enlarged by a factor of 2 and shifted to an empty part
5364
of the map.
@@ -56,20 +67,31 @@ of the map.
5667
library(ggmapinset)
5768
library(ggplot2)
5869
70+
# load the North Carolina map example shipped with sf
5971
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
6072
61-
# The basic ggplot example:
62-
# ggplot(nc) +
63-
# geom_sf(aes(fill = AREA)) +
64-
# coord_sf()
73+
# find the centroid of the specified county
74+
inset_centre <- sf::st_centroid(sf::st_geometry(nc)[nc$NAME == "Yancey"])
6575
66-
# Adding an inset means replacing geom_sf(...) -> geom_sf_inset(...) and
67-
# coord_sf(...) -> coord_sf_inset(..., inset = configure_inset(...))
76+
# pick some counties to label
77+
labelled_counties <- sample(nc$NAME, 10)
78+
79+
# the basic ggplot example:
80+
base_plot <- ggplot(nc) +
81+
geom_sf(aes(fill = AREA)) +
82+
geom_sf_label(aes(label = NAME), data = ~dplyr::filter(.x, NAME %in% labelled_counties)) +
83+
coord_sf()
84+
85+
# the same plot with an inset zooming in on one area:
6886
ggplot(nc) +
87+
# replace sf layers with their `_inset` versions
6988
geom_sf_inset(aes(fill = AREA)) +
89+
# add the inset frame (the two circles with the connecting lines)
7090
geom_inset_frame() +
71-
geom_sf_label_inset(aes(label = NAME), data = ~dplyr::slice_sample(.x, n = 10)) +
72-
coord_sf_inset(inset = configure_inset(
73-
centre = sf::st_centroid(sf::st_geometry(nc)[nc$NAME == "Yancey"]),
74-
scale = 2, translation = c(70, -180), radius = 50, units = "mi"))
91+
geom_sf_label_inset(aes(label = NAME), data = ~dplyr::filter(.x, NAME %in% labelled_counties)) +
92+
# configure the inset in the coordinate system so that all layers can see it
93+
coord_sf_inset(inset = configure_inset(centre = inset_centre, scale = 2, units = "mi",
94+
translation = c(70, -180), radius = 50))
7595
```
96+
97+
For more information, see the [online documentation](https://cidm-ph.github.io/ggmapinset/) and `vignette("ggmapinset")`.

README.md

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,40 +27,64 @@ install.packages('ggmapinset', repos = c('https://cidm-ph.r-universe.dev', 'http
2727

2828
## Replacing ‘ggplot2’ sf layers
2929

30-
`{ggmapinset}` provides replacements for each of the sf-related layers
31-
from `{ggplot2}` with the suffix `_inset` added to the names. For the
32-
most part they work the same way as the standard layers, but they can
33-
understand the inset configuration and make small adjustments
34-
accordingly such as expanding axes to fit the inset frame or duplicating
35-
the layer and transforming it to fit into the inset viewport.
30+
`{ggmapinset}` provides drop-in replacements for each of the
31+
`{sf}`-related layers from `{ggplot2}`:
32+
33+
| ‘ggplot2’ function | ‘ggmapinset’ replacement |
34+
|:------------------------|:------------------------------|
35+
| `geom_sf()` | `geom_sf_inset()` |
36+
| `geom_sf_text()` | `geom_sf_text_inset()` |
37+
| `geom_sf_label()` | `geom_sf_label_inset()` |
38+
| `stat_sf()` | `stat_sf_inset()` |
39+
| `stat_sf_coordinates()` | `stat_sf_coordinates_inset()` |
40+
| `coord_sf()` | `coord_sf_inset()` |
41+
42+
The replacements work the same as their normal versions but copy, zoom,
43+
and clip the layers to make the inset work. The stats can be used to add
44+
inset support to geoms from third-party packages. For extension
45+
developers, tools are provided to make `{sf}`-based layers inset-aware
46+
(see `{ggautomap}` for examples).
3647

3748
## Example
3849

39-
This example adds an inset to the first example from `ggplot2::geom_sf`.
40-
The inset area is defined as a circle centred on the named county, with
41-
radius 50 miles. The inset is enlarged by a factor of 2 and shifted to
42-
an empty part of the map.
50+
This example adds an inset to the first example from
51+
`ggplot2::geom_sf()`. The inset area is defined as a circle centred on
52+
the named county, with radius 50 miles. The inset is enlarged by a
53+
factor of 2 and shifted to an empty part of the map.
4354

4455
``` r
4556
library(ggmapinset)
4657
library(ggplot2)
4758

59+
# load the North Carolina map example shipped with sf
4860
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
4961

50-
# The basic ggplot example:
51-
# ggplot(nc) +
52-
# geom_sf(aes(fill = AREA)) +
53-
# coord_sf()
62+
# find the centroid of the specified county
63+
inset_centre <- sf::st_centroid(sf::st_geometry(nc)[nc$NAME == "Yancey"])
5464

55-
# Adding an inset means replacing geom_sf(...) -> geom_sf_inset(...) and
56-
# coord_sf(...) -> coord_sf_inset(..., inset = configure_inset(...))
65+
# pick some counties to label
66+
labelled_counties <- sample(nc$NAME, 10)
67+
68+
# the basic ggplot example:
69+
base_plot <- ggplot(nc) +
70+
geom_sf(aes(fill = AREA)) +
71+
geom_sf_label(aes(label = NAME), data = ~dplyr::filter(.x, NAME %in% labelled_counties)) +
72+
coord_sf()
73+
74+
# the same plot with an inset zooming in on one area:
5775
ggplot(nc) +
76+
# replace sf layers with their `_inset` versions
5877
geom_sf_inset(aes(fill = AREA)) +
78+
# add the inset frame (the two circles with the connecting lines)
5979
geom_inset_frame() +
60-
geom_sf_label_inset(aes(label = NAME), data = ~dplyr::slice_sample(.x, n = 10)) +
61-
coord_sf_inset(inset = configure_inset(
62-
centre = sf::st_centroid(sf::st_geometry(nc)[nc$NAME == "Yancey"]),
63-
scale = 2, translation = c(70, -180), radius = 50, units = "mi"))
80+
geom_sf_label_inset(aes(label = NAME), data = ~dplyr::filter(.x, NAME %in% labelled_counties)) +
81+
# configure the inset in the coordinate system so that all layers can see it
82+
coord_sf_inset(inset = configure_inset(centre = inset_centre, scale = 2, units = "mi",
83+
translation = c(70, -180), radius = 50))
6484
```
6585

6686
<img src="man/figures/README-example-1.png" width="100%" />
87+
88+
For more information, see the [online
89+
documentation](https://cidm-ph.github.io/ggmapinset/) and
90+
`vignette("ggmapinset")`.

man/coord_sf_inset.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/figures/README-example-1.png

-2 Bytes
Loading

vignettes/ggmapinset.Rmd

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,30 @@ ggplot(nc) +
6464
translation = c(100, -120), radius = 50, units = "mi"))
6565
```
6666

67+
## Multiple insets
68+
For multiple insets, the appropriate inset configuration just needs to be passed to each
69+
layer separately. It's probably clearer to avoid providing an inset to the coordinate
70+
system in this case.
71+
72+
Since the inset-aware layers will duplicate themselves for the base and inset maps,
73+
you will probably want to disable that behaviour with `map_base = "none"` to avoid
74+
having multiple identical copies of the base map.
75+
```{r multiple, fig.width=7, fig.height=5}
76+
inset1 <- configure_inset(
77+
centre = sf::st_centroid(sf::st_geometry(nc)[nc$NAME == "Bladen"]), scale = 1.5,
78+
translation = c(150, -50), radius = 50, units = "mi")
79+
inset2 <- configure_inset(
80+
centre = sf::st_centroid(sf::st_geometry(nc)[nc$NAME == "Orange"]), scale = 3,
81+
translation = c(30, 120), radius = 30, units = "mi")
82+
83+
ggplot(nc) +
84+
# base map
85+
geom_sf_inset() +
86+
# inset 1
87+
geom_sf_inset(map_base = "none", inset = inset1) +
88+
geom_inset_frame(inset = inset1, colour = "red") +
89+
# inset 2
90+
geom_sf_inset(map_base = "none", inset = inset2) +
91+
geom_inset_frame(inset = inset2, colour = "blue")
92+
```
93+

0 commit comments

Comments
 (0)