[R] multiple colors within same line of text
David Dailey
Lists at CompassRoseEnterprises.com
Fri Aug 31 19:40:45 CEST 2007
Andrew Yee <andrewjyee <at> gmail.com> writes:
>
> Hi, I'm interested in using mtext(), but with the option of having multiple
> colors in the same line of text.
>
> For example, creating a line of text where:
>
> Red is red and blue is blue
>
> How do you create a text argument that lets you do this within mtext()?
>
> Thanks,
> Andrew
> MGH Cancer Center
>
I see this thread has languished for a couple of weeks-- I'm behind in reading
my digests but the responses I see rely on the user figuring out how wide the
text is. I don't think we need to do that.
Instead, try using multiple calls to mtext(), passing slightly different
expression() calls as the text argument. Make use of the phantom() "function"
of plotmath (see ?plotmath).
Example, which you can modify to make it look prettier:
plot( 1:10)
mtext( expression( phantom( "Red" ) * " is red and " *
phantom( "Blue" ) * " is blue" ), side=3 )
mtext( expression( "Red" * phantom(" is red and Blue is blue" ) ),
side=3, col='red' )
mtext( expression( phantom( "Red is red and " ) * "Blue" *
phantom( " is blue" ) ), side=3, col='blue' )
It might be a fun exercise to write a function (call it multicolorText?) that
would accept an argument such as "<col='red'>Red</col> is red and
<col='blue'>Blue</col> is blue", then do the same thing as the three mtext calls
above...
--David Dailey
Shoreline, Washington, USA
More information about the R-help
mailing list