Figure 13-4: R Relative Coordinates

Portfolio Categories: All Graphics and SGR Book Graphics.

R Graphics Relative Coordinates Plot

Placing Objects with Relative Coordinates in R Graphics


# Proportional coordinates

par()$usr                              # Show usr coords from previous plot

png(filename = "illustrations/fig-13-4-usr coords.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(1, 1, .25, .25))           # Plot margins - no title

plot(myX, myY, type = "n")             # Plot x & y, but suppress points
par(usr = c(0, 1, 0, 1))               # Set coordinates based on % of plot area
text(.5, .5, "Hello World!")           # Place text in middle of plot
polygon(x = c(.25, .75, .75, .25),     # Place box at 25% from edges
  y = c(.25, .25, .75, .75),
  lwd = 4)                             # Set the line width at 4

xtext2 = "x coordinates 0-1"           # Some text for adding to plot
ytext2 = "y coordinates 0-1"           # Some more text for plot    

arrows(x0 = .35, y0 = .8, x1 = 0,      # Add a horizontal arrow
  lwd = 2)                             # Set line width to 2
text(x = .5, y = .8, xtext2)           # Add coordinates text
arrows(x0 = .65, y0 = .8, x1 = 1,      # Add another horizontal arrow
  lwd = 2)                             # Set line width to 2

arrows(x0 = .2, y0 = .2, y1 = 0,       # Add a vertical arrow
  lwd = 2)                             # Set line width to 2
text(x = .2, y = .5, ytext2, srt = 90) # Add rotated y coordinates text
arrows(x0 = .2, y0 = .8, y1 = 1,       # Add another vertical arrow
  lwd = 2)                             # Set line width to 2

dev.off()                              # Output png file