7

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.

January
  • 16,320
  • 6
  • 52
  • 74
  • this link may help: http://stackoverflow.com/questions/7734535/control-font-thickness-without-changing-font-size – Ruthger Righart Mar 27 '15 at 15:28
  • Some possibilities for `ggplot` [here](http://stackoverflow.com/questions/10686054/outlined-text-with-ggplot2/10691826#10691826). – Henrik Mar 27 '15 at 19:39

1 Answers1

8

Try shadowtext from the TeachingDemos package:

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

shadow text

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • The `shadowtext` function is interesting because it's basically the OP's intuition, [just repeated a bunch of times](https://github.com/cran/TeachingDemos/blob/master/R/shadowtext.R). – Thomas Mar 27 '15 at 18:25
  • Not really, the OP is scaling the text size, but shadowtext uses multiple copies at the same size as the main text. Scaling doesn't work to create shadows on internal holes. – Spacedman Mar 27 '15 at 20:58
  • Is there any similar function that can be used in `ggplot2`? – ikashnitsky Oct 07 '15 at 03:47
  • @Ilya see Henrik's link in the comment on the question. – Spacedman Oct 07 '15 at 06:49