Skip to content

Set Legend for a Gene Chart ​

This function configures the legend for a gene chart. It allows toggling the legend's visibility, setting a background color, and assigning custom labels for the legend entries. The function can also handle additional customizations through various options.

Usage ​

r
GC_legend(
  GC_chart,
  group = NULL,
  show = TRUE,
  backgroundColor = NULL,
  order = list(),
  position = NULL,
  align = NULL,
  style = list(),
  legendOptions = list(),
  legendTextOptions = list(),
  corner_radius = NULL,
  groupStyle = NULL,
  ...
)

Arguments ​

ArgumentDescription
GC_chartThe gene chart object to be modified.
groupOptional; character or NULL, specifies the groups to include in the legend. If NULL, groups are taken from the 'group' attribute of the 'GC_chart' object.
showLogical, specifies whether to display the legend.
backgroundColorString, the background color of the legend. Same as style = list(backgroundColor = ...), which takes precedence. Default is NULL: as set by the theme, otherwise transparent.
orderOptional; list, specifies the order of the legend labels.
positionCharacter. Position of the legend, either "top" or "bottom". Default is NULL: the position of the theme, otherwise "bottom".
alignCharacter. Horizontal alignment of the legend items, "left", "center" or "right", like the title. Each row of a horizontal legend is aligned on its own. x moves the legend from this position. Default is NULL: the alignment of the theme, otherwise "left".
styleA list of CSS styles to be applied to the chart container. Each element of the list should be a valid CSS property-value pair. For example, list(backgroundColor = "white", border = "2px solid black"). Default is an empty list.
legendOptionsList, additional options for the legend.
legendTextOptionsList, additional text options for the legend.
corner_radiusNumeric or NULL, radius in pixels of the rounded corners of the legend markers, e.g. 3. Default is NULL: square markers.
groupStyleNamed list of styles by group, applied to the text of the legend entry of that group, e.g. list(tnsA = list(fontStyle = "italic")) for italic gene names. Styles in marker go to its marker, e.g. list(A = list(marker = list(stroke = "black"))).
...Additional arguments to be passed to the legend configuration.

Value ​

Returns the modified gene chart object with the legend configured.

Examples ​

r
genes_data <- data.frame(
  start = c(10, 90, 130, 170, 210),
  end = c(40, 120, 160, 200, 240),
  name = c('Gene 1', 'Gene 3', 'Gene 4', 'Gene 5', 'Gene 6'),
  group = c('A', 'B', 'B', 'A', 'C'),
  cluster = c('A', 'A', 'A', 'B', 'B')
)

# Customize legend
GC_chart(genes_data, cluster = "cluster", group = "group", height = "200px") %>%
  GC_legend(
    position = "top", #bottom
    orientation = "horizontal", #vertical
    align = "center", #left / right
    x = 0,
    y = 0,
    adjustHeight = TRUE,
    backgroundColor = "#0000",
    order = list(),
    style = list(
      backgroundColor = "#0000"
      # Any other CSS styles
    ),
    legendOptions = list(
      cursor = "pointer",
      colorScheme = NULL,
      customColors = NULL # c("red", "green", "orange")
      # Additional styles
    ),
    legendTextOptions = list(
      cursor = "pointer",
      textAnchor = "start",
      dy = ".35em",
      fontSize = "12px",
      fontFamily = "sans-serif",
      fill = "black"
      # Additional styles
    )
  )