Figure 15-11: Grid Lines

Portfolio Categories: All Graphics and SGR Book Graphics.

Adding Grid Lines in R Graphics

Adding Grid Lines in R Graphics

 
# 15.3d Grid lines ======================================================= 15.3d

png(filename = "illustrations/fig-15-11-grids.png",
  units = "in",                        # Set measurements in inches
  res = 1200,                          # Set resolution at 1200dpi
  width = 6,                           # Width at 6 inches
  height = 6)                          # Height at 6 inches

layout(matrix(c(1, 1, 2, 3),           # Set layout of 3 plots
  ncol = 2,                            #  organized as 2 columns
  byrow = T))                          #  and filled by row.

par(mai = c(.5, .5, .5, .25))          # Set the margin sizes

plot(0:10, pch = 19,                   # Plot 1 w/solid dots
  main = "Horizontal Lines",           # Title for plot 1
  xlab = NA, ylab = NA,                # Turn off axis labels
  yaxs = "i")                          # Eliminate y axis overage

grid(nx = NA,                          # Create a grid w/o verticals
  ny = 5,                              #  and 5 horizontal partitions
  lty = 1,                             # Set line type to solid
  lwd = 2,                             # Set line width to 2
  col = "gray")                        # Set color

points(0:10, pch = 19)                 # Redo points on top of grid

plot(0:10, pch = 19,                   # Plot 2 w/solid dots
  main = "Default Grid",               # Title for plot 2
  xlab = NA, ylab = NA)                # Turn off axis labels

grid()                                 # Add default grid on tick marks
                                       
par(mai = c(0, 0, 0, 0))               # Set margins to zero
   
plot(0:10, type = "n", yaxt = "n")     # Create an empty plot
grid(nx = 10, ny = 10,                 # Add gridlines
  lty = 1,                             # Use solid lines
  col = "black")                       # Set color to black

par(usr = c(0, 1, 0, 1))               # Use usr par
text(.5, .55,                          # Add text at center
  labels = "An empty grid",            # Text to add
  cex = 2)                             # Set text size at 2

dev.off()                              # Output png file