Figure 14-6: Legends

Portfolio Categories: All Graphics and SGR Book Graphics.

Demonstration of Adding Legends in R Graphics

Adding Legends in R Graphics


# 14.4 Legends ============================================================ 14.4

# Set up some data
year = 1990:1996                       # Create a series of years
myV1 = c(10, 11, 12, 9, 14, 10, 11)    # Create some values for myV1
myV2 = c(8, 14, 9, 12, 10, 13, 12)     # Create some values for myV2

png(filename = "illustrations/fig-14-6-legends-BW.png",
  units = "in",                        # Set measurements in inches
  res = 1200,                          # Set resolution at 1200dpi
  width = 6,                           # Width at 6 inches
  height = 4)                          # Height at 4 inches

par(mai = c(1, .5, .25, .25))          # Set space around plot

# Create a simple plot
plot(year, myV1,                       # Plot myV1 against year
  ylim = c(0, 20),                     # Set y axis scale
  ylab = NA,                           # Turn off y axis label
  type = "o",                          # Set as line plot with points
  lty = 1,                             # Solid line
  pch = 16)                            # Point style
points(year, myV2,                     # Add myV2 to plot
  type = "o",                          # Set type as line with points
  col = "darkgray",                    # Set color to red
  lty = 2,                             # Dashed line
  pch = 22)                            # Use square box for point style

# Legend 1
legend("topleft",                      # Location of legend
  inset = .025,                        # Distance from edge of plot region
  legend = c("Variable myV1",          # Legend Text
    "Variable myV2"),
  col = c("black", "darkgray"),        # Legend Element Colors
  pch = c(16, 22),                     # Legend Element Styles
  title = "Legend 1")                  # Legend title

# Legend 2
legend(x = 1992.25, y = 20,            # Location of legend on x & y scale
  legend = c("Variable myV1",          # Legend Text
    "Variable myV2"),
  cex = .8,                            # Text size set to .8
  fill = c("black", "darkgray"),       # Legend Element Colors
  title = "Legend 2",                  # Legend Title
  horiz = T,                           # Set legend horizontally
  bg = "light gray")                   # Set color for legend background

# Set up percentage coordinates system for legend
my.usr = par("usr")                    # Save current coordinate units
par(usr = c(0, 1, 0, 1))               # Set coordinate space to 0,1 scales

# Legend 3 Bold Title
par(font = 2, family = "serif")        # Set bold serif font
legend(x = .05, y = .3,                # Location of legend in percent units
  yjust = 1,                           # Put legend below y value
  legend = c("Variable myV1",          # Legend Text
    "Variable myV2"),
  lty = c(1, 2),                       # line types for legend elements
  col = c("black","darkgray"),         # Legend Element Colors
  title = "Legend 3",                  # Legend title
  title.col = gray(.2),                # Legend title color
  text.col = "white",                  # Set text color to be invisible
  bty = "n")                           # No box around legend

par(font = 1, family = "sans")         # Return default font to normal sans

# Legend 3
legend(x = .05, y = .3,                # Location of legend in percent units
  yjust = 1,                           # Put legend below y value
  legend = c("Variable myV1",          # Legend Text
    "Variable myV2"),
  lty = c(1, 2),                       # line types for legend elements
  col = c("black", "darkgray"),        # Legend Element Colors
  title = NA,                          # Legend title turned off
  bty = "n")                           # No box around legend

# Legend 4
legend(x = .95, y = .05,               # Location of legend in percent units
  xjust = 1,                           # Left justify legend box on x coord.
  yjust = 0,                           # Bottom justify legend box on y coord.
  legend = c("Variable myV1",          # Legend Text
    "Variable myV2"),
  lty = c(1, 2),                       # Line type for legend elements
  col = c("black", "darkgray"),        # Legend Element Colors
  pch = c(16, 22),                     # Symbol styles for legend elements
  merge = TRUE,                        # Merge line & symbol for legend elements
  title = "Legend 4",                  # Legend Title
  title.col = gray(.4),                # Legend title color
  box.lty = 9,                         # Legend box line type
  box.lwd = 2,                         # Legend box line width
  box.col = "darkgray")                # Legend box color

dev.off()                              # Output png file