Figure 14-13: Positioning Text

Portfolio Categories: All Graphics and SGR Book Graphics.

fig-14-13-text


# 14.7 Ad hoc text placement ============================================== 14.7

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

par(mai = c(.5, .5, .25, .25))         # Set plot margins - no title

plot(x = 1.25, y = 1.5,                # Start plot with first point
  xlim = c(0, 3),                      # Set x range of plot
  ylim = c(0, 2),                      # Set y range of plot
  pch = 18,                            # Diamond shaped plotting symbol
  xlab = NA,                           # Turn off x axis label
  ylab = NA)                           # Turn off y axis label

points(1.5, 1, pch = 18)               # Add diamond shaped point
text(1.5, 1,                           # Add text at same position
  labels = "Defaults: No pos= or adj=")  

# Using the adj= option
points(.5, .25, pch = 18)              # Add diamond shaped point
text(.5, .25,                          # Add text at same point
  labels = "adj = c(1,1)",             # Text to add
  adj = c(1, 1))                       # Adjust position to lower left

text(.5, .25,                          # Add more text at same point
  labels = "adj = c(0,0)",             # Text to add
  adj = c(0, 0))                       # Adjust position to upper right

points(.5, .75, pch = 18)              # Add diamond shaped point
text(.5, .75,                          # Add text at same point
  labels = "adj = c(1,0)",             # Text to add
  adj = c(1, 0))                       # Adjust position to lower right

text(.5, .75,                          # Add text at same point
  labels = "adj = c(0,1)",             # Text to add
  adj = c(0, 1))                       # Adjust position to upper left

points(.5, 1.75, pch = 18)             # Add diamond shaped point
text(.5, 1.75,                         # Add text at same point
  labels = "adj = c(.5,.5) \n srt = 90",   
  srt = 90,                            # Rotate text 90 degrees
  adj = c(.5, .5))                     # Adjust position to center text
                                       #   (note this is also the default)
# Using the pos =  option 
points(1.25, 1.5, pch = 18)            # Add diamond shaped point
text(1.25, 1.5,                        # Add text at same point
  labels = "pos = 1",                  # Text to add
  pos = 1)                             # Set position to bottom centered (1)

text(1.25, 1.5,                        # Add text at same point
  labels = "pos = 2",                  # Text to add
  pos = 2)                             # Set position to left (2)

text(1.25, 1.5,                        # Add text at same point
  labels = "pos = 3",                  # Text to add
  pos = 3)                             # Set position to top centered (3)

text(1.25, 1.5,                        # Add text at same point
  labels = "pos = 4",                  # Text to add
  pos = 4)                             # Set position to right (4)

# Using xpd =  to go outside the plot area
text(.45, 0,                           # Add text at bottom of plot
  labels = "This uses xpd = TRUE to allow it to go outside the plot area",
  pos = 4,                             # Place text to right of point
  xpd = TRUE,                          # Set xpd = TRUE to allow
  font = 3)                            # Set font to italic

# plotmath expression
text(2.5, .75,                         # Add an expression w/plotmath
  labels = expression(Phi(italic(x))   # Build the expression
    == over(1, sigma * sqrt(2 * pi)) *
    italic(e)^ -over((x - mu)^2, 2 * sigma^2)),
  srt = 45,                            # Set at 45 degree angle
  cex = 1.25)                          # Increase size to 1.25

text(2.5, 1.75,                        # Add text for expression detail
  labels = "plotmath equation with \n srt = 45")

arrows(x0 = 2.5, x1 = 2.5,             # Add an arrow
  y0 = 1.5, y1 = 1.1,                  # y coordinates for arrow
  lwd = 3.5,                           # Set linewidth to 3.5
  col = "darkgray")                    # Set color to dark gray

dev.off()                              # Output png file