Skip to contents

Verify the integrity of the legends object

Usage

verify_legends(legends, palettes, column_info, data)

Arguments

legends

A list of legends to add to the plot. Each entry in column_info$legend should have a corresponding entry in this object. Each entry should be a list with the following names:

  • palette (character): The palette to use for the legend. Must be a value in palettes.

  • geom (character): The geom of the legend. Must be one of: "funkyrect", "circle", "rect", "bar", "pie", "text", "image".

  • title (character, optional): The title of the legend. Defaults to the palette name.

  • enabled (logical, optional): Whether or not to add the legend. Defaults to TRUE.

  • labels (character, optional): The labels to use for the legend. The defaults depend on the selected geom.

  • size (numeric, optional): The size of the listed geoms. The defaults depend on the selected geom.

  • color (character, optional): The color of the listed geoms. The defaults depend on the selected geom.

  • values (optional): Used as values for the text and image geoms.

  • label_width (numeric, optional): The width of the labels (only when geom is text or pie). Defaults to 1 for text and 2 for images.

  • value_width (numeric, optional): The width of the values (only for geom = "text"). Defaults to 2.

  • label_hjust (numeric, optional): The horizontal alignment of the labels (only when geom is circle, rect or funkyrect). Defaults to 0.5.

palettes

A named list of palettes. Each entry in column_info$palette should have an entry in this object. If an entry is missing, the type of the column will be inferred (categorical or numerical) and one of the default palettes will be applied. Alternatively, the name of one of the standard palette names can be used:

  • numerical: "Greys", "Blues", "Reds", "YlOrBr", "Greens"

  • categorical: "Set3", "Set1", "Set2", "Dark2"

column_info

A data frame describing which columns in data to plot. This data frame should contain the following columns:

  • id (character, required): A column name in data to plot. Determines the size of the resulting geoms, and also the color unless color is specified.

  • id_color (character): A column name in data to use for the color of the resulting geoms. If NA, the id column will be used.

  • id_size (character): A column name in data to use for the size of the resulting geoms. If NA, the id column will be used.

  • name (character): A label for the column. If NA or "", no label will be plotted. If this column is missing, id will be used to generate the name column.

  • geom (character): The geom of the column. Must be one of: "funkyrect", "circle", "rect", "bar", "pie", "text" or "image". For "text", the corresponding column in data must be a character. For "pie", the column must be a list of named numeric vectors. For all other geoms, the column must be a numeric.

  • group (character): The grouping id of each column, must match with column_groups$group. If this column is missing or all values are NA, columns are assumed not to be grouped.

  • palette (character): Which palette to colour the geom by. Each value should have a matching value in palettes$palette.

  • width: Custom width for this column (default: 1).

  • overlay: Whether to overlay this column over the previous column. If so, the width of that column will be inherited.

  • legend: Whether or not to add a legend for this column.

  • hjust: Horizontal alignment of the bar, must be between [0,1] (only for geom = "bar").

  • vjust: Vertical alignment of the label, must be between [0,1] (only for geom = "text").

  • size: Size of the label, must be a numeric value (only for geom = "text").

  • label: Which column to use as a label (only for geom = "text").

  • directory: Which directory to use to find the images (only for geom = "image").

  • extension: The extension of the images (only for geom = "image").

  • draw_outline: Whether or not to draw bounding guides (only for geom == "bar"). Default: TRUE.

  • options (list or json): Any of the options above. Any values in this column will be spread across the other columns. This is useful for not having to provide a data frame with 1000s of columns. This column can be a json string.

data

A data frame with items by row and features in the columns. Must contain one column named "id".

Value

The legends object in the expected format.

Examples

library(tibble)
library(grDevices)
library(RColorBrewer)

# explicit form
data <- tribble(
  ~id, ~name, ~x, ~y,
  "foo", "Foo", 0.5, 0.7,
  "bar", "Bar", 1.0, 0.1
)
column_info <- tribble(
  ~id, ~geom, ~palette,
  "name", "text", NA,
  "foo", "funkyrect", "pal1",
  "bar", "funkyrect", "pal2"
)
palettes <- list(
  pal1 = rev(brewer.pal(9, "Greys")[-1]),
  pal2 = rev(brewer.pal(9, "Reds")[-8:-9])
)
legends <- list()
verify_legends(legends, palettes, column_info, data)
#>  Some palettes were not used in the column info, adding legends for them.
#>  Legend 1 did not contain a geom, inferring from the column info.
#>  Legend 1 did not contain labels, inferring from the geom.
#>  Legend 1 did not contain size, inferring from the labels.
#>  Legend 1 did not contain color, inferring from the palette.
#>  Legend 2 did not contain a geom, inferring from the column info.
#>  Legend 2 did not contain labels, inferring from the geom.
#>  Legend 2 did not contain size, inferring from the labels.
#>  Legend 2 did not contain color, inferring from the palette.
#> [[1]]
#> [[1]]$title
#> [1] "pal1"
#> 
#> [[1]]$palette
#> [1] "pal1"
#> 
#> [[1]]$enabled
#> [1] TRUE
#> 
#> [[1]]$geom
#> [1] "funkyrect"
#> 
#> [[1]]$labels
#>  [1] "0"   ""    "0.2" ""    "0.4" ""    "0.6" ""    "0.8" ""    "1"  
#> 
#> [[1]]$size
#>  [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
#> 
#> [[1]]$color
#>  [1] "#000000" "#252525" "#252525" "#525252" "#737373" "#737373" "#969696"
#>  [8] "#BDBDBD" "#D9D9D9" "#D9D9D9" "#F0F0F0"
#> 
#> [[1]]$label_hjust
#>  [1] 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
#> 
#> 
#> [[2]]
#> [[2]]$title
#> [1] "pal2"
#> 
#> [[2]]$palette
#> [1] "pal2"
#> 
#> [[2]]$enabled
#> [1] TRUE
#> 
#> [[2]]$geom
#> [1] "funkyrect"
#> 
#> [[2]]$labels
#>  [1] "0"   ""    "0.2" ""    "0.4" ""    "0.6" ""    "0.8" ""    "1"  
#> 
#> [[2]]$size
#>  [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
#> 
#> [[2]]$color
#>  [1] "#CB181D" "#EF3B2C" "#EF3B2C" "#FB6A4A" "#FB6A4A" "#FC9272" "#FCBBA1"
#>  [8] "#FCBBA1" "#FEE0D2" "#FEE0D2" "#FFF5F0"
#> 
#> [[2]]$label_hjust
#>  [1] 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
#> 
#>