[R] Text Contrast in a Plot

Greg Snow Greg.Snow at imail.org
Fri Apr 24 23:23:25 CEST 2009


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,

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Rodrigo Aluizio
> Sent: Friday, April 24, 2009 8:12 AM
> To: R Help
> Subject: [R] Text Contrast in a Plot
> 
> Hi List,
> 
> I would appreciate any suggestion on how can I make a text I've
> inserted in a plot show some contrast? With this I mean that I have a
> white text on a plot and I would like to make a tiny border around it
> in black, so even being small sized and the entire graphic being small
> in the text page I'll insert it. It will be visible.
> 
> 
> 
> Well I tried inserting a box() function inside the text() function, R
> accepts the command but there is no effect. I even tried to plot a
> greater black text and then a smaller white one at the same place, but
> it doesn't look good.
> 
> 
> 
> Any ideas!
> 
> 
> 
> Bellow is my plot code without the above mentioned attempts. I'd like
> to add this effect at the second text() command.
> 
> 
> 
> png('BaseMap.png',width=5.2,height=3.6,pointsize=5,units='in',bg=NA,res
> toreC
> onsole=T,res=1200)
> 
> par(mar=c(5,0,0,0))
> 
> map('worldHires','brazil',ylim=c(-5.15,-4.55),xlim=c(-37,-
> 36.1),type='n')
> 
> rect(-37.1,-5.25,-36,-4.9,density=NULL,angle=45,col='#dbf4ff',border=F)
> 
> plot(Batimetria,ylab='',xlab='',border=F,col=Cor$Cor,add=T)
> 
> plot(Municipios,ylab='',xlab='',col=rgb(245,232,140,max=255,alpha=255),
> add=T
> ,lwd=0.2)
> 
> axis(1,xaxp=c(-37,-36.1,5),cex.axis=1.5)
> 
> axis(2,yaxp=c(-5.15,-4.55,5),cex.axis=1.5)
> 
> text(Prof$Long,Prof$Lat,rownames(Prof),col='black',cex=0.9,srt=-
> 24,font=2)
> 
> text(c(-36.96,-36.96,-36.865,-36.54,-36.37,-36.2),c(-4.96,-5.05,-5.11,-
> 5.14,
> -5.14,-5.14),c('Areia
> 
> Branca','Serra
> 
> do Mel','Porto do
> Mangue','Macau','Guamaré','Galinhos'),col='Black',cex=1.3,font=2)
> 
> rect(-36.20,-4.56,-36.155,-
> 4.565,density=NULL,angle=45,col='black',border='b
> lack',lwd=0.1)
> 
> rect(-36.155,-4.56,-36.111,-
> 4.565,density=NULL,angle=45,col='white',border='
> black',lwd=0.1)
> 
> text(c(-36.20,-36.155,-36.111,-36.215),c(-4.573,-4.573,-4.573,-
> 4.562),c('0',
> '5','10','km'),col='white',cex=1)
> 
> Norte(-36.155,-4.60,0.02)
> 
> box(which='plot',lty='solid')
> 
> dev.off()
> 
> 
> 
> Thanks in advance!
> 
> 
> 
> -------------------------------------------------------------
> 
> MSc.  <mailto:r.aluizio at gmail.com> Rodrigo Aluizio
> 
> Centro de Estudos do Mar/UFPR
> Laboratório de Micropaleontologia
> Avenida Beira Mar s/n - CEP 83255-000
> Pontal do Paraná - PR - Brasil
> 
> 
> 	[[alternative HTML version deleted]]




More information about the R-help mailing list