[R] coloured letters in a text

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Aug 28 15:31:32 CEST 2008


On Thu, 28 Aug 2008, Barry Rowlingson wrote:

> 2008/8/28 Fränzi Korner <fraenzi.korner at oikostat.ch>:
>
>> example 1:
>> I would like to add the word "ABC" to a figure. Thereby each letter should
>> have a different colour.
>>
>> text(x,y,"ABC", col=c(1,2,3))  # this does not work
>>
>
> kludge alert!
>
> How about:
> text(x,y,"ABC",col=3)
> text(x,y,"AB",col=2)
> text(x,y,"A",col=1)

You need adj=0 for left justification.

> Basically this overlays three coloured texts to give the impression
> of individual coloured letters. This may or may not be practical for
> your real application. It's also possible that if you print this your
> printer will end up printing "A" with a mix of colours 1, 2 and 3.
>
> If your font is monospaced you could do it with text(x,y,"
> C",col=3), but in a pretty font the letters AB wont have the same
> width as two spaces.

What's wrong with using strwidth?

> x <- 3
> text(x, 5, "A", adj=0, col=1)
> x <- x + strwidth("A")
> text(x, 5, "B", adj=0, col=2)
> x <- x + strwidth("B")
> text(x, 5, "C", adj=0, col=3)

This will not use kerning, but only a few devices do.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


More information about the R-help mailing list