{"id":517,"date":"2014-02-27T18:09:37","date_gmt":"2014-02-27T18:09:37","guid":{"rendered":"http:\/\/kktg.net\/sgr\/?page_id=517"},"modified":"2014-02-27T18:09:37","modified_gmt":"2014-02-27T18:09:37","slug":"sgr-code-4","status":"publish","type":"page","link":"http:\/\/kktg.net\/sgr\/sgr-code-2\/sgr-code-4\/","title":{"rendered":"SGR Code from Chapters 14-15"},"content":{"rendered":"<h3>R Code for Chapter 14: Graphics III &#8212; The Fun Stuff &#8212; Text<\/h3>\n<pre lang=\"r-script\" line=\"1\">\r\n# ==============================================================================  \r\n# Chapter 14 -- Graphics III: The fun stuff -- Text\r\n# ==============================================================================\r\n\r\ndefault.par = par()                    # Set default parameters for recovery\r\n\r\n# 14.1 Adding text ======================================================== 14.1\r\n                     \r\nmyV1 = c(1, 2, 3, 4, 5)                # Set up some temporary data\r\nmyV2 = c(5, 7, 3, 9, 8)                       \r\n\r\npng(filename = \"illustrations\/fig-14-1-Text-BW.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\nplot(myV1, myV2,                       # Plot the data\r\n  ylim = c(0, 10),                     # Set range for y axis\r\n  type = \"b\",                          # Set line type connecting dots  \r\n  main = \"A bold title on top\",        # Add a title at the top\r\n  font.main = 2,                       # Bold for  main title\r\n  cex.main = 2,                        # Set font size for the main title \r\n  sub = \"Subtitle at the bottom\",      # Put a subtitle on the bottom\r\n  cex.sub = .75,                       # Set font size for the subtitle\r\n  col.sub = \"darkgray\",                # Set color for subtitle\r\n  xlab = \"This is the x axis\",         # Add text for the x axis\r\n  ylab = \"This is the y axis\",         # Add text for the y axis \r\n  font.lab = 3,                        # Set axis label font to italic\r\n  col.lab = \"black\")                   # Set color for axis labels\r\n\r\n# A legend \r\nlegend(\"bottomright\",                  # Location of legend\r\n  inset = .025,                        # Distance from edge of plot region\r\n  legend = c(\"my x1 variable\",         # Legend Text\r\n    \"my x2 variable\"),\r\n  col = c(\"black\", \"darkgray\"),        # Legend Element Colors\r\n  pch = c(1, 22),                      # Legend Element Styles\r\n  title = \"A Legend\")                  # Legend title\r\n\r\n# Some ad hoc text\r\ntext(x = 2.5, y = 5,                   # Add some ad hoc text at x = 2 y = 5\r\n  labels = \"Some ad hoc text\",         # The text to add\r\n  srt = 45,                            # Rotate 90 degrees\r\n  family = \"mono\",                     # Use mono-spaced font\r\n  col = \"darkgray\")                    # Set color to dark green\r\n\r\n# Ad hoc text in margin\r\npar(usr = c(0, 1, 0, 1))               # Set usr parameters to 0,1 space\r\ntext(x = 1.05, y = .5,                 # Place text just outside right border\r\n  labels = \"Ad hoc text in margin\",    # Text to use\r\n  xpd = TRUE,                          # Allow text outside of border\r\n  font = 3,                            # Italic font\r\n  cex = 1.25,                          # Font size to 1.25\r\n  srt = 270,                           # Rotate string 270 degrees\r\n  col = gray(.75))                     # Set color to gray .75\r\n     \r\ndev.off()                              # Output png file\r\n\r\n\r\n# 14.2 Setting up fonts =================================================== 14.2\r\n\r\n# 14.2a The built-in fonts =============================================== 14.2a\r\npng(filename = \"illustrations\/fig-14-2-fonts.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mfcol = c(2, 2))                   # Set 2x2 grid of plots\r\nplot(myV1, myV2, type = \"b\",           # A simple plot\r\n  main = \"Family = 'sans'\",            # A title \r\n  sub = \"The default sans-serif font\", # A subtitle\r\n  family = \"sans\")                     # Set font family to sans\r\n\r\nplot(myV1, myV2, type = \"o\",           # A simple plot\r\n  main = \"Family = 'serif'\",           # A title \r\n  sub = \"The serif font\",              # A sub-title\r\n  family = \"serif\")                    # Set font family to serif\r\n\r\nplot(myV1, myV2, type = \"l\",           # A simple plot\r\n  main = \"Family = 'mono'\",            # A title \r\n  sub = \"The mono-spaced font\",        # A sub-title\r\n  family = \"mono\")                     # Set font family to mono\r\n\r\nplot(myV1, myV2, type = \"p\",           # A simple plot\r\n  main = \"Variation in font styles\",   # A title \r\n  sub = \"Same font family, different styles and sizes\",\r\n  family = \"serif\",                    # Set font family to serif\r\n  font.main = 4, cex.main = 1.75,      # Title in larger bold italic\r\n  font.axis = 2, cex.axis = .75,       # Axis fonts small but bold\r\n  font.sub = 3, cex.sub = .6,          # Subtitle fonts in italic & larger\r\n  font.lab = 1, cex.lab = 1)           # Axis labels plain & default size\r\n     \r\ndev.off()                              # Output png file\r\n\r\n# Check windows font mappings\r\nwindowsFonts()                         # Show windows font mappings\r\n\r\n# 14.2b Remapping windows fonts ========================================== 14.2b\r\n\r\npng(filename = \"illustrations\/fig-14-3-windowsFonts.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mfcol = c(2, 1))                   # Set up 2 plots vertically\r\npar(mai = c(.85, .85, .5, .25))        # Set margins\r\nwindowsFonts(                          # Reassign the sans font \r\n  sans = \"TT Comic Sans MS\")           #   to Comic Sans\r\nplot(myV1, myV2, type = \"b\",           # A simple plot \r\n  main = \"Some Comic Sans Action!\",    # A Title\r\n  family = \"sans\")                     # Font family for plot\r\n\r\nwindowsFonts(                          # Reassign serif font\r\n  serif = \"TT Blackadder ITC\")         #   to Blackadder ITC\r\nplot(myV1, myV2, type = \"b\",           # A simple plot \r\n  main = \r\n    \"Please don't use this font!\",     # A title for the plot\r\n  family = \"serif\")                    # Font family for plot\r\n\r\ndev.off()                              # Output png file\r\n\r\n# Reset fonts to defaults (Please!)\r\nwindowsFonts(sans = \"TT Arial\")        # Reset fonts back to defaults\r\nwindowsFonts(serif = \"TT Times New Roman\")  \r\n\r\n# 14.2c The Rdevga method for Windows ==================================== 14.2c\r\n\r\n# Note that this method requires editing your Rdevga file so that lines\r\n# 15 - 18 (not include blank and comment lines read: \r\n# TT Century Gothic : plain\r\n# TT Century Gothic : bold\r\n# TT Century Gothic : italic\r\n# TT Century Gothic : bold&italic\r\n# (but without the hashtag at the beginning).\r\n\r\npng(filename = \"illustrations\/fig-14-4-Rdevga method.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mfcol = c(1, 1))                   # Reset mfcol for just one plot\r\nplot(myV1, myV2, type = \"b\",           # A simple plot \r\n  main = \"The Rdevga method\",          # A Title\r\n  font.main = 15,                      # This should be Century Gothic bold\r\n  font.lab = 12,                       # This should be Courier italic\r\n  font.axis = 7)                       # This should be Times bold\r\n\r\ndev.off()                              # Output png file\r\n\r\n# 14.3 Titles and Subtitles =============================================== 14.3\r\n\r\n# Short example of using text variables for title\r\nmyTitle = \"My Nice Title\"              # Create a txt variable with title\r\nplot(myX, myY, main = myTitle)         # Plot using txt variable title\r\n\r\n                     \r\npng(filename = \"illustrations\/fig-14-5-Title and subtitle-BW.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\nplot(myV1, myV2, type = \"l\",           # A simple plot\r\n  main = \"Here is the title\",          # The main title\r\n  family = \"serif\",                    # Use serif family font\r\n  font.main = 2,                       # Set title font to bold\r\n  cex.main = 3,                        # Set title font size to double\r\n  col.main = \"darkgray\",               # Set title to dark gray\r\n  xlab = NA, ylab = NA)                # Supress axis labels \r\n\r\ntitle(                                 # Put subtitle on plot\r\n  sub = \"This is the subtitle\",        # A subtitle\r\n  adj = 1,                             # Put it on right side\r\n  family = \"mono\",                     # Use mono-spaced font\r\n  font.sub = 2,                        # Use bold for subtitle\r\n  cex.sub = 1.5,                       # Subtitle font size = 1.5\r\n  col.sub = \"black\")                   # Subtitle color \r\n\r\ntitle(                                 # Put X axis label on plot\r\n  xlab = \"This is the x-axis label\",   # X axis label text\r\n  family = \"sans\",                     # Use sans-serif font\r\n  font.lab = 3,                        # Use italic for x axis label\r\n  cex.lab = 1.25,                      # X-axis label font size \r\n  col.lab = \"darkgray\")                # X-axis label color\r\n\r\ntitle(                                 # Put Y axis label on plot\r\n  ylab = \"Y is here\",                  # Y axis label text\r\n  family = \"serif\",                    # Use serif font\r\n  font.lab = 2,                        # Use bold for y axis label\r\n  cex.lab = 1.25,                      # Y-axis font size = 1.25\r\n  col.lab = \"darkgray\",                # Y-axis label color\r\n  las = 2)     \r\n\r\ndev.off()                              # Output png file\r\n\r\n# 14.4 Legends ============================================================ 14.4\r\n\r\n# Set up some data\r\nyear = 1990:1996                       # Create a series of years\r\nmyV1 = c(10, 11, 12, 9, 14, 10, 11)    # Create some values for myV1\r\nmyV2 = c(8, 14, 9, 12, 10, 13, 12)     # Create some values for myV2\r\n\r\npng(filename = \"illustrations\/fig-14-6-legends-BW.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(1, .5, .25, .25))          # Set space around plot\r\n\r\n# Create a simple plot\r\nplot(year, myV1,                       # Plot myV1 against year\r\n  ylim = c(0, 20),                     # Set y axis scale\r\n  ylab = NA,                           # Turn off y axis label\r\n  type = \"o\",                          # Set as line plot with points\r\n  lty = 1,                             # Solid line\r\n  pch = 16)                            # Point style\r\npoints(year, myV2,                     # Add myV2 to plot\r\n  type = \"o\",                          # Set type as line with points\r\n  col = \"darkgray\",                    # Set color to red\r\n  lty = 2,                             # Dashed line\r\n  pch = 22)                            # Use square box for point style\r\n\r\n# Legend 1\r\nlegend(\"topleft\",                      # Location of legend\r\n  inset = .025,                        # Distance from edge of plot region\r\n  legend = c(\"Variable myV1\",          # Legend Text\r\n    \"Variable myV2\"),\r\n  col = c(\"black\", \"darkgray\"),        # Legend Element Colors\r\n  pch = c(16, 22),                     # Legend Element Styles\r\n  title = \"Legend 1\")                  # Legend title\r\n\r\n# Legend 2\r\nlegend(x = 1992.25, y = 20,            # Location of legend on x & y scale\r\n  legend = c(\"Variable myV1\",          # Legend Text\r\n    \"Variable myV2\"),\r\n  cex = .8,                            # Text size set to .8\r\n  fill = c(\"black\", \"darkgray\"),       # Legend Element Colors\r\n  title = \"Legend 2\",                  # Legend Title\r\n  horiz = T,                           # Set legend horizontally\r\n  bg = \"light gray\")                   # Set color for legend background\r\n\r\n# Set up percentage coordinates system for legend\r\nmy.usr = par(\"usr\")                    # Save current coordinate units\r\npar(usr = c(0, 1, 0, 1))               # Set coordinate space to 0,1 scales\r\n\r\n# Legend 3 Bold Title\r\npar(font = 2, family = \"serif\")        # Set bold serif font\r\nlegend(x = .05, y = .3,                # Location of legend in percent units\r\n  yjust = 1,                           # Put legend below y value\r\n  legend = c(\"Variable myV1\",          # Legend Text\r\n    \"Variable myV2\"),\r\n  lty = c(1, 2),                       # line types for legend elements\r\n  col = c(\"black\",\"darkgray\"),         # Legend Element Colors\r\n  title = \"Legend 3\",                  # Legend title\r\n  title.col = gray(.2),                # Legend title color\r\n  text.col = \"white\",                  # Set text color to be invisible\r\n  bty = \"n\")                           # No box around legend\r\n\r\npar(font = 1, family = \"sans\")         # Return default font to normal sans\r\n\r\n# Legend 3\r\nlegend(x = .05, y = .3,                # Location of legend in percent units\r\n  yjust = 1,                           # Put legend below y value\r\n  legend = c(\"Variable myV1\",          # Legend Text\r\n    \"Variable myV2\"),\r\n  lty = c(1, 2),                       # line types for legend elements\r\n  col = c(\"black\", \"darkgray\"),        # Legend Element Colors\r\n  title = NA,                          # Legend title turned off\r\n  bty = \"n\")                           # No box around legend\r\n\r\n# Legend 4\r\nlegend(x = .95, y = .05,               # Location of legend in percent units\r\n  xjust = 1,                           # Left justify legend box on x coord.\r\n  yjust = 0,                           # Bottom justify legend box on y coord.\r\n  legend = c(\"Variable myV1\",          # Legend Text\r\n    \"Variable myV2\"),\r\n  lty = c(1, 2),                       # Line type for legend elements\r\n  col = c(\"black\", \"darkgray\"),        # Legend Element Colors\r\n  pch = c(16, 22),                     # Symbol styles for legend elements\r\n  merge = TRUE,                        # Merge line & symbol for legend elements\r\n  title = \"Legend 4\",                  # Legend Title\r\n  title.col = gray(.4),                # Legend title color\r\n  box.lty = 9,                         # Legend box line type\r\n  box.lwd = 2,                         # Legend box line width\r\n  box.col = \"darkgray\")                # Legend box color\r\n\r\ndev.off()                              # Output png file\r\n\r\n# Putting a legend outside the plot area ---------------------------------------\r\n\r\nyear = 1990:1996                       # Create a series of years\r\nmyV1 = c(10, 11, 12, 9, 14, 10, 11)    # Create some values for myV1\r\nmyV2 = c(8, 14, 9, 12, 10, 13, 12)     # Create some values for myV2\r\n\r\n# Create a simple plot\r\n\r\npng(filename = \"illustrations\/fig-14-7-legends-BW.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(1.5,  .5, .25, 2))         # Right margin space for legend\r\n\r\nplot(year, myV1,                       # Plot myV1 against year\r\n  ylim = c(0, 20),                     # Set y axis scale\r\n  ylab = NA,                           # Turn off y axis label\r\n  type = \"o\",                          # Set as line plot with points\r\n  lty = 1,                             # Solid line\r\n  pch = 16)                            # Point style\r\n\r\npoints(year, myV2,                     # Add myV2 to plot\r\n  type = \"o\",                          # Set type as line with points\r\n  col = gray(.2),                      # Set color\r\n  lty = 2,                             # Dashed line\r\n  pch = 22)                            # Use square box for point style\r\n\r\n# Legend outside the plot on right\r\n\r\nlegend(x = 1996.5, y = 15,             # Location of legend \r\n  xpd = TRUE,                          # Allow drawing outside plot area\r\n  xjust = 0,                           # Left justify legend box on x\r\n  yjust = .5,                          # Center legend box on y\r\n  legend = c(\"Variable myV1\",          # Legend Text\r\n    \"Variable myV2\"),\r\n  col = c(\"black\", gray(.2)),          # Legend Element Colors\r\n  pch = c(16,22),                      # Legend Element Styles\r\n  title = \"Legend 5\",                  # Legend Title\r\n  title.col = gray(.2),                # Legend title color\r\n  box.lty = 1,                         # Legend box line type\r\n  box.lwd = 2)                         # Legend box line width\r\n\r\n# Legend outside the plot on bottom\r\nlegend(x = 1993, y = -6.5,             # Location of legend\r\n  xpd = TRUE,                          # Allow drawing outside plot area\r\n  xjust = .5,                          # Center legend box on x\r\n  yjust = 1,                           # Top justify legend box on y\r\n  horiz = TRUE,                        # Set legend horizontally\r\n  legend = c(\"Variable myV1\",          # Legend Text\r\n    \"Variable myV2\"),\r\n  lty = c(1, 2),                       # Line type for legend elements\r\n  col = c(\"black\", gray(.2)),          # Legend Element Colors\r\n  pch = c(16, 22),                     # Symbol styles for legend elements\r\n  merge = TRUE,                        # Merge line & symbol for legend \r\n  title = \"Legend 6\",                  # Legend Title\r\n  title.col = gray(.4),                # Legend title color\r\n  box.lty = 1,                         # Legend box line type\r\n  box.lwd = 2,                         # Legend box line width\r\n  box.col = gray(.4))                  # Legend box color\r\n\r\ndev.off()                              # Output png file\r\n\r\n# 14.5 Simple axes and axis labels ======================================== 14.5\r\n\r\n# 14.6 Building more complex axes ========================================= 14.6\r\n\r\n# Turning axes off selectively (not in book).\r\ndefault.par = par()                    # Save parameter settings\r\n\r\nmyV1 = c(1, 3, 4, 7, 9)                # Set up some x values\r\nmyV2 = c(4, 6, 5, 3, 7)                # Set up some y values\r\n\r\npar(mfrow = c(2, 2))                   # Create a 2x2 grid of plots     \r\n\r\nplot(myV1, myV2, type = \"b\",           # Plot myV1 with myV2\r\n  main = \"xaxt = 'n'\",                 # Add main title\r\n  xaxt = \"n\")                          # Turn off x axis\r\n\r\nplot(myV1, myV2, type = \"b\",           # Plot myVq with myV2\r\n  main = \"yaxt = 'n'\",                 # Add main title\r\n  yaxt = \"n\")                          # Turn off y axis\r\n\r\nplot(myV1, myV2, type = \"b\",           # Plot myVq with myV2\r\n  main = \"xaxt='n' & yaxt='n'\",        # Add main title\r\n  xaxt = \"n\", yaxt = \"n\")              # Turn off x & y axes\r\n\r\nplot(myV1, myV2, type = \"b\",           # Plot myVq with myV2\r\n  main = \"axes=FALSE\",                 # Add main title\r\n  axes = FALSE)                        # Turn off both axes\r\n\r\n# Axis Modifications I ---------------------------------------------------------\r\n\r\nmyV1 = c(1, 3, 4, 7, 9)                # Set up some x values\r\nmyV2 = c(4, 6, 5, 3, 7)                # Set up some y values\r\n\r\npng(filename = \"illustrations\/fig-14-8-axes-BW.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\nplot(myV1, myV2,                       # Plot myV1 and myV2\r\n  xaxt = \"n\", yaxt = \"n\",              # Supress axes\r\n  xlim = c(0, 10))                     # Set x range\r\n\r\naxis(side = 1,                         # X axis\r\n  at = c(seq(0, 10, by = 2)))          # tick marks every 2\r\n\r\naxis(side = 1,                         # X axis minor tick marks\r\n  at = c(seq(1, 9, by = 2)),           # Set on odd numbers\r\n  labels = NA,                         # No labels for minor tick marks\r\n  tcl = -.25)                          # Shorten minor tick marks\r\n\r\naxis(side = 3,                         # Put another axis on top\r\n  at = c(0:10),                        # Ticks at 0-10\r\n  labels = LETTERS[1:11])              # Use alphabet labels\r\n\r\naxis(side = 2,                         # Add Y axis\r\n  at = c(3:7),                         # Tick marks from 3-7 by 1\r\n  las = 1,                             # Rotate labels perpendicular to axis\r\n  lwd = 0,                             # Turn off axis line\r\n  lwd.ticks = 2,                       # Set tick width to 2\r\n  col.ticks = gray(.3))                # Set tick color\r\n\r\ndev.off()                              # Ouput png file\r\n\r\n# Axis positions and modifications II ------------------------------------------\r\n\r\npng(filename = \"illustrations\/fig-14-9-more axes.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(1, 1, .5, .25))\r\n\r\nplot(myV1, myV2,                       # Plot myV1 and myV2\r\n  xaxt = \"n\", yaxt = \"n\",              # Suppress axes\r\n  xlim = c(0, 10),                     # Set x range\r\n  xlab = NA)                           # Turn off X label  \r\n\r\naxis(side = 1,                         # Set up new X axis\r\n  at = c(seq(0, 10, by = 2)),          # Tick marks trom 0-10 by 2\r\n  labels = c(\"Zero\", \"Two\", \"Four\",    # Labels for tick marks\r\n    \"Six\", \"Eight\", \"Ten\"),\r\n  family = \"serif\",                    # Set font\r\n  cex.axis = 1.5,                      # Increase font size by 50%\r\n  las = 2,                             # Make labels perpendicular to axis\r\n  line = 1)                            # Put axis 1 line into the margin\r\n\r\naxis(side = 2,                         # Set up new Y axis\r\n  at = c(3:7),                         # Set tick marks from 3-7 by 1\r\n  font = 4,                            # Set font to bold italic\r\n  lwd.ticks = 2,                       # Set major tick line width at 2\r\n  pos = -.5)                           # Set axis at -.5 on X scale\r\n\r\naxis(side = 2,                         # Overlay minor tick marks on Y axis\r\n  at = c(3.5:6.5),                     # Put them on the .5 marks\r\n  labels = NA,                         # No labels\r\n  tcl = -.25,                          # Set length of tick marks\r\n  pos = -.5)                           # Set position of axis (same as above)     \r\n\r\naxis(side = 3,                         # Add another axis on top of plot\r\n  at = c(seq(0, 10, by = 1.35)),       # Set another scale\r\n  labels = c(seq(100, 170, by = 10)),  # Add labels\r\n  padj = 1,                            # Adjust labels downward\r\n  tcl = 1)                             # Put long tick marks into plot area\r\n\r\ndev.off()                              # Output png file\r\n\r\n\r\n# 14.6a Tick marks ======================================================= 14.6a\r\n\r\npng(filename = \"illustrations\/fig-14-10-ticks-BW.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(3.5, .25, .25, .25))       # Set margin sizes in inches\r\n\r\nplot(myV1, myV2, type = \"n\",           # Setup w\/no points & default x axis\r\n  xlab = NA,                           # Turn off x axis label\r\n  ylab = NA,                           # Turn off y axis label\r\n  yaxt = \"n\",                          # Turn off y axis\r\n  xlim = c(0, 10))                     # Set x axis range\r\n\r\naxis(side = 1,                         # New x axis\r\n  at = c(0:10),                        # Ticks at 0-10\r\n  line = 3)                            # Move axis down to 3rd line\r\n\r\naxis(side = 1,                         # New x axis\r\n  at = c(seq(0, 10, by = 2)),          # Major ticks at 0-10 by 2\r\n  labels = LETTERS[1:6],               # A-F as labels\r\n  line = 6)                            # Put axis on 6th line\r\n\r\naxis(side = 1,                         # New x axis\r\n  at = c(seq(1, 10, by = 2)),          # Minor ticks at 1-10 by 2\r\n  labels = NA,                         # Turn off labels for minor ticks\r\n  line = 6)                            # Overlay axis on 6th line\r\n\r\naxis(side = 1,                         # New x axis\r\n  at = c(0:10),                        # Major ticks at 0-10 \r\n  labels = c(1990:2000),               # Use years for labels\r\n  las = 2,                             # Rotate labels to be perpendicular\r\n  col = gray(.5),                      # Set main axis line to med gray\r\n  tcl = -.75,                          # Major tick length at .75 below line\r\n  col.tick = \"black\",                  # Make major ticks black\r\n  lwd = 2,                             # Make major ticks longer\r\n  line = 9)                            # Put axis on 9th line below plot\r\n\r\naxis(side = 1,                         # Add overlay axis for the minor ticks\r\n  at = c(seq(.5, 10, by = 1)),         # Minor ticks at every .5\r\n  labels = NA,                         # No labels for minor ticks\r\n  tcl = -.3,                           # Minor ticks length is .3 below line\r\n  lwd = 0,                             # Turn off main axis line for overlay\r\n  lwd.ticks = 1.25,                    # Set minor tick width to 1.25      \r\n  col.tick = \"darkgray\",               # Color for minor tick marks \r\n  line = 9)                            # Overlay axis on 9th line\r\n\r\naxis(side = 1,                         # New x axis\r\n  at = c(0:10),                        # Major ticks at 0 - 10\r\n  labels = NA,                         # Turn off labels\r\n  tcl = -.5,                           # Major tick length at .5 below line\r\n  lwd = 1.25,                          # Make major ticks thicker\r\n  line = 13)                           # Put axis on 13th line\r\n\r\ntext(x = seq(0.3, 10.3, 1),            # x values for axis labels\r\n  y = -46,                             # y value for axis labels\r\n  xpd = TRUE,                          # Allow writing outside plot area\r\n  labels = as.character(c(1990:2000)), # Years as text labels\r\n  pos = 2,                             # Left align labels\r\n  srt = 45)                            # Rotate strings 45 degrees\r\n\r\ndev.off()                              # Output png file\r\n\r\n\r\n# 14.6b Axes and Dates =================================================== 14.6b\r\n\r\ndFormat = \"%m\/%d\/%Y\"                   # Set up a date format\r\nmyDate = as.Date(c(\"10\/12\/1947\",       # Set up a date variable\r\n  \"5\/14\/1969\", \"7\/2\/1998\", \"1\/3\/2004\", #  with several dates\r\n  \"11\/24\/2012\"), dFormat)              #  using the format above\r\nmyV2 = c(14, 32, 7, 19, 11)            # Another simple variable\r\n\r\n# A simple default time plot ---------------------------------------------------\r\n\r\npng(filename = \"illustrations\/fig-14-11-dates.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(1, 1, .25, .25))\r\n\r\nplot(myDate, myV2)                     # Basic plot with defaults\r\n\r\ndev.off()                              # Output png file\r\n\r\n# A customized time plot -------------------------------------------------------\r\n\r\npng(filename = \"illustrations\/fig-14-12-dates-custom-BW.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(1, 1, .25, .25))           # Set margins - no title\r\n\r\nplot(myDate, myV2,                     # Customized plot\r\n  xlim =                               # Set range for X axis\r\n    c(as.Date(\"1\/1\/1940\", dFormat),    # Starting date\r\n    as.Date(\"12\/31\/2020\", dFormat)),   # Ending date\r\n  xaxt = \"n\",                          # Turn off X axis\r\n  xlab = \"Year\")                       # Set X axis label\r\n\r\naxis.Date(side = 1,                    # Set up new X axis w\/ major tick marks\r\n  at = seq.Date(                       # Create a sequence of dates\r\n    as.Date(\"1\/1\/1940\", dFormat),      # Starting point for sequence\r\n    as.Date(\"12\/31\/2020\", dFormat),    # Ending point for sequence\r\n    by = \"5 years\"),                   # Increment value for sequence\r\n  labels = seq(1940, 2020, by = 5),    # Labels for major tick marks\r\n  las = 2)                             # Rotate labels perpendicular\r\n\r\naxis.Date(side = 1,                    # Overlay X axis w\/ minor tick marks\r\n  at = seq.Date(                       # Create a sequence of dates\r\n    as.Date(\"1\/1\/1940\", dFormat),      # Starting point for sequence\r\n    as.Date(\"12\/31\/2020\", dFormat),    # Ending point for sequence\r\n    by = \"year\"),                      # Increment value for sequence\r\n  labels = NA,                         # Turn off labels\r\n  tcl = -.25)                          # Set length at .25 below axis\r\n\r\nabline(                                # Add a line \r\n  v=as.Date(\"10\/23\/1957\", dFormat),    # Vertical placement on 10\/23\/1957\r\n  col = \"darkgray\",                    # Set color\r\n  lwd = 2)                             # Line width at 2\r\n\r\ntext(x = as.Date(\"1\/1\/1959\", dFormat), # Label for the line - X coordinate\r\n  y = 25,                              # Y coordinate\r\n  pos = 4,                             # Put text to right of coordinates\r\n  label = \"October 23, 1957\",          # Text for label\r\n  cex = 1.5,                           # Set font size 50% bigger\r\n  family = \"mono\",                     # Set font style\r\n  font = 2)                            # Bold font\r\n\r\ndev.off()                              # Output png file\r\n\r\n# 14.7 Ad hoc text placement ============================================== 14.7\r\n\r\npng(filename = \"illustrations\/fig-14-13-text.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(.5, .5, .25, .25))         # Set plot margins - no title\r\n\r\nplot(x = 1.25, y = 1.5,                # Start plot with first point\r\n  xlim = c(0, 3),                      # Set x range of plot\r\n  ylim = c(0, 2),                      # Set y range of plot\r\n  pch = 18,                            # Diamond shaped plotting symbol\r\n  xlab = NA,                           # Turn off x axis label\r\n  ylab = NA)                           # Turn off y axis label\r\n\r\npoints(1.5, 1, pch = 18)               # Add diamond shaped point\r\ntext(1.5, 1,                           # Add text at same position\r\n  labels = \"Defaults: No pos= or adj=\")  \r\n\r\n# Using the adj= option\r\npoints(.5, .25, pch = 18)              # Add diamond shaped point\r\ntext(.5, .25,                          # Add text at same point\r\n  labels = \"adj = c(1,1)\",             # Text to add\r\n  adj = c(1, 1))                       # Adjust position to lower left\r\n\r\ntext(.5, .25,                          # Add more text at same point\r\n  labels = \"adj = c(0,0)\",             # Text to add\r\n  adj = c(0, 0))                       # Adjust position to upper right\r\n\r\npoints(.5, .75, pch = 18)              # Add diamond shaped point\r\ntext(.5, .75,                          # Add text at same point\r\n  labels = \"adj = c(1,0)\",             # Text to add\r\n  adj = c(1, 0))                       # Adjust position to lower right\r\n\r\ntext(.5, .75,                          # Add text at same point\r\n  labels = \"adj = c(0,1)\",             # Text to add\r\n  adj = c(0, 1))                       # Adjust position to upper left\r\n\r\npoints(.5, 1.75, pch = 18)             # Add diamond shaped point\r\ntext(.5, 1.75,                         # Add text at same point\r\n  labels = \"adj = c(.5,.5) \\n srt = 90\",   \r\n  srt = 90,                            # Rotate text 90 degrees\r\n  adj = c(.5, .5))                     # Adjust position to center text\r\n                                       #   (note this is also the default)\r\n\r\n# Using the pos =  option \r\npoints(1.25, 1.5, pch = 18)            # Add diamond shaped point\r\ntext(1.25, 1.5,                        # Add text at same point\r\n  labels = \"pos = 1\",                  # Text to add\r\n  pos = 1)                             # Set position to bottom centered (1)\r\n\r\ntext(1.25, 1.5,                        # Add text at same point\r\n  labels = \"pos = 2\",                  # Text to add\r\n  pos = 2)                             # Set position to left (2)\r\n\r\ntext(1.25, 1.5,                        # Add text at same point\r\n  labels = \"pos = 3\",                  # Text to add\r\n  pos = 3)                             # Set position to top centered (3)\r\n\r\ntext(1.25, 1.5,                        # Add text at same point\r\n  labels = \"pos = 4\",                  # Text to add\r\n  pos = 4)                             # Set position to right (4)\r\n\r\n# Using xpd =  to go outside the plot area\r\ntext(.45, 0,                           # Add text at bottom of plot\r\n  labels = \"This uses xpd = TRUE to allow it to go outside the plot area\",\r\n  pos = 4,                             # Place text to right of point\r\n  xpd = TRUE,                          # Set xpd = TRUE to allow\r\n  font = 3)                            # Set font to italic\r\n\r\n# plotmath expression\r\ntext(2.5, .75,                         # Add an expression w\/plotmath\r\n  labels = expression(Phi(italic(x))   # Build the expression\r\n    == over(1, sigma * sqrt(2 * pi)) *\r\n    italic(e)^ -over((x - mu)^2, 2 * sigma^2)),\r\n  srt = 45,                            # Set at 45 degree angle\r\n  cex = 1.25)                          # Increase size to 1.25\r\n\r\ntext(2.5, 1.75,                        # Add text for expression detail\r\n  labels = \"plotmath equation with \\n srt = 45\")\r\n\r\narrows(x0 = 2.5, x1 = 2.5,             # Add an arrow\r\n  y0 = 1.5, y1 = 1.1,                  # y coordinates for arrow\r\n  lwd = 3.5,                           # Set linewidth to 3.5\r\n  col = \"darkgray\")                    # Set color to dark gray\r\n\r\ndev.off()                              # Output png file\r\n\r\n <\/pre>\n<h3>R Code for Chapter 15: Graphics IV &#8212; The Fun Stuff &#8212; Color, Shapes, and Lines<\/h3>\n<pre lang=\"r-script\" line=\"1\">\r\n# ==============================================================================  \r\n# Chapter 15 -- Graphics IV: The fun stuff -- Shapes\r\n# ==============================================================================\r\n\r\n# 15.1 Doing colors ======================================================= 15.1\r\n\r\n# 15.1a Specifying colors ================================================ 15.1a\r\n\r\n# Display all named colors\r\ncolors()\r\n\r\n# A plot of normal deviates with colors ----------------------------------------\r\n\r\nmyV1 = seq(0, 1, by = .001)            # Set up some x values\r\nmyV2 = rnorm(myV1)                     # Y from random normal dist\r\n\r\n# Here we will create a vector of integer values sorting myV2 which ranges\r\n# between -4 and +4 into 8 values between 1 and 8.  We'll then use these\r\n# to select colors from a palette of 8 colors\r\n\r\nmyV2colors = abs(round(2 * myV2, 0)) + 1    \r\n\r\n# Here is the color version\r\npng(filename = \"illustrations\/fig-15-1-colors.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(bg = gray(.9))                     # Set plot background to light gray\r\nplot(myV1, myV2, type = \"n\",           # Setup plot without points\r\n  ylim = c(-4, 4),                     # Set y range\r\n  main =                               # Set title and subtitle\r\n    \"A color plot (in black and white)\",  \r\n  sub = \"(Color version available online at www.sagepub.com\/gaubatz)\",\r\n  col.lab = \"red\",                     # Color for axis labels\r\n  col.axis = \"blue\",                   # Color for axis and ticks\r\n  col.main = \"darkblue\")               # Color for main title\r\n                      \r\nrect(                                  # Create rectangle w\/plot area coords\r\n  par()$usr[1],                        # left x value at usr[1]\r\n\tpar()$usr[3],                        # Right x value at usr[3]\r\n\tpar()$usr[2],                        # Bottom y value at usr[2]\r\n\tpar()$usr[4],                        # Top y value at usr[4]\r\n  col = gray(.5),                      # Set rect color to medium gray\r\n  lwd = 8,                             # Border with line width 8\r\n  border = \"red\")                      # Set border color to red\r\n\r\nmyPalette = rainbow(8)                 # Create a palette of 8 colors \r\n\r\npoints(myV1, myV2,                     # Add points to plot\r\n  pch = 16,                            # Use solid circle\r\n  cex = .5,                            # Set the points at half size\r\n  col = myPalette[myV2colors])         # Set color based on myV2 value\r\n\r\ndev.off()                              # Output png file\r\n\r\n# Here is the grayscale version used in the book -------------------------------\r\npng(filename = \"illustrations\/fig-15-1-colors-BW.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(bg = gray(.9))                     # Set plot background to light gray\r\nplot(myV1, myV2, type = \"n\",           # Setup plot without points\r\n  ylim = c(-4, 4),                     # Set y range\r\n  main =                               # Set title and subtitle\r\n    \"A color plot (in black and white)\", \r\n  sub = \"(Color version available online at www.sagepub.com\/gaubatz)\",\r\n  col.lab = gray(.2),                  # Color for axis labels\r\n  col.axis = gray(.4),                 # Color for axis and ticks\r\n  col.main = gray(.1))                 # Color for main title\r\n                      \r\nrect(                                  # Create rectangle w\/plot area coords\r\n  par()$usr[1],                        # left x value at usr[1]\r\n\tpar()$usr[3],                        # Right x value at usr[3]\r\n\tpar()$usr[2],                        # Bottom y value at usr[2]\r\n\tpar()$usr[4],                        # Top y value at usr[4]\r\n  col = gray(.5),                      # Set rect color to medium gray\r\n  lwd = 8,                             # Border with line width 8\r\n  border = gray(.4))                   # Set border color to red\r\n\r\nmyPalette = gray.colors(8)             # Create a palette of 8 colors \r\n\r\npoints(myV1, myV2,                     # Add points to plot\r\n  pch = 16,                            # Use solid circle\r\n  cex = .5,                            # Set the points at half size\r\n  col = myPalette[myV2colors])         # Set color based on myV2 value\r\n\r\ndev.off()                              # Output png file\r\n\r\n# 15.1b Color transparency =============================================== 15.1b\r\n\r\npng(filename = \"illustrations\/fig-15-2-transparency-BW.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 2)                          # Height at 4 inches\r\n\r\npar(mar = c(0, 0, 0, 0))               # Set all margins to 0\r\nplot.new()                             # New plot\r\n\r\nabline(h = .5,                         # Add a horizontal line at .5\r\n  lwd = 4)                             # Set line width to 4\r\n\r\nsegments(                              # Add segments w\/ degrees of transparency\r\n  x0 = seq(.1, .9, by = .15),          # The first x values for the lines\r\n  y0 = .1,                             # The first y values for the lines\r\n  x1 = seq(.1, .9, by = .15),          # The second x values\r\n  y1 = .9,                             # The second y values\r\n  col = c(\"#00000010\", \"#00000025\",    # Black w\/increasing opacity\r\n    \"#00000050\", \"#000000A5\",\r\n    \"#000000D0\", \"#000000FF\"),\r\n  lwd = 40)                            # Line width = 40\r\n\r\ntext(                                  # Black labels for degree of opacity\r\n  x = seq(.1, .9, by = .15),           # x values\r\n  y = .1,                              # y value\r\n  labels =                             # Text to show alpha channel values\r\n    c(\"10\", \"25\", \"50\", \"A5\", \"D0\", \"FF\"))    \r\n\r\ntext(                                  # White labels for degree of opacity\r\n  x = seq(.1, .9, by = .15),           # x values\r\n  y = .9,                              # y value\r\n  col = \"white\",                       # Set color to white\r\n  labels =                             # Text to show alpha channel values\r\n    c(\"10\", \"25\", \"50\", \"A5\", \"D0\", \"FF\"))    \r\n\r\ndev.off()                              # Output png file\r\n\r\n\r\n# 15.2 Custom points ====================================================== 15.2\r\n\r\npng(filename = \"illustrations\/fig-15-3-pch-values.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 6)                          # Height at 6 inches\r\n\r\npar(mai = c(0, 0, 0, 0))               # No space around plot\r\n\r\nplot(x = 0, y = 0,                     # Set up an empty plot\r\n  xlim = c(0, 9), ylim = c(0, 14),     # Set x and y lengths\r\n  type = \"n\",                          # Suppress points\r\n  xaxt = \"n\", yaxt = \"n\",              # Suppress axis marks\r\n  xlab = \"\", ylab = \"\")                # Suppress axis labels\r\n\r\npoints(x = c(rep(3, 13),rep(7, 13)),   # Add points in 2 cols (x = 3 & 7)\r\n  y = c(c(13:1), c(13:1)),             # Increment y values for the 2 cols\r\n  pch = c(0:25),                       # Run through 25 values for pch\r\n  cex = 1.5)                           # Increase size to 1.5\r\n\r\ntext(x = c(rep(2, 13),rep(6, 13)),     # Add text labels also in 2 cols\r\n  y = c(c(13:1), c(13:1)),             #  with incremented y values\r\n  labels =                             # Label each point with \r\n    paste(as.character(c(0:25)),       #  the appropriate numeric\r\n    \" = \"))                            #  value.\r\n\r\ndev.off()                              # Close png device to output plot\r\n\r\n# Plotting other symbols: Alphanumerics ----------------------------------------\r\n\r\n                                       # Output a png file\r\npng(filename = \"illustrations\/fig-15-4-pch-letters.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(1, 1, .25, .25))           # Set plot margins - no title\r\n\r\nplot(x = c(1:4), y = c(2, 4, 3, 4),    # Plot some x & y values\r\n  pch = c(\"g\", \"&\", \"3\", \"H\"),         # Assign alphanumerics to each point\r\n  cex = 2,                             # Scale symbols to double size\r\n  xlim = c(0, 5), ylim = c(0, 5),      # Set range of plot\r\n  xlab = \"X\", ylab = \"Y\")              # Set axis labels\r\n\r\ndev.off()                              # Close png device to output plot\r\n\r\n# 15.2a Connectors ======================================================= 15.2a\r\n\r\npng(filename = \"illustrations\/fig-15-5-point connectors.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 6 inches\r\n\r\nlayout(matrix(c(1:12),                 # Setup a layout matrix\r\n  ncol = 4,                            #   with 4 columns & 3 rows\r\n  byrow = T),                          #   to fill by row.\r\n  heights = c(.45, .45, .1),           # Relative heights\r\n  widths = c(.05, .3, .3, .3))         # Relative widths\r\n\r\nlayout.show(12)                        # Show the layout\r\n\r\npar(mai = c(.1, .1, .5, .1))           # Set margins \r\nplot.new()                             # Skip the first grid location\r\nplot(x = c(1:4), y = c(2, 4, 3, 4),    # Plot some x & y values\r\n  type = \"p\",                          # Set connectors to just points\r\n  xlim = c(0, 5), ylim = c(0, 5),      # Set range of plot\r\n  xaxt = \"n\",                          # Turn off x axis\r\n  xlab = NA, ylab = NA,                # Turn off axis labels\r\n  main = \"type = 'p'\")                 # Add main title\r\n\r\nplot(x = c(1:4), y = c(2, 4, 3, 4),    # Plot some x & y values\r\n  type = \"l\",                          # Set connectors to just lines\r\n  xlim = c(0, 5), ylim = c(0, 5),      # Set range of plot\r\n  xaxt = \"n\", yaxt = \"n\",              # Turn off axes\r\n  xlab = NA, ylab = NA,                # Turn off axis labels\r\n  main = \"type = 'l'\")                 # Add main title\r\n    \r\nplot(x = c(1:4), y = c(2, 4, 3, 4),    # Plot some x & y values\r\n  type = \"b\",                          # Set connectors to both lines & points\r\n  xlim = c(0, 5), ylim = c(0, 5),      # Set range of plot\r\n  xaxt = \"n\", yaxt = \"n\",              # Turn off axes\r\n  xlab = NA, ylab = NA,                # Turn off axis labels\r\n  main = \"type = 'b'\")                 # Add main title\r\n    \r\nplot.new()                             # Skip the 5th layout space\r\nplot(x = c(1:4), y = c(2, 4, 3, 4),    # Plot some x & y values\r\n  type = \"s\",                          # Set connectors to stair steps\r\n  xlim = c(0, 5), ylim = c(0, 5),      # Set range of plot\r\n  xlab = NA, ylab = NA,                # Turn off axis labels\r\n  main = \"type = 's'\")                 # Add main title\r\n\r\nplot(x = c(1:4), y = c(2, 4, 3, 4),    # Plot some x & y values\r\n  type = \"h\",                          # Set to histogram-style vertical lines\r\n  xlim = c(0, 5), ylim = c(0, 5),      # Set range of plot\r\n  yaxt = \"n\",                          # Turn off y axis\r\n  xlab = NA, ylab = NA,                # Turn off axis labels\r\n  main = \"type = 'h'\")                 # Add main title\r\n    \r\nplot(x = c(1:4), y = c(2, 4, 3, 4),    # Plot some x & y values\r\n  type = \"n\",                          # Turn off points\r\n  xlim = c(0, 5), ylim = c(0, 5),      # Set range of plot\r\n  yaxt = \"n\",                          # Turn off y axis\r\n  xlab = NA, ylab = NA,                # Turn off axis labels\r\n  main = \"type = 'n'\")                 # Add main title\r\n    \r\n    \r\ndev.off()                              # Close png device to output plot\r\n\r\n# 15.2b The symbols() approach =========================================== 15.2b\r\n                                       # Output a png file\r\npng(filename = \"illustrations\/fig-15-6-symbols-BW.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 6 inches\r\n\r\npar(mai = c(1, 1, .25, .25))\r\n\r\nsymbols(x = c(1:3), y = c(2, 4, 3),    # Set up some X and Y values\r\n  circles = c(1, 2, 1.5),              # Circle radii\r\n  bg = gray.colors(3),                 # Circle colors from gray palette\r\n  xlim = c(0,5), ylim = c(0,5),        # Set range of plot\r\n  xlab = \"X\", ylab = \"Y\")              # Set axis labels\r\n\r\nmyRect = rbind(c(1.5, .75),            # Set widths for 2 rectangles\r\n  c(.75, 2))                           # Set heights for 2 rectangles\r\n\r\nsymbols(x = c(2, 4), y = c(2, 3),      # 2nd symbol plot X & Y coords\r\n  add = TRUE,                          # Overlay on previous plot     \r\n  rectangles = myRect,                 # Add rectangles\r\n  bg = c(\"lightgray\", \"darkgray\"))     # Rectangle colors      \r\n\r\ndev.off()                              # Close png device to output plot\r\n\r\n# 15.3 Adding Lines ======================================================= 15.3\r\n\r\npng(filename = \"illustrations\/fig-15-7-line types.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(0, 0, 0, 0))               # Set for no margins\r\n\r\nplot.new()                             # Start a plot\r\n  \r\nsegments(                              # Add segments w\/ degrees of transparency\r\n  x0 = .1,                             # The first x values for the lines\r\n  y0 = seq(.9, .1, -.16),              # The first y values for the lines\r\n  x1 = .6,                             # The second x values\r\n  y1 = seq(.9, .1, -.16),              # The second y values\r\n  lty = c(1:6),                        # A vector of line types\r\n  lwd = 2)                             # Line width = 2\r\n\r\n\r\ntext(                                  # Add labels \r\n  x = .65,                             # x values\r\n  y = seq(.9, .1, -.16),               # y value\r\n  pos = 4,                             # Text to right of point\r\n  cex = 1.25,                          # Text size\r\n  label = c(\"lty = 1 or 'solid'\",      # The set of labels\r\n    \"lty = 2 or 'dashed'\",             \r\n    \"lty = 3 or 'dotted'\",\r\n    \"lty = 4 or 'dotdash'\",\r\n    \"lty = 5 or 'longdash'\",\r\n    \"lty = 6 or 'twodash'\"))\r\n \r\ndev.off()                              # Output png file\r\n\r\n# 15.3a Basic lines ====================================================== 15.3a\r\n\r\nmyV1 = c(1, 2, 4, 5, 8)                # Some data to work with\r\nmyV2 = c(1.5, 3, 2, 7, 6)\r\n\r\npng(filename = \"illustrations\/fig-15-8-lines-BW.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mar = c(4.2, 4, 1, 1))             # Set margin widths in lines\r\n\r\nplot(myV1, myV2,                       # A simple default scatterplot\r\n  pch = 19, col = \"darkgray\",          # Solid point markers in darkgray\r\n  xlim = c(0, 10), ylim = c(0, 10))    # Set range for x & y axes\r\n\r\n# Line 1 - a horizontal dashed line\r\nabline(h = 1.5,                        # Add a horizontal line at y = 1.5\r\n  lwd = 3,                             # Give it a width of 3     \r\n  lty = 2,                             # Dash it all\r\n  col = gray(.4))                      # Make it medium gray     \r\n\r\ntext(x = 8, y = 2.2,                   # Add label for line 1\r\n  label = \"Line 1\",                    # Label text\r\n  cex = 2,                             # Set size to 2\r\n  col = gray(.4))                      # Set color to gray .4\r\n\r\n# Line 2 - a vertical line with long dashes\r\nabline(v = 4,                          # Add a vertical line at x = 4\r\n  lwd = 8,                             # Give it a width of 8     \r\n  lty = \"longdash\",                    # Use long dashes\r\n  col = gray(.7))                      # Make it light gray     \r\n\r\ntext(x = 3.5, y = 6,                   # Add label for line 2\r\n  label = \"Line 2\",                    # Label text\r\n  cex = 2,                             # Set size to 2\r\n  srt = 90,                            # Rotate text to vertical\r\n  col = gray(.7))                      # Set color to light gray\r\n\r\n# Line 3 - abline using coefficients\r\nabline(a = 3.2,                        # Add a line w\/ intercept = 3.2\r\n  b = 1.5,                             #   and slope = 1.5\r\n  lwd = .5,                            # Give it a width of .5     \r\n  col = gray(.2))                      # Make it dark gray     \r\n\r\ntext(x = 1.5, y = 6.5,                 # Add label for line 3\r\n  label = \"Line 3\",                    # Label text\r\n  cex = 2,                             # Set size to 2\r\n  srt = 44,                            # Rotate text\r\n  col = gray(.2))                      # Set color to dark gray\r\n\r\n# Line 4 - Line segment\r\nlines(x = c(6, 12),                    # x values for line segment\r\n  y = c(11, 4),                        # y values for line segment\r\n  lty = 4,                             # Dot-dash pattern\r\n  col = gray(.1),                      # Make it dark gray\r\n  lwd = 2,                             # Set line width at 2\r\n  xpd = TRUE)                          # Allow outside plot area\r\n\r\ntext(x = 8, y = 9.75,                  # Add label for line 4\r\n  label = \"Line 4\",                    # Label text\r\n  cex = 2,                             # Set size to 2\r\n  srt = -34,                           # Rotate text\r\n  col = gray(.1),                      # Set color to dark gray\r\n  xpd = TRUE)                          # Allow to extend beyond plot\r\n\r\n# Line 5 - A regression line \r\nmyReg = lm(myV2 ~ myV1)                # A simple regression model\r\n                                       \r\nabline(reg = myReg,                    # Use regression model to define line\r\n  col = \"black\")                       # Set color\r\n\r\ntext(x = 6, y = 6,                     # Add label for line 5\r\n  label = \"Line 5\",                    # Label text\r\n  cex = 2,                             # Set size to 2\r\n  srt = 23,                            # Rotate text\r\n  col = \"black\")                       # Set color\r\n\r\ndev.off()                              # Output the png file\r\n\r\n\r\n# 15.3b Data-based line segments ========================================= 15.3b\r\n\r\nmyV1 = c(1, 2, 4, 5, 7, 8, 9)          # Some data to work with\r\nmyV2 = c(2, 3, 2, 7, 6, 8, 6)\r\n\r\nmyReg = lm(myV2 ~ myV1)                # A quick regression model                       \r\n\r\npng(filename = \"illustrations\/fig-15-9-line-segments-BW.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(1, 1, .25, .25))           # Set margin widths in inches\r\n\r\nplot(myV1, myV2,                       # The scatter plot\r\n  pch = 19,                            # Solid point markers\r\n  xlim = c(0, 10), ylim = c(0, 10))    # Set range for x & y axes\r\n\r\nabline(reg = myReg)                    # Use regression model to define line\r\n\r\nsegments(x0 = myV1, x1 = myV1,         # Add line segments from observed\r\n  y0 = myV2, y1 = myReg$fitted.values) #  to fitted values\r\n\r\n# Add model equation text to plot\r\n# First paste together the model text\r\nmyModel = paste(                       # Paste together reg model as text\r\n  \"Model: \",                             \r\n  names(myReg$model)[1],               # Get dependent variable\r\n  \" = \",                              \r\n  round(myReg$coefficients[1], 2),     # Get y intercept coefficient\r\n  \" + \",\r\n  round(myReg$coefficients[2], 2),     # Get slope coefficient\r\n  names(myReg$model)[2],               # Get independent variable\r\n  sep = \"\")                            # No space between paste elements    \r\n\r\ntext(x = 10, y = 1.5,                  # Add model to plot\r\n  label = myModel,                     # The model as text \r\n  pos = 2)                             # Right side alignment\r\n\r\n# Get the R-squared value from the summary function\r\nmyR2 = round(summary(myReg)$r.squared, 2)\r\n\r\n# Add the R-squared value to the plot\r\ntext(x = 10, y = .5,                   # Add R-squared value to plot\r\n  label = bquote(R^2 == .(myR2)),      # Use bquote to paste elements\r\n  pos = 2)                             # Right side alignment\r\n\r\ndev.off()                              # Output the png file\r\n\r\n\r\n# 15.3c Arrows =========================================================== 15.3c \r\n\r\n# A solid arrowhead function\r\n# \r\n# This function draws an arrow from (x0,y0) to (x1,y1) with a solid\r\n# arrowhead. \r\n#\r\n# The default arrowhead length is .25 inches. \r\n# The default angle of the arrowhead edges is 30 degrees.\r\n# The other parameters are standard for line and polygon formatting.\r\n#\r\n# The basic logic here is to use matrix rotation to translate the arrowhead\r\n# coordinates.\r\n#\r\n# Note that R's trigonometric functions work with radians rather than degrees\r\n#\r\nmyArrow = function(x0, y0, x1, y1,     # Set up arrow function ----------------+\r\n  L = .25,                             # Default arrowhead length              |\r\n  angle = 30,                          # Default angle of arrowhead            |\r\n  code = 2,                            # Default arrowhead at x1,y1            |\r\n  col = par(\"fg\"),                     # Default color                         |\r\n  ljoin = par(\"ljoin\"),                # Default line joint style (0)          |\r\n  lty = par(\"lty\"),                    # Default line type                     |\r\n  lwd = par(\"lwd\"),                    # Default line width                    |\r\n  xpd = FALSE){                        # Default stay within plot area         |\r\n                                       # Start function code                   |\r\n    if(code == 1){                     # Reverse arrow direction               |\r\n      tmp = x1; x1 = x0; x0 = tmp      # Switch x values                       |\r\n    }                                  #                                       |\r\n    if(code == 1){                     #                                       |\r\n      tmp = y1; y1 = y0; y0 = tmp      # Switch y values                       |\r\n    }                                  #                                       |\r\n#                                                                              |\r\n# We need to control for the aspect ratio or for different x,y scales          |\r\n#   in setting up the arrow heads. We'll do that by translating the            |\r\n#   usr parameter setting from the original x,y units to units based           |\r\n#   on the plot dimensions measured in inches.  This will allow us to          |\r\n#   adjust the angles to account for different x and y scales. Note,           |\r\n#   however, that rescaling the plot after it is drawn will distort            |\r\n#   the arrowheads.                                                            |\r\n#                                                                              |\r\n    X0 = (x0 - par()$usr[1])\/(par()$usr[2] - par()$usr[1]) * par()$fin[1]  #   |\r\n    Y0 = (y0 - par()$usr[3])\/(par()$usr[4] - par()$usr[3]) * par()$fin[2]  #   |\r\n    X1 = (x1 - par()$usr[1])\/(par()$usr[2] - par()$usr[1]) * par()$fin[1]  #   |\r\n    Y1 = (y1 - par()$usr[3])\/(par()$usr[4] - par()$usr[3]) * par()$fin[2]  #   |\r\n#                                                                              |    \r\n    oldusr = par(\"usr\")                # Save original usr settings            |\r\n    par(usr = c(0, par(\"fin\")[1],      # Set up new usr settings based         |\r\n              0, par(\"fin\")[2]))       #  on plot dimensions in inches         |\r\n#                                                                              |\r\n    t = angle * pi\/180                 # Convert angle degrees to radians      |\r\n    slope = (Y1 - Y0)\/(X1 - X0)        # Calculate slope of line               |\r\n    S = atan(slope)                    # Slope angle in radians                |\r\n                                       #                                       |\r\n    M = ifelse(X1 < X0, -1, 1)         # Set a marker for X1 < X0              |\r\n                                       # Set length of vector XA               |\r\n    XA = sqrt((X1 - X0)^2 + (Y1 - Y0)^2)                                   #   |\r\n#                                                                              |\r\n# Get arrowhead vertices from rotated vectors                                  |\r\n    XC = X0 + M * ((XA - L) * cos(S) + L * tan(t) * sin(S))                #   |\r\n    YC = Y0 + M * ((XA - L) * sin(S) - L * tan(t) * cos(S))                #   |\r\n    XB = X0 + M * ((XA - L) * cos(S) - L * tan(t) * sin(S))                #   |\r\n    YB = Y0 + M * ((XA - L) * sin(S) + L * tan(t) * cos(S))                #   |\r\n#                     |                                                        |\r\n# Draw arrow line stopping at beginning of the arrowhead                       |\r\n    lines(x = c(X0, X1 - M * L * cos(S)),                                  #   |\r\n      y = c(Y0, Y1 - M * L * sin(S)),                                      #   |\r\n      lty = lty, lwd = lwd,            # Apply line format options             |\r\n      col = col, xpd = xpd,            #                                       |\r\n      ljoin = ljoin)                   #                                       |\r\n    polygon(x = c(X1, XB, XC),         # Draw arrow head                       |\r\n      y = c(Y1, YB, YC),               #  at vertices                          |\r\n      col = col,                       # Apply format options                  |\r\n      border = col,                    #                                       |\r\n      xpd = xpd)                       #                                       |\r\n    par(usr = oldusr)                  # Reset to original usr settings        |\r\n}                                      # End of myArrow function --------------+\r\n\r\n# A plot of arrows \r\npng(filename = \"illustrations\/fig-15-10-arrows-BW.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(.5, .5, .25, .25))         # Set margin widths in lines\r\n\r\nplot(x = 0, y = 0, type = \"n\",         # Set up a blank plot\r\n  xlim = c(0, 5), ylim = c(0, 5),      # Define x and y range\r\n  xlab = \"\", ylab = \"\")                # Turn off axis labels\r\n\r\n# Diverse arrows\r\narrows(x0 = 3, y0 = 4.25, x1 = 5, y1 = .25, \r\n  length = 1, angle = 15, lwd = 2)\r\narrows(x0 = 1.25, y0 = 1, x1 = 3.25, y1 = .75, \r\n  col = \"darkgray\", code = 3)\r\nmyArrow(x0 = 0, y0 = 1.5, x1 = 3.5, y1 = 4.5, \r\n  col = \"black\")\r\nmyArrow(x0 = 1, y0 = .5, x1 = 4.5, y1 = 0, \r\n  col = gray(.6)) \r\nmyArrow(x0 = 4, y0 = 3.5, x1 = 1, y1 = 1.5, L=.5, \r\n  col = gray(.4), lwd = 2)\r\nmyArrow(x0 = 2.5, y0 = 1.25, x1 = 1, y1 = 4.2, \r\n  col = gray(.5), lwd = 4)\r\nmyArrow(x0 = 6, y0 = 5, x1 = 4, y1 = 3, \r\n  col = \"gray\", lwd = 8, angle = 20, L=.75, xpd = T)\r\nmyArrow(x0 = 0, y0 = 5, x1 = 4, y1 = 5, \r\n  col = \"gray\", lwd = 3, angle = 40, code = 1)  \r\nmyArrow(x0 = .5, y0 = .5, x1 = 4, y1 = 1.5, \r\n  col = \"darkgray\", lwd = 1, angle = 20, L=.1)\r\n\r\ndev.off()                              # Output png file\r\n\r\n\r\n# 15.3d Grid lines ======================================================= 15.3d\r\n\r\npng(filename = \"illustrations\/fig-15-11-grids.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 6)                          # Height at 6 inches\r\n\r\nlayout(matrix(c(1, 1, 2, 3),           # Set layout of 3 plots\r\n  ncol = 2,                            #  organized as 2 columns\r\n  byrow = T))                          #  and filled by row.\r\n\r\npar(mai = c(.5, .5, .5, .25))          # Set the margin sizes\r\n\r\nplot(0:10, pch = 19,                   # Plot 1 w\/solid dots\r\n  main = \"Horizontal Lines\",           # Title for plot 1\r\n  xlab = NA, ylab = NA,                # Turn off axis labels\r\n  yaxs = \"i\")                          # Eliminate y axis overage\r\n\r\ngrid(nx = NA,                          # Create a grid w\/o verticals\r\n  ny = 5,                              #  and 5 horizontal partitions\r\n  lty = 1,                             # Set line type to solid\r\n  lwd = 2,                             # Set line width to 2\r\n  col = \"gray\")                        # Set color\r\n\r\npoints(0:10, pch = 19)                 # Redo points on top of grid\r\n\r\nplot(0:10, pch = 19,                   # Plot 2 w\/solid dots\r\n  main = \"Default Grid\",               # Title for plot 2\r\n  xlab = NA, ylab = NA)                # Turn off axis labels\r\n\r\ngrid()                                 # Add default grid on tick marks\r\n                                       \r\npar(mai = c(0, 0, 0, 0))               # Set margins to zero\r\n   \r\nplot(0:10, type = \"n\", yaxt = \"n\")     # Create an empty plot\r\ngrid(nx = 10, ny = 10,                 # Add gridlines\r\n  lty = 1,                             # Use solid lines\r\n  col = \"black\")                       # Set color to black\r\n\r\npar(usr = c(0, 1, 0, 1))               # Use usr par\r\ntext(.5, .55,                          # Add text at center\r\n  labels = \"An empty grid\",            # Text to add\r\n  cex = 2)                             # Set text size at 2\r\n\r\ndev.off()                              # Output png file\r\n\r\n# 15.4 Shapes ============================================================= 15.4\r\n\r\n# Some demonstration polygons\r\n\r\nmyX1 = c(1, 2, 3, 6, 7, 8)             # Set up some coordinates to work with\r\nmyY1 = c(2, 8, 4, 4, 6, 3)\r\nmyX2 = seq(1, 3, by = .001)\r\nmyY2 = 9 - (2 - myX2)^2\r\nmyX2 = c(1, myX2, 3)\r\nmyY2 = c(7, myY2, 7)\r\n\r\npng(filename = \"illustrations\/fig-15-12-polygons-BW.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(.5, .5, .25, .25))         # Set margin widths in inches\r\n\r\nplot(x = 0, y = 0, type = \"n\",         # Set up a blank plot\r\n  xlim = c(0, 10), ylim = c(0, 10),    # Define x and y range\r\n  xlab = \"\", ylab = \"\")                # Turn off axis labels\r\n\r\npolygon(x = c(4, 4, 8, 8),             # Set x &#038; y values for rectangle \r\n  y = c(1, 2, 2, 1),     \r\n  col = gray(.7))                      # Fill color\r\n\r\npolygon(x = c(6, 6, 9, 9),             # Set x &#038; y values for rectangle\r\n  y = c(6, 8, 8, 6),    \r\n  border = gray(.2),                   # Add a dark gray border\r\n  lwd = 4,                             # Line width 4 for border\r\n  col = gray(.9))                      # Light gray fill for rectangle\r\n\r\npolygon(myX2, myY2,                    # A polygon using preset points\r\n  lwd = 2, lty = 2, border = \"black\",  # Dashed border with line width 2\r\n  col = gray(.3))                      # Fill color\r\n\r\npolygon(myX1, myY1,                    # A polygon with preset points\r\n  border = gray(.1),                   # Border color\r\n  lwd = 3,                             # Border &#038; shading width = 3\r\n  density = 10,                        # Shading density = 10 lines\/inch\r\n  angle = 135,                         # Shading line angle = 135 degrees\r\n  col = gray(.6))                      # Color of shading lines\r\n\r\n\r\ndev.off()                              # Output png file\r\n\r\n# A more complex shapefile -----------------------------------------------------\r\n\r\n# California Shapefile Read\r\n# This reads in the coordinates for the shape of California, extracted from\r\n# a google earth kml file.\r\n\r\n# Note that the location of this datafile will change to somewhere on the \r\n# www.sagepub.com\/gaubatz site\r\n\r\nCAshape = read.csv(\r\n  file = \"http:\/\/www.kktg.net\/R\/calif-coords.csv\", header = FALSE)\r\n\r\nsummary(CAshape)                       # Show structure &#038; range of coordinates\r\n\r\npng(filename = \"illustrations\/fig-15-13-california.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 4,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(0, 0, 0, 0))               # no margins\r\n\r\nplot(x = 0, y = 0, type = \"n\",         # Set empty plot\r\n  xlim = c(-125, -113),                # X range\r\n  ylim = c(32, 43),                    # Y range\r\n  axes = FALSE,                        # Turn off axes\r\n  xlab = NA, ylab = NA)                # Turn off labels\r\n\r\npolygon(x = CAshape$V1,                # Set x &#038; y values from CA coordinates\r\n  y = CAshape$V2, \r\n  col = gray(.8))                      # Fill color\r\n\r\ntext(x = -117, y = 39.5,               # Position label\r\n  label = \"California\",                # Label text\r\n  cex = 2.5)                           # Set size to 2.5\r\n\r\ndev.off()                              # Output png file\r\n\r\n# This routine draws a two-sided hypothesis test -------------------------------\r\n\r\noptions(scipen = 6)                    # Stop sci notation if <6 dec places\r\n\r\n# Provided Values\r\nmean1 = 3.35                           # First sample mean\r\nmean2 = 3.09                           # Second sample mean\r\nsd1 = 1.24                             # First sample std deviation\r\nsd2 = 1.21                             # Second sample std deviation\r\nn1 = 142                               # First sample size\r\nn2 = 126                               # Second sample size\r\n\r\n# Calculated Values\r\n\r\nseBlend = sqrt(sd1^2\/n1 + sd2^2\/n2)    # Blended std. error\r\ndMeans = max(mean1, mean2) -           # Calculate difference in means \r\n  min(mean1, mean2)\r\nz = (dMeans)\/seBlend                   # Calculate z score\r\nrangeZ = ceiling(abs(z)) + 2           # Set range for x values\r\nrangeZ = ifelse(rangeZ < 4, 4, rangeZ) # Set minimum range of +\/- 4\r\n\r\n# Hypothesis test results\r\np = 2 * (1 - pnorm(z))                 # p value for 2 sided z-test\r\nptext = round(p, 2)                    # Version of p for annotation\r\nOneMinusP = round(1 - p, 2)            # Version of 1 - p for annotation\r\n\r\n# Graphing values\r\nX1 = seq(-rangeZ, rangeZ, by = .001)   # Set up sequence of x values\r\nY1 = dnorm(X1)                         # Set up t curve values for each x\r\nX2 = X1                                # Second x to use for filling curve\r\nY2 = Y1                                # Second y to use for filling curve\r\ncd = data.frame(X1, Y1, X2, Y2)        # Join in data frame\r\n\r\npng(filename = \"illustrations\/fig-15-14-normal-curve-BW.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(1, 1, .25, .25))           # Set margin - no title\r\n\r\n\r\n# Plot components\r\nplot(cd$X1, cd$Y1,                     # Plot normal distribution\r\n  type = \"l\", lwd = 2,                 # Line plot with linewidth =2\r\n  ylim = c(0, .5),                     # Set range for y axis\r\n  xlab = \"z-score\",                    # Add x axis label\r\n  ylab = \"Probability\",                # Add y axis label\r\n  yaxs = \"i\")                          # Eliminate y axis overage\r\n\r\n\r\nabline(h = 0)                          # Horizontal boundary line at y = 0\r\n\r\nlines(x = c(z, z), y = c(0, .43))      # Add short vertical line at z\r\nlines(x = c(-z, -z), y = c(0, .43))    # Add short vertical line at -z    \r\n\r\n# Set up borders of shaded regions\r\nxz1 = which(\r\n  cd$X1 == max(cd$X1[cd$X1 < z]))      # Find x just to left of z\r\ncd$Y2[xz1] = 0                         # Set y for that x to 0      \r\ncd$X2[xz1] = cd$X2[xz1 - 1]            # Shift max x over for vertical line\r\nxz2 = which(\r\n  cd$X1 == min(cd$X1[cd$X1 > -z]))     # Find x just to right of -z\r\ncd$Y2[xz2] = 0                         # Set y for that x to 0      \r\ncd$X2[xz2] = cd$X2[xz2 + 1]            # Shift max x over for vertical line\r\n\r\nxz3 = which(\r\n  cd$X1 == min(cd$X1[cd$X1 > z]))      # Find x just to right of z\r\ncd$Y2[xz3] = 0                         # Set y for that x to 0      \r\ncd$X2[xz3] = cd$X2[xz3 + 1]            # Shift max x over for vertical line\r\n\r\nxz4 = which(\r\n  cd$X1 == max(cd$X1[cd$X1 < -z]))     # Find x just to left of -z\r\ncd$Y2[xz4] = 0                         # Set y for that x to 0      \r\ncd$X2[xz4] = cd$X2[xz4 - 1]            # Shift max x over for vertical line\r\n    \r\n# Do shading\r\npolygon(cd$X2[cd$X1 < z &#038; cd$X1 > -z], # Fill for center region\r\n  cd$Y2[cd$X1 < z &#038; cd$X1 > -z],\r\n  col = \"light gray\")                  # Set color\r\n  \r\npolygon(cd$X2[cd$X1 < -z],             # Fill for left tail \r\n  cd$Y2[cd$X1 < -z], \r\n  col = \"darkgray\")                    # Set color\r\n  \r\npolygon(cd$X2[cd$X1 > z],              # Fill for right tail \r\n  cd$Y2[cd$X1 > z],\r\n  col = \"darkgray\")                    # Set color\r\n\r\nabline(v = 0)                          # Vertical line at x = 0    \r\n\r\n# Add labels\r\ntext(x = z, y = .42, pos = 4,          # Add label showing z-score 1\r\n  labels = paste(\"z = +\", round(z, 2)))\r\n  \r\ntext(x = -z, y = .42, pos = 2,         # Add label showing z-score 2\r\n  labels = paste(\"z = -\", round(z, 2)))  \r\n\r\nlabel1 = paste(                        # Annotation for area under curve\r\n  \"Area under curve\\nbetween z values = \", \r\n  OneMinusP)\r\n  \r\nlabel2 = paste(                        # Annotation for area in tails\r\n  \"Area under curve\\nin both tails = \",\r\n  ptext)\r\n\r\ntext(x = -rangeZ\/1.4, y = .2,          # Add label for main pr region\r\n  labels = label1,                     # Use text created above\r\n  cex = .75)                           # Font size \r\n  \r\narrows(x0 = -rangeZ\/1.4, x1 = -z\/2,    # Arrow from text to main pr region\r\n  y0 = .17, y1 = .1,\r\n  length = .15)                        # Set arrowhead size\r\n  \r\ntext(x = rangeZ\/1.4, y = .2,           # Add label for tails\r\n  labels = label2,                     # Use label created above\r\n  cex = .75)                           # Set text size\r\n\r\narrows(x0 = z + (rangeZ - z)\/2,        # Add arrow from text to tails label\r\n  y0 = .17,      \r\n  y1 = .02,\r\n  length = .15)\r\n  \r\ndev.off()                              # Output the png file\r\n\r\n# 15.5 Incorporating images into plots ==================================== 15.5\r\n\r\n# Kittens and Skulls -- raster images\r\n\r\nlibrary(\"png\")                         # Load the png library\r\n                                       # Then read kitten & skull images\r\nkittenPNG = readPNG(\"illustrations\/kitten-BW.png\")\r\nskullPNG = readPNG(\"illustrations\/skull.png\")\r\n\r\nskull = skullPNG[,,-4]                 # remove alpha channel\r\nkitten = kittenPNG[,,-4]               # remove alpha channel\r\n\r\nskull2 = as.raster(skull)              # Convert skull to raster\r\nkitten2 = as.raster(kitten)            # Convert kitten to raster\r\n\r\n# Some Data from Google ngrams showing average occurance of skull\/s \r\n# and kittens\/cats averaged by decade.  Decade\/skulls\/kittens\r\nd0 = c(1900, 1.60, 1.65)\r\nd1 = c(1910, 1.59, 1.67)\r\nd2 = c(1920, 1.45, 1.86)\r\nd3 = c(1930, 1.46, 1.76)\r\nd4 = c(1940, 1.37, 1.73)\r\nd5 = c(1950, 1.32, 1.68)\r\nd6 = c(1960, 1.26, 1.66)\r\nd7 = c(1970, 1.19, 1.96)\r\nd8 = c(1980, 1.08, 2.26)\r\nd9 = c(1990, 1.06, 2.45)\r\n\r\nkdata = rbind(d0, d1, d2, d3, d4, d5, d6, d7, d8, d9)\r\nkdata = data.frame(kdata)              # Combine into data frame\r\nrow.names(kdata) = kdata$V1            # Set years as row names\r\ncolnames(kdata) =                      # Set up column names\r\n  c(\"Decade\", \"Skulls\", \"Kittens\")\r\n\r\n                                       # Create a png output file\r\npng(filename = \"illustrations\/fig-15-15-images-plot.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mar = c(4.2, 4, 1, 1))             # Set margin widths in lines\r\n\r\nplot(x = kdata$Decade,                 # Set up background plot\r\n  y = kdata$Kittens,  \r\n  xlim = c(1890, 2000),                # Set x range    \r\n  ylim = c(0, 4),                      # Set y range\r\n  xlab = \"Kittens v. Skulls\",          # Create X axis label\r\n  ylab = \"Occurence Rate (x10,000)\",   # Create Y axis label\r\n  type = \"l\")                          # Use lines without points\r\n\r\npoints(x = kdata$Decade,               # Add skull points\r\n  y = kdata$Skulls,        \r\n  type = \"l\",                          # Connect with lines\r\n  lty = 2)                             # Use dashed line\r\n\r\nrasterImage(image = kitten2,           # Add kitten images \r\n  xleft = kdata$Decade - 3,\r\n  xright = kdata$Decade + 3,           # Left & right x values for kittens\r\n  ybottom = kdata$Kittens - .3,\r\n  ytop = kdata$Kittens + .3)           # Top & bottom y values for kittens\r\n\r\nrasterImage(image = skull2,            # Add skull images \r\n  xleft = kdata$Decade - 2,\r\n  xright = kdata$Decade + 2,           # Left & right x values for skulls\r\n  ybottom = kdata$Skulls - .25,\r\n  ytop = kdata$Skulls + .25)           # Top & bottom y values for skulls \r\n\r\ndev.off()                              # Ouput png file\r\n\r\n\r\n# A PDF version utilizing transparency -----------------------------------------\r\n\r\nkittenPNG = readPNG(\"illustrations\/kitten-BW.png\")\r\nskullPNG = readPNG(\"illustrations\/skull.png\")\r\n\r\nskull = skullPNG\r\nkitten = kittenPNG\r\n\r\nskull2 = as.raster(skull)              # Convert skull to raster\r\nkitten2 = as.raster(kitten)            # Convert kitten to raster\r\n\r\n# Some Data from Google ngrams showing average occurance of skull\/s \r\n# and kittens\/cats averaged by decade.  Decade\/skulls\/Kittens\r\nd0 = c(1900, 1.60, 1.65)\r\nd1 = c(1910, 1.59, 1.67)\r\nd2 = c(1920, 1.45, 1.86)\r\nd3 = c(1930, 1.46, 1.76)\r\nd4 = c(1940, 1.37, 1.73)\r\nd5 = c(1950, 1.32, 1.68)\r\nd6 = c(1960, 1.26, 1.66)\r\nd7 = c(1970, 1.19, 1.96)\r\nd8 = c(1980, 1.08, 2.26)\r\nd9 = c(1990, 1.06, 2.45)\r\n\r\nkdata = rbind(d0, d1, d2, d3, d4, d5, d6, d7, d8, d9)\r\nkdata = data.frame(kdata)\r\nrow.names(kdata) = kdata$V1\r\ncolnames(kdata) = c(\"Decade\", \"Skulls\", \"Kittens\")\r\n\r\n                                       # Create a pdf output file\r\npdf(file = \"illustrations\/fig-15-16-images-plot-BW.pdf\",\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\n\r\nplot(x = kdata$Decade,                 # Set up background plot\r\n  y = kdata$Kittens,  \r\n  xlim = c(1890, 2000),                # Set range of x axis     \r\n  ylim = c(0, 4),                      # Set range for y axis\r\n  xlab = \"Kittens v. Skulls\",          # Create X axis label\r\n  ylab = \"Occurence Rate (x10,000)\",   # Create Y axis label\r\n  type = \"l\")                          # Connect points with a line\r\n\r\npoints(x = kdata$Decade,               # Add points for skulls\r\n  y = kdata$Skulls,\r\n  type = \"l\",                          # Connect points with a line\r\n  lty = 2)                             # Use dashed line    \r\n\r\nrasterImage(image = kitten2,           # Add kitten images \r\n  xleft = kdata$Decade - 3,            # Left x values\r\n  xright = kdata$Decade + 3,           # Right x values for kittens\r\n  ybottom = kdata$Kittens - .3,        # Bottom y values    \r\n  ytop = kdata$Kittens + .3)           # Top y values for kittens\r\n\r\nrasterImage(image = skull2,            # Add skull images \r\n  xleft = kdata$Decade - 2,            # Left x values    \r\n  xright = kdata$Decade + 2,           # Right x values for kittens\r\n  ybottom = kdata$Skulls - .25,        # Bottom y values    \r\n  ytop = kdata$Skulls + .25)           # Top y values for kittens \r\n\r\ndev.off()                              # Close pdf file and output\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>R Code for Chapter 14: Graphics III &#8212; The Fun Stuff &#8212; Text # ============================================================================== # Chapter 14 &#8212; Graphics III: The fun stuff &#8212; Text # ============================================================================== default.par = par() # Set default parameters for recovery # 14.1 Adding text ======================================================== 14.1 myV1 = c(1, 2, 3, 4, 5) # Set up some temporary [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":98,"menu_order":0,"comment_status":"closed","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-517","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/kktg.net\/sgr\/wp-json\/wp\/v2\/pages\/517","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/kktg.net\/sgr\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/kktg.net\/sgr\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/kktg.net\/sgr\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/kktg.net\/sgr\/wp-json\/wp\/v2\/comments?post=517"}],"version-history":[{"count":1,"href":"http:\/\/kktg.net\/sgr\/wp-json\/wp\/v2\/pages\/517\/revisions"}],"predecessor-version":[{"id":518,"href":"http:\/\/kktg.net\/sgr\/wp-json\/wp\/v2\/pages\/517\/revisions\/518"}],"up":[{"embeddable":true,"href":"http:\/\/kktg.net\/sgr\/wp-json\/wp\/v2\/pages\/98"}],"wp:attachment":[{"href":"http:\/\/kktg.net\/sgr\/wp-json\/wp\/v2\/media?parent=517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}