Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

grid_legend

Legend Function for grid Graphics


Description

This function can be used to add legends to grid-based plots.

Usage

grid_legend(x, y, pch = NA, col = par('col'), labels, frame = TRUE,
            hgap = unit(0.8, "lines"), vgap = unit(0.8, "lines"),
            default_units = "lines", gp = gpar(), draw = TRUE,
            title = NULL, just = 'center', lwd = NA, lty = NA,
            size = 1,
            gp_title = NULL, gp_labels = NULL,
            gp_frame = gpar(fill = "transparent"),
            inset = c(0, 0))

Arguments

x

character string "topright", "topleft", "bottomright", "bottomleft", "top", "bottom", "left", "right", "center" or x coordinate of the legend.

y

y coordinates of the legend.

pch

integer vector of plotting symbols, if any.

col

character vector of colors for the symbols.

labels

character vector of labels corresponding to the symbols.

frame

logical indicating whether the legend should have a border or not.

hgap

object of class "unit" specifying the space between symbols and labels.

vgap

object of class "unit" specifying the space between the lines.

default_units

character string indicating the default unit.

gp

object of class "gpar" used for the legend.

draw

logical indicating whether the legend be drawn or not.

title

character string indicating the plot's title.

just

justification of the legend relative to its (x, y) location. see ?viewport for more details.

lwd

positive number to set the line width. if specified lines are drawn.

lty

line type. if specified lines are drawn.

size

size of the group symbols (in char units).

gp_title

object of class "gpar" used for the title.

gp_labels

object of class "gpar" used for the labels.

gp_frame

object of class "gpar" used for the frame.

inset

numeric vector of length 2 specifying the inset of the legend in npc units, relative to the specified x and y coordinates.

Value

Invisibly, the legend as a "grob" object.

Author(s)

See Also

Examples

data("Lifeboats")
attach(Lifeboats)
ternaryplot(Lifeboats[,4:6],
  pch = ifelse(side == "Port", 1, 19),
  col = ifelse(side == "Port", "red", "blue"),
  id  = ifelse(men / total > 0.1, as.character(boat), NA),
  prop_size = 2,
  dimnames_position = "edge",
  main = "Lifeboats on Titanic")
grid_legend(0.8, 0.9, c(1, 19), c("red", "blue"),
  c("Port", "Starboard"), title = "SIDE")


grid.newpage()
pushViewport(viewport(height = .9, width = .9 ))
grid.rect(gp = gpar(lwd = 2, lty = 2))

grid_legend(x = unit(.05,'npc'),
            y = unit(.05,'npc'),
            just = c(0,0),
            pch = c(1,2,3),
            col = c(1,2,3),
            lwd=NA, 
            lty=NA,
            labels = c("b",'r','g'),
            title = NULL,
            gp=gpar(lwd=2, cex=1),
            hgap = unit(.8, "lines"),
            vgap = unit(.9, "lines"))

grid_legend(x = unit(1,'npc'),
            y = unit(1,'npc'),
            just = c(1,1),
            pch = NA,
            col = c(1,2,3,4),
            lwd=c(1,1,1,3), 
            lty=c(1,2,1,3),
            labels = c("black",'red','green','blue'),
            gp_labels = list(gpar(col = 1), gpar(col = 2), gpar(col = 3), gpar(col = 4)),
            title = NULL,
            gp=gpar(lwd=2, cex=1),
            hgap = unit(.8, "lines"),
            vgap = unit(.9, "lines"))

grid_legend(x = 'topleft',
            pch = c(1,NA,2,NA),
            col = c(1,2,3,4),
            lwd=NA, 
            lty=c(NA,2,NA,3),
            labels = c("black",'red','green','blue'),
            title = 'Some LONG Title',
            gp_title = gpar(col = 3),
            gp_frame = gpar(col = 4, lty = 2, fill = "transparent"),
            gp_labels = gpar(col = 6),
            gp=gpar(lwd=2, cex=2, col = 1),
            hgap = unit(.8, "lines"),
            vgap = unit(.9, "lines"))

grid_legend(x = .7,
            y = .7,
            pch = c(1,NA,2,NA),
            col = c(1,2,3,4),
            lwd=1, 
            lty=c(NA,2,NA,3),
            labels = c("black",'red','green','blue'),
            title = 'short T',
            gp=gpar(lwd=1, cex=.7,col = 1),
            hgap = unit(.8, "lines"),
            vgap = unit(.9, "lines"))

grid_legend(x = 'bottomright',
            pch = c(1,NA,2,NA),
            col = c(2),
            lwd=NA, 
            lty=c(NA,2,NA,3),
            labels = c("black",'red','green','blue'),
            title = NULL,
            gp=gpar(lwd=2, cex=1,col = 1),
            hgap = unit(.8, "lines"),
            vgap = unit(.9, "lines"))

vcd

Visualizing Categorical Data

v1.4-10
GPL-2
Authors
David Meyer [aut, cre], Achim Zeileis [aut] (<https://orcid.org/0000-0003-0918-3766>), Kurt Hornik [aut], Florian Gerber [ctb], Michael Friendly [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.