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-10-24 03:00:53 UTC |
Source: | https://github.com/milesmcbain/inlegend |
Build a custom inset map legend
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), ... )
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), ... )
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
|
legend.text |
text for labels, eg factor names, or scale tick labels. A
|
legend.title |
text for legend title. A ggplot2::element_text, where the following parameters map to what you see:
|
legend.key |
the background rectangles that the individual legend key elements are drawn on. A
|
legend.margin |
The spacing between the edge of the legend.background and legend elements (text, keys etc.). A |
... |
arguments passed to |
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
.
a object of classes 'theme' and 'gg'
Miles McBain
Inset legend themes for ggplot2 maps
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 )
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 )
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. |
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.
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
.
Miles McBain
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()
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()
Remove plot axis and whitespace margins
theme_cropped_map(...)
theme_cropped_map(...)
... |
arguments passed to |
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.
Miles McBain