Figure 9-1: Plotting Dates

Portfolio Categories: All Graphics and SGR Book Graphics.

fig-9-1-dates


# Some date data to work with
date1 = c("Dec 5, 1932", "May 21, 1970",
  "Jun 12, 1971", "Apr 2, 1980",
  "Jan 16, 1996", "Feb 28, 1997")
y = c(4, 7, 6, 6.2, 6.5, 3)            # Some arbitrary variable values to use

date2 = as.Date(date1,                 # Create date2 as date variable from
  format="%b %d, %Y")                  #   date1, based on the given format

png(filename = "illustrations/fig-9-1-dates.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(mfrow = c(1, 2),                   # Set up 1x2 set of plots
  mai = c(1.5, .75, .25, .25))         # Set margins w/ room for date labels

plot(1:6, y,                           # Plot 1 - ordered dates
  type = "o",                          # set plot type - line through dots
  xaxt = "n",                          # No default x axis scale
  xlab = "", ylab = "",                # Suppress axis labels
  ylim = c(0, 10))                     # Set y axis scale

axis(side = 1,                         # Add x axis date labels
  labels = date1,                      # Labels from date data
  at = c(1:6),                         # Placement of labels
  las = 2)                             # Rotate text 90 degrees

plot(date2, y,                         # Plot 2 - scaled dates
  type = "o",                          # set plot type - line through dots
  las = 2,                             # Rotate axis text by 90 degrees
  xlab = "", ylab = "",                # Suppress axis labels
  ylim = c(0,10))                      # Set y axis scale

dev.off()                              # Output png file