Figure 15-1c: A Color Plot

Portfolio Categories: All Graphics and SGR Color Graphics.

A Plot with Colors

A Plot with Colors


# A plot of normal deviates with colors ----------------------------------------

myV1 = seq(0, 1, by = .001)            # Set up some x values
myV2 = rnorm(myV1)                     # Y from random normal dist

# Here we will create a vector of integer values sorting myV2 which ranges
# between -4 and +4 into 8 values between 1 and 8.  We'll then use these
# to select colors from a palette of 8 colors

myV2colors = abs(round(2 * myV2, 0)) + 1    

# Here is the color version
png(filename = "illustrations/fig-15-1-colors.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(bg = gray(.9))                     # Set plot background to light gray
plot(myV1, myV2, type = "n",           # Setup plot without points
  ylim = c(-4, 4),                     # Set y range
  main =                               # Set title and subtitle
    "A color plot (in black and white)",  
  sub = "(Color version available online at www.sagepub.com/gaubatz)",
  col.lab = "red",                     # Color for axis labels
  col.axis = "blue",                   # Color for axis and ticks
  col.main = "darkblue")               # Color for main title
                      
rect(                                  # Create rectangle w/plot area coords
  par()$usr[1],                        # left x value at usr[1]
	par()$usr[3],                        # Right x value at usr[3]
	par()$usr[2],                        # Bottom y value at usr[2]
	par()$usr[4],                        # Top y value at usr[4]
  col = gray(.5),                      # Set rect color to medium gray
  lwd = 8,                             # Border with line width 8
  border = "red")                      # Set border color to red

myPalette = rainbow(8)                 # Create a palette of 8 colors 

points(myV1, myV2,                     # Add points to plot
  pch = 16,                            # Use solid circle
  cex = .5,                            # Set the points at half size
  col = myPalette[myV2colors])         # Set color based on myV2 value

dev.off()                              # Output png file