Giter Site home page Giter Site logo

ggmapinset's Introduction

ggmapinset

r-universe status CRAN status 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’ 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.

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)

# find the centroid of the specified county
inset_centre <- sf::st_centroid(sf::st_geometry(nc)[nc$NAME == "Yancey"])

# pick some counties to label
labelled_counties <- sample(nc$NAME, 10)

# the basic ggplot example:
base_plot <- ggplot(nc) +
  geom_sf(aes(fill = AREA)) +
  geom_sf_label(aes(label = NAME), data = ~dplyr::filter(.x, NAME %in% labelled_counties)) +
  coord_sf()

# the same plot with an inset zooming in on one area:
ggplot(nc) +
  # replace sf layers with their `_inset` versions
  geom_sf_inset(aes(fill = AREA)) +
  # add the inset frame (the two circles with the connecting lines)
  geom_inset_frame() +
  geom_sf_label_inset(aes(label = NAME), data = ~dplyr::filter(.x, NAME %in% labelled_counties)) +
  # configure the inset in the coordinate system so that all layers can see it
  coord_sf_inset(inset = configure_inset(centre = inset_centre, scale = 2, units = "mi",
                                         translation = c(70, -180), radius = 50))

For more information, see the online documentation and vignette("ggmapinset").

Alternatives

  • ggforce::facet_zoom()
  • ggmagnify::geom_magnify()

ggmapinset's People

Contributors

arcresu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ggmapinset's Issues

Inset titles

Inset definitions could include a title that is rendered (perhaps on the inset frame layer) to describe or label the inset.

Coordinates (by design) not correct for rescaled insets

The insets work by transforming geometries, meaning that the coordinate system is wrong. Things like scale bars (e.g. from ggspatial) or the graticule could be misleading for the insets. It would at least be good to support an inset-aware scale bar geom.

Release ggmapinset 0.3.0

Prepare for release:

  • git pull
  • Check current CRAN check results
  • Check if any deprecation processes should be advanced, as described in Gradual deprecation
  • Polish NEWS
  • devtools::build_readme()
  • urlchecker::url_check()
  • devtools::check(remote = TRUE, manual = TRUE)
  • devtools::check_win_devel()
  • rhub::check_for_cran()
  • revdepcheck::revdep_check(num_workers = 4)
  • Update cran-comments.md
  • git push
  • Draft blog post

Submit to CRAN:

  • usethis::use_version('minor')
  • devtools::submit_cran()
  • Approve email

Wait for CRAN...

  • Accepted 🎉
  • git push
  • usethis::use_github_release()
  • usethis::use_dev_version()
  • git push
  • Finish blog post
  • Tweet
  • Add link to blog post in pkgdown news menu

Rectangular inset configurations

The groundwork is partly in place. We should be able to require that in configure_inset() either centre=, radius= are specified for a circle, or centre= , width=, height = for a rectangle. Instead of bitangents, the connecting lines would just need to work out the two closest corners and connect them.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.