The legend() Function with R

The legend() Function

Basic Usage

The legend() function adds a legend to an existing plot, providing a key to interpret the various graphical elements. The basic syntax is: 

legend(x, y, legend, col, pch, lty, lwd, cex, bg)
  • x: The x-coordinate or position for the legend.
  • y: The y-coordinate or position for the legend.
  • legend: A vector of text strings to describe the legend items.
  • col: A vector of colors corresponding to the items in the legend.
  • pch: A vector of plotting characters (symbols) corresponding to the items.
  • lty: A vector of line types (e.g., solid, dashed) corresponding to the items.
  • lwd: A vector of line widths for the lines in the legend.
  • cex: A numerical value to adjust the size of the text in the legend.
  • bg: Background color of the legend box.

Adding a Simple Legend

Here’s an example of adding a basic legend to a plot with different colors and symbols.

Example: 

# Create a base plot
x <- 1:10
y1 <- x
y2 <- x^2
plot(x, y1, type = "b", col = "blue", pch = 16, lty = 1, ylim = c(0, 100),
     xlab = "X", ylab = "Y", main = "Example with Legend")
# Add another series
lines(x, y2, col = "red", pch = 17, lty = 2)
# Add a legend
legend("topleft", legend = c("Linear", "Quadratic"),
       col = c("blue", "red"), pch = c(16, 17), lty = c(1, 2))
  • legend(“topleft”, …): Places the legend in the top-left corner.
  • legend = c(“Linear”, “Quadratic”): Labels for each series.
  • col = c(“blue”, “red”): Colors for the points or lines.
  • pch = c(16, 17): Symbols used in the plot.
  • lty = c(1, 2): Line types for each series.

Customizing the Legend Appearance

You can customize the legend’s appearance using additional arguments.

Example: 

# Create a base plot
plot(x, y1, type = "b", col = "blue", pch = 16, lty = 1, ylim = c(0, 100),
     xlab = "X", ylab = "Y", main = "Customized Legend")
# Add another series
lines(x, y2, col = "red", pch = 17, lty = 2)
# Add a customized legend
legend("topright", legend = c("Linear", "Quadratic"),
       col = c("blue", "red"), pch = c(16, 17), lty = c(1, 2),
       cex = 0.8, bg = "lightgray", box.col = "black")
  • cex = 0.8: Reduces the text size in the legend.
  • bg = “lightgray”: Sets the background color of the legend box.
  • box.col = “black”: Sets the color of the box around the legend.

Positioning the Legend

The x and y arguments in legend() can be specified as positions or coordinates.

  • String Positions: Use strings like “topright”, “bottomleft”, “center” to place the legend in standard locations.
  • Numeric Coordinates: Specify exact coordinates for precise positioning.

Example: 

# Create a base plot
plot(x, y1, type = "b", col = "blue", pch = 16, lty = 1, ylim = c(0, 100),
     xlab = "X", ylab = "Y", main = "Legend Positioning")
# Add another series
lines(x, y2, col = "red", pch = 17, lty = 2)
# Add a legend at a specific coordinate
legend(8, 80, legend = c("Linear", "Quadratic"),
       col = c("blue", "red"), pch = c(16, 17), lty = c(1, 2))
  • legend(8, 80, …): Places the legend at the coordinates (8, 80) on the plot.

Using Legends for Multiple Plot Types

Legends can describe different types of graphical elements, including points, lines, and more.

Example: 

# Create a base plot
plot(x, y1, type = "b", col = "blue", pch = 16, lty = 1, ylim = c(0, 100),
     xlab = "X", ylab = "Y", main = "Legend for Different Plot Types")
# Add a series with lines
lines(x, y2, col = "red", lty = 2)
# Add a series with points only
points(x, y1 + 5, col = "green", pch = 18)
# Add a legend for all plot types
legend("topright", legend = c("Linear - Lines", "Quadratic - Lines", "Points Only"),
       col = c("blue", "red", "green"), pch = c(16, NA, 18), lty = c(1, 2, NA))
  • pch = c(16, NA, 18): NA indicates that no symbol is used (for lines).
  • lty = c(1, 2, NA): NA indicates that no line type is used (for points only).

Summary

  1. Basic Usage: Use legend() to add a legend to an existing plot.
  2. Customizing Appearance: Adjust the text size (cex), background color (bg), and box color (box.col).
  3. Positioning: Use standard string positions or numeric coordinates.
  4. Multiple Plot Types: Create legends for different plot elements like lines, points, and more.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Facebook
Twitter
LinkedIn
WhatsApp
Email
Print