R Tips



Steven Holland

Plots with semi-transparent symbols

1 December 2009

When plots have many densely spaced points, it can be difficult to determine how many points are present where points overlap. To solve this problem, you can make data points semi-transparent by specifying their color with the rgb() function. The alpha parameter is what sets the the opacity, with 1 indicating an opaque color and 0 indicating a transparent color. By default, all four parameters range from 0.0 to 1.0. Here's an example of how to use rgb() and what the result is.

> x <- rnorm(5000)
> y <- rnorm(5000)
> plot(x,y, pch=16, col=rgb(red=0.2, green=0.2, blue=1.0, alpha=0.2))

transparent plotting symbols

The rgb() function can be called wherever you need to specify a color. If you’re trying to match a certain color, you may want to first use the color pickers that are built into OS X or Windows. For example, the color picker for OS X, lets you set your red, green, and blue values to get the desired color.

OS X color picker

Color pickers like this often specify the red, green, and blue values on a 0–255 scale. To use this scale in the rgb() function, set the parameter maxColorValue = 255.

> plot(x,y, pch=16, col=rgb(red=0.2, green=0.2, blue=1.0, alpha=0.2, maxColorValue = 255))