Learn R Programming

TeachingDemos (version 2.13)

shadowtext: Add text to a plot with a contrasting background.

Description

This is similar to the text function, but it also puts a background shadow (outline) behind the text to make it stand out from the background better.

Usage

shadowtext(x, y = NULL, labels, col = "white", bg = "black",
  theta = seq(pi/32, 2 * pi, length.out = 64), r = 0.1, 
  cex = 1, ...)

Value

This function is run for its side effects, returns NULL.

Arguments

x

x-coordinates for the text

y

y-coordinates for the text

labels

The text labels to plot

col

Color of the text

bg

Color of the background shadow

theta

Angles for plotting the background

r

Thickness of the shadow relative to plotting size

cex

Character expansion passed through to text and used in computing text size.

...

Additional arguments passed on to text

Author

Greg Snow, 538280@gmail.com, with improvements by Thomas Danhorn

Details

When adding text to a plot it is possible that the color of the text may make it difficult to see relative to its background. If the text spans different backgrounds then it may not be possible to find a single color to give proper contrast.

This function creates a contrasting shadow for the text by first plotting several copies of the text at angles theta and distance r in the background color, then plotting the text on top.

This gives a shadowing or outlining effect to the text making it easier to read on any background.

See Also

Examples

Run this code
 plot(1:10, 1:10, bg='aliceblue')
 rect(3,3,5,8, col='navy')
 text(5,6, 'Test 1', col='lightsteelblue')
 shadowtext(5,4, 'Test 2', col='lightsteelblue')

Run the code above in your browser using DataLab