Figure A2-3: Plot & Regression Line

Portfolio Categories: All Graphics and SGR Book Graphics.

Scatterplot with Regression Line

A Scatterplot with a Regression Line


# AII.3 - Bivariate descriptives: Measures of Association ================ AII.3

# We'll use the built-in trees data.  For demonstration purposes we'll just
# use the first 26 observations and add a character variable using the 
# built-in LETTERS vector (A-Z)

myTrees = trees[1:26,]                 # Use the first 26 observations
myTrees$treeID = LETTERS               # Add a letter ID for each obs
myTrees$even =                         # Add a factor variable for even and
  as.factor(c("even", "odd"))          #   odd observations

# Bivariate visualizations -----------------------------------------------------

png(filename = "illustrations/fig-AII-3-bivariate visualization.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))           # Set margins

plot(myTrees$Girth,myTrees$Height,     # Default 2 variable scatter plot
  xlim = c(5, 20),ylim = c(40, 100))   # Set axis lengths
abline(                                # Add 2 variable regression line
  lm(myTrees$Height ~ myTrees$Girth))    

dev.off()                              # Output png file