Figure 5-4: Scatterplot w/Labels

Portfolio Categories: All Graphics and SGR Book Graphics.

fig-5-4-scatterplot labels


myX = c(1, 5, 19, 7, 6, 18, 11, 10)    # Create an x variable
myY = c(4, 3, 12, 7, 8, 9, 15, 9)      # Create a y variable

# adding labels

myLabels = c(                          # Add a vector of labels for each obs
  "Bach", "Beethoven",
  "Brahms", "Mozart", "Chopin",
  "Tchaikovsky", "Satie", "Bartok")

png(filename = "illustrations/fig-5-4-scatterplot labels.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 - no Title

plot(myX, myY)                         # Simple scatterplot of x & y
text(myX, myY,                         # Add labels using x & y coords
  labels = myLabels,                   # The labels to add
  pos = 3,                             # Put labels above points
  xpd = TRUE)                          # Allow printing outside plot

dev.off()                              # Output png file