Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text labels with outline in R

Tags:

text

plot

r

I wonder whether it is possible to draw text with an outline in R, such that the text is readable independent of the background (like the text on memes). The following (obviously) fails:

# prepare a colorful background
randcolors <- sprintf( "#%02X%02X%02X99", sample(1:255, 1000, replace=T), sample(1:255, 1000,replace=T), sample(1:255, 1000, replace=T))
plot( NULL, xlim=c(0,1), ylim=c(0,1), xaxt="n", bty="n", yaxt="n")
points( runif(1000, 0, 1 ), runif( 1000, 0, 1 ), cex=runif(1000, 0.5, 5), col= randcolors, pch= 19)
text( 0.5, 0.5, "test text", cex= 5 )
text( 0.5, 0.5, "test text", cex= 4.5, col="white" )

The result is not spectacular:

enter image description here

Clearly, I can first create a white or semi-transparent background, but I would actually much prefer to have nice outlines.

like image 992
January Avatar asked Mar 27 '15 14:03

January


1 Answers

Try shadowtext from the TeachingDemos package:

 shadowtext( 0.5, 0.5, "test text", cex= 4.5, col="white" , r=0.3)

shadow text

like image 81
Spacedman Avatar answered Oct 06 '22 01:10

Spacedman