customLegend: a function that takes a series of colors/fills/shapes and...

View source: R/graphicR.R

customLegendR Documentation

a function that takes a series of colors/fills/shapes and adds it as a legend to a ggplot object

Description

a function that takes a series of colors/fills/shapes and adds it as a legend to a ggplot object

Usage

customLegend(
  p,
  legend = legendDefinition(),
  legend.title = "Legend",
  legend.title.face = "bold",
  legend.title.size = 12,
  legend.element.size = 12,
  legend.position = "bottom",
  fakePosition = NA,
  axesAsis = TRUE
)

Arguments

p

ggplot object to which the legend has to be added

legend

each row of the legend data.frame is an item in the legend to be created, so the color, shape, etc of a single row 'belong' together The row-order determines the order in the legend

legend.title

defines title of the legend

legend.title.face

defines typography of the legend title

legend.title.size

defines size of the legend title

legend.element.size

defines size of the items in the legend

legend.position

defines the placement of the legend

fakePosition

defines the position of the data point of the fake dataset used to generate the legend (trick). Choose this to be (well) outside of the actual data in the ggplot object p. If NA (not defined), it will be set to (0,0) or (if axesAsis = TRUE) outside the current ranges of the axes

axesAsis

if TRUE then the exes ranges are left as they were, ie the argument fakePoistion will have no influence on the axes ranges. Note that this uses the function ggplot_build() function which seems somewhat experimental, meaning it may work differently in future versions of ggplot2

Value

a ggplot object

Note

use of this function will result in a series of warning messages (one for every item in the legend). This is 'normal' because the function uses a trick to do its thing. Use suprressWarnings(print()) to prevent seeing the messages while running code. In R markdown use the option 'warning=FALSE' to not see the messages

it's advisable to make this customLegend the last thing to be added to the ggplot object p (make customLegend the last in the


BenBruyneel/BBPersonalR documentation built on Aug. 23, 2024, 8:28 p.m.