Figure 13-5: Overlaid Plots

Portfolio Categories: All Graphics and SGR Book Graphics.

R graphics overlaid plots

Two overlaid line plots using R graphics


# 13.5 Overlaying plots =================================================== 13.5

myX1 = c(1, 3, 4, 7, 9, 12, 15)
myY1 = c(15, 17, 14, 19, 24, 23, 24)
myY2 = c(8, 6, 3, 5, 4, 2, 1)

png(filename = "illustrations/fig-13-5-overlay-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, 1, .25, .25))           # Plot margins - no title

plot(myX1, myY1,                       # Plot myX1 and myY1
  ylim = c(0, 25),                     # Set Y axis range for both plots
  type = "l",                          # Line plot
  ylab = "myY1 & myY2")                # Set Y axis label

par(new = T)                           # Overlay the next plot
plot(myX1, myY2,                       # Plot myX1 and myY2
  type = "l",                          # Line plot
  lty = 2,                             # Set line type to dashes
  axes = FALSE,                        # Turn off the axes
  xlab = NA, ylab = NA)                # Turn off the labels

dev.off()                              # Output png file