MyNixOS website logo
Description

Add Inset Panels to Maps.

Helper to add insets based on geom_sf() from 'ggplot2'. This package gives you a drop-in replacement for geom_sf() that supports adding a zoomed inset map without having to create and embed a separate plot.

ggmapinset

r-universestatus CRANstatus R-CMD-check

Add zoomed inset panels to your ggplot maps.

Installation

You can install ggmapinset like so:

# 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 functionggmapinset 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.

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)
)
#> Warning: st_centroid assumes attributes are constant over geometries

# pick some counties to label
labelled_counties <- sample(nc$NAME, 10)
data_subset <- function(df) df[df$NAME %in% labelled_counties,]
# base ggplot
ggplot(nc) +
  geom_sf(aes(fill = AREA)) +
  
  geom_sf_label(
    aes(label = NAME),
    data = data_subset
  ) +
  coord_sf()
# 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)

For more information, see the online documentation 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:

There are also several articles describing more manual ways to achieve different insets:

Metadata

Version

0.4.0

License

Unknown

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows