2014-09-02 11 views
7

Voglio tracciare un'etichetta su una linea in un grafico monocromatico. Quindi ho bisogno di un piccolo bordo bianco su ogni lettera dell'etichetta.R: C'è un modo per mettere bordi, ombre o buffer attorno alle etichette di testo en R plot?

Il bordo o lo sfondo del rettangolo dell'etichetta di testo non è utile perché nasconde un sacco di dati tracciati.

C'è un modo per mettere bordi, ombre o buffer attorno alle etichette di testo en R plot?

EDIT:

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('x') 
    yo <- r*strheight('x') 

    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, ...) 
} 

pdf(file="test.pdf", width=2, height=2); par(mar=c(0,0,0,0)+.1) 
    plot(c(0,1), c(0,1), type="l", lwd=20, axes=FALSE, xlab="", ylab="") 
    text(1/6, 1/6, "Test 1") 
    text(2/6, 2/6, "Test 2", col="white") 
    shadowtext(3/6, 3/6, "Test 3") 
    shadowtext(4/6, 4/6, "Test 4", col="black", bg="white") 
    shadowtext(5/6, 5/6, "Test 5", col="black", bg="white", theta = seq(pi/4, 2*pi, length.out=24)) 
dev.off() 

Il codice sopra usa la soluzione da koekenbakker. Questo va bene per la grafica PNG.

Ho bisogno di un approccio diverso per alta risoluzione o vettoriale PDF come questo https://docs.google.com/file/d/0BwpJogoN4kzHUEkwMEI2Rzg2T0k/edit?pli=1

+1

Ci sono molte funzioni di tracciato diversi e sistemi grafici a R. Si prega di essere molto più preciso su cosa esattamente tramando comandi che si sta utilizzando. Ancora meglio, includi un [esempio riproducibile] (http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – MrFlick

+0

Qual è il problema con il pdf? Intendi la risoluzione? È possibile adattare il parametro theta per una risoluzione maggiore. Lo cambierò nella mia risposta. – koekenbakker

+0

hai ragione. Nel mio esempio ho provato un theta di 24 articoli e non era abbastanza. Ma nel tuo esempio funziona perfettamente con 50. Grazie – fnd

risposta

13

Si può provare questa funzione 'SHADOWTEXT' che disegna un alone o un bordo intorno al testo stampandolo più volte con un offset in una leggera colore diverso Tutti i crediti per Greg Snow here.

shadowtext <- function(x, y=NULL, labels, col='white', bg='black', 
         theta= seq(0, 2*pi, length.out=50), r=0.1, ...) { 

    xy <- xy.coords(x,y) 
    xo <- r*strwidth('A') 
    yo <- r*strheight('A') 

    # draw background text with small shift in x and y in background colour 
    for (i in theta) { 
     text(xy$x + cos(i)*xo, xy$y + sin(i)*yo, labels, col=bg, ...) 
    } 
    # draw actual text in exact xy position in foreground colour 
    text(xy$x, xy$y, labels, col=col, ...) 
} 

# And here is an example of use: 
# pdf(file="test2.pdf", width=2, height=2); par(mar=c(0,0,0,0)+.1) 
plot(c(0,1), c(0,1), type="n", lwd=20, axes=FALSE, xlab="", ylab="") 

rect(xleft = 0.5, xright = 1, ybottom = 0, ytop = 1, col=1) 
text(1/6, 1/6, "Test 1") 
shadowtext(2/6, 2/6, "Test 2", col='red', bg="blue") 
shadowtext(3/6, 3/6, "Test 3", cex=2) 

# `r` controls the width of the border 
shadowtext(5/6, 5/6, "Test 4", col="black", bg="white", cex=4, r=0.2) 
# dev.off() 

enter image description here

1

ho scritto una funzione simile per i campi di testo, che funziona anche su scale logaritmiche.

install.packages("berryFunctions") 
library("berryFunctions") 
?textField 

Questo può essere considerato migliore per la grafica vettoriale. ecco alcuni esempi:

enter image description here

PS: Se vuoi contribuire: https://github.com/brry/berryFunctions

1

Ecco il mio tentativo di aggiungere un buffer di testo per l'etichettatura contorni (o cerchi in questo esempio). Non è sicuramente così efficace o carino come la soluzione di Koekenbakker, ma è servito allo scopo di isolare l'etichetta di testo dalla linea.

require(shape) 
plot(250,250, xlim=c(0,500), ylim=c(0,500), axes=F, ylab="", xlab="", col="black") ## set up the plotting area 

circle_radius<-c(200, 150, 50) ## we wan't to draw a few circles which we will label after using our buffer 

for (i in 1:length(circle_radius)){ 
    plotcircle(mid=c(250,250), r=circle_radius[i], lwd=0.5,lcol="grey40") ## iteratively plot the circles 

text_buffer<-seq(0.1, 0.7, by=0.01) ## this is the key to the text buffer. Create a vector of values to set the character size (cex) during the following loop 

for(j in text_buffer){ 
    text(x=250+circle_radius[i], 250, print(paste(circle_radius[i],"m")), cex=j, srt=270, col="white") ## write the text buffer in white starting from the smallest value of cex to the largest 
} ## end of loop for writing buffer 
    text(x=250+circle_radius[i], 250, print(paste(circle_radius[i],"m")), cex=0.5, srt=270) ## write over the buffer with black text 

} ## end of loop for drawing circles 

Plot of circles with text buffers

7

avevo bisogno di fare questo per una mappa in R e finito per usare il pacchetto "raster" per disegnare aloni intorno alle etichette di testo.

http://rpackages.ianhowson.com/cran/raster/man/text.html

esempio,

text(Points, labels = Points$Labels, halo = TRUE, hw = 0.08, hc = "white", cex = 0.8) 
# hw = halo width 
# hc = halo color 

enter image description here

+0

Questa è la risposta che le persone stanno cercando. – Cyrille

Problemi correlati