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 [[alternative HTML version deleted]]
Hi Andrew, This is pretty clumsy but it seems to work. I suspect there are many better ways x <- seq(0, 1, length=21) plot(db) colour <- c("red", "blue") mytext <- c("RED", "BLUE") mtext( mytext, at= c(2,5), side=1, col=colour) --- Andrew Yee <andrewjyee at gmail.com> wrote:> 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 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, > reproducible code. >
Andrew Yee wrote:> 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()? >You can do something like this with "text" and then use xpd=TRUE to use it outside the plot. I think it would be more fiddly trying to use "mtext". concat.text<-function(x,y,txt,col) { thisx<-x for(txtstr in 1:length(txt)) { text(thisx,y,txt[txtstr],col=col[txtstr],adj=0) thisx<-thisx+strwidth(txt[txtstr]) } } plot(0,xlim=c(0,1),ylim=c(0,1),type="n") ctext<-c("Roses are ","red, ","violets are ","purple") concat.text(0,0.5,ctext,col=c("black","red","black","purple")) Jim
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
Seemingly Similar Threads
- alternatives to RColorBrewer?
- trouble understanding why ...=="NaN" isn't true
- looking for the na.omit equivalent for a matrix of characters
- more woes trying to convert a data.frame to a numerical matrix
- in cor.test, difference between exact=FALSE and exact=NULL