Package 'inlegend'

Title: Inset legend themes for ggplot2 maps
Description: Provides themes for making ggplot2 maps with inset legends and without whitespace around the margins.
Authors: Miles McBain [aut, cre]
Maintainer: Miles McBain <[email protected]>
License: MIT + file LICENSE
Version: 0.1.0
Built: 2024-11-23 03:02:06 UTC
Source: https://github.com/milesmcbain/inlegend

Help Index


build custom inset map legend.

Description

Build a custom inset map legend

Usage

inset_legend_custom(
  inlegend.location = c("top-right", "top-left", "bottom-left", "bottom-right"),
  legend.background,
  legend.text,
  legend.title,
  legend.key,
  legend.margin = ggplot2::margin(8, 8, 10, 8),
  ...
)

Arguments

inlegend.location

the location of the legend. One of "top-right", "top-left", "bottom-left", "bottom-right"

legend.background

the background rectangle the legend is drawn on. A ggplot::element_rect, where the following parameter mappings map to what you see:

  • fill - background colour

  • colour - border colour

  • size - border thickness.

legend.text

text for labels, eg factor names, or scale tick labels. A ggplot2::element_text, where the following parameters map to what you see:

  • colour - the text colour

  • size - the text size

  • face - the text face (e.g. bold)

  • family - the text font

  • margin - ggplot2::margin that sets the spacing around the legend element text

legend.title

text for legend title. A ggplot2::element_text, where the following parameters map to what you see:

  • colour - the text colour

  • size - the text size

  • face - the text face (e.g. bold)

  • family - the text font

  • margin - ggplot2::margin that sets the spacing around the title text and legend.background edge.

legend.key

the background rectangles that the individual legend key elements are drawn on. A ggplot2::element_rect where the following parameters map to what you see:

  • fill - the rectangle

legend.margin

The spacing between the edge of the legend.background and legend elements (text, keys etc.). A ⁠ggplot2::margin object⁠. Additional padding on bottom seems to be required for things to look right.

...

arguments passed to ggplot2::theme.

Details

This function has defaults that will position an inset map ggplot2 legend. It is the legend constructor used by inset_legend_light and inset_legend_dark.

Value

a object of classes 'theme' and 'gg'

Author(s)

Miles McBain


inset map legend themes

Description

Inset legend themes for ggplot2 maps

Usage

inset_legend_dark(
  location = "top-right",
  title_text_size = NULL,
  text_size = NULL
)

inset_legend_light(
  location = "top-right",
  title_text_size = NULL,
  text_size = NULL
)

Arguments

location

the location to inset legends. One of "top-left", "top-right", "bottom-left", "bottom-right"

title_text_size

a convenience parameter for setting the size of the legend title text. For high resolution maps the defaults tend to be too small.

text_size

a convenience parameter for setting the size the legend key elements text. For high resolution maps the defaults tend to be too small.

Details

These themes move and restyle the ggplot2 legend to be more in line with mapping nroms. ggplot2 are inset in the desired map corner. The legends are restyled to appear simpler and draw less attention when overlaid on maps.

Convenience parameters are provided to tweak the size of legend text. The text style can be fully customised with the ggplot2 theme engine. See examples.

Value

inlegend_dark returns dark legend theme based on kepler.gl as an object of classes theme and gg.

inlegend_light returns a light white legend theme as an object of classes theme and gg.

Author(s)

Miles McBain

Examples

library(spData)
library(dplyr)
library(ggplot2)
library(ggspatial)
library(inlegend)

coffee_prod <-
  world %>%
  left_join(coffee_data,
    by = "name_long"
  )
ggplot() +
  layer_spatial(
    coffee_prod,
    aes(fill = coffee_production_2017)
  ) +
  theme_cropped_map() +
  inset_legend_light("bottom-left",
    title_text_size = 10,
    text_size = 8
  ) +
  theme(legend.title = element_text(face = "bold")) +
  scale_fill_viridis_b()

theme_cropped_map

Description

Remove plot axis and whitespace margins

Usage

theme_cropped_map(...)

Arguments

...

arguments passed to ggplot2::theme()

Details

In a map where the legend is inset it may be convenient to have the plot tightly cropped around the map extent. This theme give you a such a plot. Beware that there is no space for titles, so that kind of information must be conveyed in section headings or captions.

When saving map using this theme as an image, you will see whitespace in the output unless the aspect ratio of your output image matches the aspect ratio of the map (plot area) exactly.

Author(s)

Miles McBain