Figure 15-2: Transparency

Portfolio Categories: All Graphics and SGR Book Graphics.

Using Color Transparency in R Graphics

Using Color Transparency in R Graphics


# 15.1b Color transparency =============================================== 15.1b

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

par(mar = c(0, 0, 0, 0))               # Set all margins to 0
plot.new()                             # New plot

abline(h = .5,                         # Add a horizontal line at .5
  lwd = 4)                             # Set line width to 4

segments(                              # Add segments w/ degrees of transparency
  x0 = seq(.1, .9, by = .15),          # The first x values for the lines
  y0 = .1,                             # The first y values for the lines
  x1 = seq(.1, .9, by = .15),          # The second x values
  y1 = .9,                             # The second y values
  col = c("#00000010", "#00000025",    # Black w/increasing opacity
    "#00000050", "#000000A5",
    "#000000D0", "#000000FF"),
  lwd = 40)                            # Line width = 40

text(                                  # Black labels for degree of opacity
  x = seq(.1, .9, by = .15),           # x values
  y = .1,                              # y value
  labels =                             # Text to show alpha channel values
    c("10", "25", "50", "A5", "D0", "FF"))    

text(                                  # White labels for degree of opacity
  x = seq(.1, .9, by = .15),           # x values
  y = .9,                              # y value
  col = "white",                       # Set color to white
  labels =                             # Text to show alpha channel values
    c("10", "25", "50", "A5", "D0", "FF"))    

dev.off()                              # Output png file