Figure 10-4: More Shape Drawing

Portfolio Categories: All Graphics and SGR Book Graphics.

fig-10-4-Keyed table-BW


# Figure 10-4 
# This will use the same oval function we set up for figure 10-3

# First we'll set up a function to draw ellipses (This exists in various
#  packages (e.g. hmisc and ellipse) but not in the base graphics. So, we'll 
#  do it as a useful exercise. This works on a (0,1) coordinate system. xcen
#  and ycen are the coordinates for the center of the oval. xlen and ylen are
#  the length of the x and y radii. ewidth is the width of the line. ecolor
#  is the color of the line.
     
oval = function(xcen, ycen,            # Oval function - center coords --------+
  xlen, ylen,                          # Length of x and y radii               |
  ewidth = 1, ecolor = "black"){       # Line width and color                  |
  t = seq(0, 2 * pi, length = 2000)    # Set up theta for degrees around oval  |
  x = xcen + xlen * cos(t)             # x values for ellipse                  |
  y = ycen + ylen * sin(t)             # y values for ellipse                  |
                                       #                                       |
  lines(x, y,                          # Plot x and y values                   |
    usr = c(0, 1, 0, 1),               # Use 0,1 coordinates                   |
    type = "l",                        # Use solid line                        |
    lwd = ewidth,                      # Line width from function call         |
    col = ecolor)                      # Color set by function call            |
}                                      # End of oval function -----------------+

png(filename = "illustrations/fig-10-4-Keyed table-BW.png",    
  units = "in",                        # Set units to inches
  width = 6,                           # Set a width of 6 inches
  height = 4,                          # Setheight to 4 inches
  res = 1200,                          # Set resolution to 1200
  pointsize = 12)                      # Set text pointsize to 12
     
par(mar = c(0, 0, 0, 0))               # Set all margins to zero
plot.new()                             # Start the plot

rect(xleft = 0, ybottom = 0,           # Draw a rectangle with coordinates
    xright = .45, ytop = .875)         #  based on percentage of the plot area
rect(xleft = .55, ybottom = .6,        # A second rectangle
  xright = 1, ytop = .875)     

# Add top labels
text(x = .225, y = .95, labels = "Performance Metrics\n by School")     
text(x = .775, y = .95, labels = "Demographic Data\n by School")     
text(x = .5, y = .95, cex = 1.5, labels = "+")

# Add ellipses
oval(xcen = .185, ycen = .8367,        # Add first ellipse
  xlen = .04, ylen = .02,
  ewidth = 1.5, ecolor = "gray")
oval(xcen = .605, ycen = .797,         # Add second ellipse
  xlen = .04, ylen = .02,
  ewidth = 1.5, ecolor = "gray")

# Add text in boxes
text(x = .045, y = .85,                # Add text
  cex = .75, adj=c(0, 1),              # Align left and below
  labels = "School A   City B\nSchool C  City C\n.\n.\n.")
text(x = .575, y = .85,                # Add text
  cex = .75, adj=c(0, 1),              # Align left and below
  labels = "City A\nCity B")
     
lines(x = c(.225, .565),               # Add a line between the two ellipses
  y = c(.8367, .797),       
  lwd = 1, col = "gray")     
     
dev.off()                              # Output png file