nplot_legend: Add legend to a netplot object

View source: R/nplot_legend.R

nplot_legendR Documentation

Add legend to a netplot object

Description

Legends in grid graphics is a bit more complicated than in base graphics. The function nplot_legend is a wrapper of grid::legendGrob() that makes the process easier. Besides labels, the main visual arguments for the figure ar passed through the gp argument (see examples).

Usage

nplot_legend(
  g,
  labels,
  pch,
  gp = grid::gpar(),
  ...,
  packgrob.args = list(side = "left")
)

## S3 method for class 'netplot_legend'
print(x, y = NULL, newpage = TRUE, ...)

Arguments

g

An object of class netplot.

labels

Character vector of labels.

pch

See graphics::points().

gp

An object of class grid::gpar()

...

Further arguments passed to grid::legendGrob().

packgrob.args

List of arguments passed to grid::packGrob().

x

An object of class netplot_legend.

y

Ignored.

newpage

Logical scalar. When TRUE it calls grid::grid.newpage().

Value

A frame grob.

Examples

library(igraph)
library(netplot)
set.seed(1)
x <- sample_smallworld(1, 200, 5, 0.03)
V(x)$nsides <- sample(c(10, 4), 200, replace = TRUE)

g <- nplot(
  x,
  vertex.nsides = V(x)$nsides,
  vertex.color  = ifelse(V(x)$nsides == 4, "red", "steelblue"),
  edge.line.breaks = 5
  )

nplot_legend(
  g,
  labels = c("circle", "diamond", "edge"),
  pch    = c(21, 23, NA),
  gp     = gpar(
    fill = c("steelblue", "red", NA),
    lwd  = c(NA, NA, 1),
    col  = c(NA, NA, "purple")
    )
  )
grid.text("Legend to the left (default)", y = unit(.95, "npc"), just = "bottom")

nplot_legend(
  g,
  labels = c("circle", "diamond", "edge"),
  pch    = c(21, 23, NA),
  gp     = gpar(
    fill = c("steelblue", "red", NA),
    lwd  = c(NA, NA, 1),
    col  = c(NA, NA, "purple")
    ),
  # These two extra options set the legend to the bottom
  packgrob.args = list(side = "bottom"),
  ncol = 3
  )
grid.text("Legend bottom", y = unit(.95, "npc"), just = "bottom")



USCCANA/netplot documentation built on Sept. 24, 2023, 5 p.m.