Figure 15-8: Adding Lines

Portfolio Categories: All Graphics and SGR Book Graphics.

Adding Lines to a Plot in R Graphics

Adding Lines to a Plot


# 15.3a Basic lines ====================================================== 15.3a

myV1 = c(1, 2, 4, 5, 8)                # Some data to work with
myV2 = c(1.5, 3, 2, 7, 6)

png(filename = "illustrations/fig-15-8-lines-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(mar = c(4.2, 4, 1, 1))             # Set margin widths in lines

plot(myV1, myV2,                       # A simple default scatterplot
  pch = 19, col = "darkgray",          # Solid point markers in darkgray
  xlim = c(0, 10), ylim = c(0, 10))    # Set range for x & y axes

# Line 1 - a horizontal dashed line
abline(h = 1.5,                        # Add a horizontal line at y = 1.5
  lwd = 3,                             # Give it a width of 3     
  lty = 2,                             # Dash it all
  col = gray(.4))                      # Make it medium gray     

text(x = 8, y = 2.2,                   # Add label for line 1
  label = "Line 1",                    # Label text
  cex = 2,                             # Set size to 2
  col = gray(.4))                      # Set color to gray .4

# Line 2 - a vertical line with long dashes
abline(v = 4,                          # Add a vertical line at x = 4
  lwd = 8,                             # Give it a width of 8     
  lty = "longdash",                    # Use long dashes
  col = gray(.7))                      # Make it light gray     

text(x = 3.5, y = 6,                   # Add label for line 2
  label = "Line 2",                    # Label text
  cex = 2,                             # Set size to 2
  srt = 90,                            # Rotate text to vertical
  col = gray(.7))                      # Set color to light gray

# Line 3 - abline using coefficients
abline(a = 3.2,                        # Add a line w/ intercept = 3.2
  b = 1.5,                             #   and slope = 1.5
  lwd = .5,                            # Give it a width of .5     
  col = gray(.2))                      # Make it dark gray     

text(x = 1.5, y = 6.5,                 # Add label for line 3
  label = "Line 3",                    # Label text
  cex = 2,                             # Set size to 2
  srt = 44,                            # Rotate text
  col = gray(.2))                      # Set color to dark gray

# Line 4 - Line segment
lines(x = c(6, 12),                    # x values for line segment
  y = c(11, 4),                        # y values for line segment
  lty = 4,                             # Dot-dash pattern
  col = gray(.1),                      # Make it dark gray
  lwd = 2,                             # Set line width at 2
  xpd = TRUE)                          # Allow outside plot area

text(x = 8, y = 9.75,                  # Add label for line 4
  label = "Line 4",                    # Label text
  cex = 2,                             # Set size to 2
  srt = -34,                           # Rotate text
  col = gray(.1),                      # Set color to dark gray
  xpd = TRUE)                          # Allow to extend beyond plot

# Line 5 - A regression line 
myReg = lm(myV2 ~ myV1)                # A simple regression model
                                       
abline(reg = myReg,                    # Use regression model to define line
  col = "black")                       # Set color

text(x = 6, y = 6,                     # Add label for line 5
  label = "Line 5",                    # Label text
  cex = 2,                             # Set size to 2
  srt = 23,                            # Rotate text
  col = "black")                       # Set color

dev.off()                              # Output the png file