Figure 14-10: Adding Tick Marks

Portfolio Categories: All Graphics and SGR Book Graphics.

Adding Tick Marks to a Custom Axis with R Graphics

Adding Tick Marks to a Custom Axis


# 14.6a Tick marks ======================================================= 14.6a

png(filename = "illustrations/fig-14-10-ticks-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(3.5, .25, .25, .25))       # Set margin sizes in inches

plot(myV1, myV2, type = "n",           # Setup w/no points & default x axis
  xlab = NA,                           # Turn off x axis label
  ylab = NA,                           # Turn off y axis label
  yaxt = "n",                          # Turn off y axis
  xlim = c(0, 10))                     # Set x axis range

axis(side = 1,                         # New x axis
  at = c(0:10),                        # Ticks at 0-10
  line = 3)                            # Move axis down to 3rd line

axis(side = 1,                         # New x axis
  at = c(seq(0, 10, by = 2)),          # Major ticks at 0-10 by 2
  labels = LETTERS[1:6],               # A-F as labels
  line = 6)                            # Put axis on 6th line

axis(side = 1,                         # New x axis
  at = c(seq(1, 10, by = 2)),          # Minor ticks at 1-10 by 2
  labels = NA,                         # Turn off labels for minor ticks
  line = 6)                            # Overlay axis on 6th line

axis(side = 1,                         # New x axis
  at = c(0:10),                        # Major ticks at 0-10 
  labels = c(1990:2000),               # Use years for labels
  las = 2,                             # Rotate labels to be perpendicular
  col = gray(.5),                      # Set main axis line to med gray
  tcl = -.75,                          # Major tick length at .75 below line
  col.tick = "black",                  # Make major ticks black
  lwd = 2,                             # Make major ticks longer
  line = 9)                            # Put axis on 9th line below plot

axis(side = 1,                         # Add overlay axis for the minor ticks
  at = c(seq(.5, 10, by = 1)),         # Minor ticks at every .5
  labels = NA,                         # No labels for minor ticks
  tcl = -.3,                           # Minor ticks length is .3 below line
  lwd = 0,                             # Turn off main axis line for overlay
  lwd.ticks = 1.25,                    # Set minor tick width to 1.25      
  col.tick = "darkgray",               # Color for minor tick marks 
  line = 9)                            # Overlay axis on 9th line

axis(side = 1,                         # New x axis
  at = c(0:10),                        # Major ticks at 0 - 10
  labels = NA,                         # Turn off labels
  tcl = -.5,                           # Major tick length at .5 below line
  lwd = 1.25,                          # Make major ticks thicker
  line = 13)                           # Put axis on 13th line

text(x = seq(0.3, 10.3, 1),            # x values for axis labels
  y = -46,                             # y value for axis labels
  xpd = TRUE,                          # Allow writing outside plot area
  labels = as.character(c(1990:2000)), # Years as text labels
  pos = 2,                             # Left align labels
  srt = 45)                            # Rotate strings 45 degrees

dev.off()                              # Output png file