Figure 14-7: External Legends

Portfolio Categories: All Graphics and SGR Book Graphics.

Demonstration of Adding a Legend Outside of the Plot Area

Adding a Legend Outside the Plot Area


# Putting a legend outside the plot area ---------------------------------------

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

# Create a simple plot

png(filename = "illustrations/fig-14-7-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,  .5, .25, 2))         # Right margin space for legend

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 = gray(.2),                      # Set color
  lty = 2,                             # Dashed line
  pch = 22)                            # Use square box for point style

# Legend outside the plot on right

legend(x = 1996.5, y = 15,             # Location of legend 
  xpd = TRUE,                          # Allow drawing outside plot area
  xjust = 0,                           # Left justify legend box on x
  yjust = .5,                          # Center legend box on y
  legend = c("Variable myV1",          # Legend Text
    "Variable myV2"),
  col = c("black", gray(.2)),          # Legend Element Colors
  pch = c(16,22),                      # Legend Element Styles
  title = "Legend 5",                  # Legend Title
  title.col = gray(.2),                # Legend title color
  box.lty = 1,                         # Legend box line type
  box.lwd = 2)                         # Legend box line width

# Legend outside the plot on bottom
legend(x = 1993, y = -6.5,             # Location of legend
  xpd = TRUE,                          # Allow drawing outside plot area
  xjust = .5,                          # Center legend box on x
  yjust = 1,                           # Top justify legend box on y
  horiz = TRUE,                        # Set legend horizontally
  legend = c("Variable myV1",          # Legend Text
    "Variable myV2"),
  lty = c(1, 2),                       # Line type for legend elements
  col = c("black", gray(.2)),          # Legend Element Colors
  pch = c(16, 22),                     # Symbol styles for legend elements
  merge = TRUE,                        # Merge line & symbol for legend 
  title = "Legend 6",                  # Legend Title
  title.col = gray(.4),                # Legend title color
  box.lty = 1,                         # Legend box line type
  box.lwd = 2,                         # Legend box line width
  box.col = gray(.4))                  # Legend box color

dev.off()                              # Output png file