Skip to content

Text Contrast in a Plot

5 messages · Rodrigo Aluizio, Mark Difford, Greg Snow +1 more

#
Hi Rodrigo,
There are quite a few approaches, but why not try legend?

Best, Mark.
Rodrigo Aluizio wrote:

  
    
#
Try this function as one approach:

shadowtext <- function(x, y=NULL, labels, col='white', bg='black',
	theta= seq(pi/4, 2*pi, length.out=8), r=0.1, ... ) {
	
	xy <- xy.coords(x,y)
	xo <- r*strwidth('A')
	yo <- r*strheight('A')

	for (i in theta) {
		text( xy$x + cos(i)*xo, xy$y + sin(i)*yo, labels, col=bg, ... )
	}
	text(xy$x, xy$y, labels, col=col, ... )
}

And here is an example of use:

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')

Hope this helps,
#
Thanks very much Greg and Mark for your attention.
The function that Greg created did the trick perfectly.
Once more thank you, I'll never be able to figure it out by myself, at least
not in time.
I'm still unable to write my own functions, but I'm working on it.

Rodrigo.

-----Mensagem original-----
De: Greg Snow [mailto:Greg.Snow at imail.org] 
Enviada em: sexta-feira, 24 de abril de 2009 18:23
Para: Rodrigo Aluizio; R Help
Assunto: RE: [R] Text Contrast in a Plot

Try this function as one approach:

shadowtext <- function(x, y=NULL, labels, col='white', bg='black',
	theta= seq(pi/4, 2*pi, length.out=8), r=0.1, ... ) {
	
	xy <- xy.coords(x,y)
	xo <- r*strwidth('A')
	yo <- r*strheight('A')

	for (i in theta) {
		text( xy$x + cos(i)*xo, xy$y + sin(i)*yo, labels, col=bg,
... )
	}
	text(xy$x, xy$y, labels, col=col, ... )
}

And here is an example of use:

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')

Hope this helps,
#
Rodrigo Aluizio wrote:
Hi Rodrigo,
Try boxed.labels in the plotrix package.

Jim