guyabel
guyabel

Reputation: 8366

Reduce the spacing between letters in the text function

I am trying to find a way to reduce the space between letters in a plot when i use the text function...

par(mar=rep(0,4))
plot.new()
text(0.5,0.8,"text", cex=8)

I want to have almost no space between each letters, or the option for overlapping (as in the bottom of the plot below...I did this by hand in MS paint).

text

Upvotes: 2

Views: 1741

Answers (2)

Greg Snow
Greg Snow

Reputation: 49640

Here is a start to a function to do this:

   squishtext <- function(x,y, text, squish=1) {
        text <- strsplit(text, '')[[1]]
        w <- strwidth(text)
        ww <- cumsum(c(0,head(w,-1)) * squish)
        text( x + ww, y, text, adj=c(0,0) )
    }

and a quick example/check:

    plot(1:10, type='n')
    text( 5, 3, "test", adj=c(0,0) )
    squishtext( 5, 4, "test", squish=1 )
    squishtext( 5, 5, "test", squish=0.8 )
    squishtext( 5, 6, "test", squish=0.5 )
    squishtext( 5, 7, "test", squish=1.2 )
    squishtext( 5, 8, "test", squish=2 )

It could be expanded to take additional parameters (adj, cex, etc.).

Upvotes: 4

user2005253
user2005253

Reputation:

One option:

par(mar=rep(0,4))
plot.new()

word = "text"
letters = strsplit(word,"")

xstart = .4
ystart = .8
space = .075

for(i in 1:length(letters[[1]])){

    text(xstart,ystart,letters[[1]][i], cex=8)
    xstart = xstart + space

}

Although me personally, I would do it manually one letter at a time like this:

par(mar=rep(0,4))
plot.new()

text(.5,.8,"t", cex=8)
text(.57,.8,"e", cex=8)
text(.645,.8,"x", cex=8)
text(.7,.8,"t", cex=8)

Upvotes: 2

Related Questions