@@ -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
4556library(ggmapinset )
4657library(ggplot2 )
4758
59+ # load the North Carolina map example shipped with sf
4860nc <- 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:
5775ggplot(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") ` .
0 commit comments