Figure 15-13: Drawing a Shapefile

Portfolio Categories: All Graphics and SGR Book Graphics.

Drawing with a Shapefile in R Graphics

Drawing with a Shapefile in R Graphics


# A more complex shapefile -----------------------------------------------------

# California Shapefile Read
# This reads in the coordinates for the shape of California, extracted from
# a google earth kml file.

# Note that the location of this datafile will change to somewhere on the 
# www.sagepub.com/gaubatz site

CAshape = read.csv(
  file = "http://www.kktg.net/R/calif-coords.csv", header = FALSE)

summary(CAshape)                       # Show structure & range of coordinates

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

par(mai = c(0, 0, 0, 0))               # no margins

plot(x = 0, y = 0, type = "n",         # Set empty plot
  xlim = c(-125, -113),                # X range
  ylim = c(32, 43),                    # Y range
  axes = FALSE,                        # Turn off axes
  xlab = NA, ylab = NA)                # Turn off labels

polygon(x = CAshape$V1,                # Set x & y values from CA coordinates
  y = CAshape$V2, 
  col = gray(.8))                      # Fill color

text(x = -117, y = 39.5,               # Position label
  label = "California",                # Label text
  cex = 2.5)                           # Set size to 2.5

dev.off()                              # Output png file