Figure 10-3: An Ovals Function

Portfolio Categories: All Graphics and SGR Book Graphics.

fig-10-3-Match-merging-BW


# Match merging figure Code ----------------------------------------------------

# 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-3-Match-merging-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))
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 = .1,        # 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 = .09, ycen = .8367,         # Add first ellipse
  xlen = .06, ylen = .02,
  ewidth = 1.5, ecolor = "gray")
oval(xcen = .64, ycen = .8367,         # Add second ellipse
  xlen = .06, ylen = .02,
  ewidth = 1.5, ecolor = "gray")
oval(xcen = .09, ycen = .756,          # Add third ellipse
  xlen = .06, ylen = .02,
  ewidth = 1.5, ecolor = "gray")
oval(xcen = .64, ycen = .7965,         # Add fourth ellipse
  xlen = .06, ylen = .02,
  ewidth = 1.5, ecolor = "gray")

# Add text in boxes
text(x = .05, y = .85,                 # Add text to box 1
  cex = .75, adj = c(0, 1),            # Aligned left & below
  labels = "School A\nSchool B\nSchool C\n.\n.\n.")
text(x = .6, y = .85,                  # Add text to box 2
  cex = .75, adj = c(0, 1),            # aligned left & below
  labels = "School A\nSchool C\n.\n.\n.")
     
lines(x =                              # Add a line between first two ellipses
  c(.15, .58), y = c(.8367, .8367),     
  lwd = 2, col = "gray")     
lines(x =                              # Add a line between wecond two ellipses
  c(.15, .58), y = c(.756, .7965),     
  lwd = 2, col = "gray")     

dev.off()                              # Output png file