key_scale: Modification of legend keys for bar plots

View source: R/legends.R

key_scaleR Documentation

Modification of legend keys for bar plots

Description

draw_key_polygon2() is an extension of draw_key_polygon() from ggplot2 to modify the spacing between legend keys on bar graphs and other graphs where the fill aesthetic is used. It does this by scaling down the legend keys within their allotted space.

Usage

key_scale(scale = 0.8)

draw_key_polygon2(data, params, size, scale = 0.8)

Arguments

scale

Scaling factor for the height and width of the legend key within its allotted space. Defaults to 0.8, which gives the key 80 percent of the allotted height and width.

data

A single row data frame containing the scaled aesthetics to display in this key

params

A list of additional parameters supplied to the geom.

size

Width and height of key in mm.

Details

key_scale() is a wrapper function to allow the specific scaling of the legend keys to be modified inside the geom function.

The core part of this approach that differs from the standard ggplot2 polygon key is that the width and height are changed to unit(scale, "npc") instead of unit(1, "npc"), where scale has a default value of 0.8. This makes the width and height of the fill key take up 80 percent (or the scale value, if different) of the key space instead of 100 percent of the key space. This makes the key fill slightly smaller, giving the appearance of increased space between the keys.

Value

A grid grob.

Examples

library(ggplot2)

df <- data.frame(
    x = factor(c("IL6R", "IL8R", "IL6R-Ab", "IL8R-Ab", "IL6R-Ab-IL8R"),
          levels = c("IL6R", "IL8R", "IL6R-Ab", "IL8R-Ab", "IL6R-Ab-IL8R")),
    y = c(0.45, 0.78, 0.61, 0.31, 0.72))
p <- ggplot(df, aes(x, y, fill = x)) +
    geom_col(key_glyph = key_scale())

christyray/crthemes documentation built on April 14, 2023, 11:18 p.m.