Skip to content

title: words in different colors?

4 messages · Michael Friendly, Barry Rowlingson, Duncan Murdoch

#
In ?title I see the

plot(cars, main = "")
 title(main = list("Stopping Distance versus Speed", cex=1.5, col="red", 
font=3))

I can't seem to generalize this to use several colors in a single title. 
What I'd like is
in latex-ish

\red{Hair color} \black{ and } \blue{Eye color}

to serve also as an implicit legend for points that are plotted.

-Michael
#
2009/1/21 Michael Friendly <friendly at yorku.ca>:
Solution from http://tolstoy.newcastle.edu.au/R/e2/help/07/09/24599.html
adapted for title:

  plot(1:10)
  title(expression("hair Color" * phantom(" and Eye color")),col.main="red")
  title(expression(phantom("hair Color and ") * "Eye color"),col.main="blue")
  title(expression(phantom("hair Color ") * "and" * phantom("Eye
color"),col.main="black"))

 The trick is to overlay three titles, one for each colour, with the
stuff not in that colour wrapped in a phantom() call to produce the
correct spacing in invisible ink.

  There's probably other ways...

Barry
#
Michael Friendly wrote:
I don't know a direct way, but you could put things together by using 
strwidth() on each piece, then plotting them at the appropriate position 
using mtext.  For example:

technicolorTitle <- function(words, colours, cex=1) {
    widths <- strwidth(words,cex=cex)
    spaces <- rep(strwidth(" ",cex=cex), length(widths)-1)
    middle <- mean(par("usr")[1:2])
    total <- sum(widths) + sum(spaces)
    start <- c(0,cumsum(widths[-length(widths)] + spaces))
    start <- start + middle - total/2
    mtext(words, 3, 1, at=start, adj=0, col=colours,cex=cex)
    }

plot(1)
technicolorTitle(c("Hair color", "and", "Eye color"), c("red", "black", 
"blue"))

(I didn't duplicate title()'s choice of position and font exactly, so 
you might want to tweak this a bit.)

Duncan Murdoch
#
Here's my version of the technicolor title function:

multiTitle <- function(...){
###
### multi-coloured title
###
### examples:
###  multiTitle(color="red","Traffic",
###             color="orange"," light ",
###             color="green","signal")
###
### - note triple backslashes needed for embedding quotes:
###
###  multiTitle(color="orange","Hello ",
###             color="red"," \\\"world\\\"!")
###
### Barry Rowlingson <b.rowlingson at lancaster.ac.uk>
###
  l = list(...)
  ic = names(l)=='color'
  colors = unique(unlist(l[ic]))

  for(i in colors){
    color=par()$col.main
    strings=c()
    for(il in 1:length(l)){
      p = l[[il]]
      if(ic[il]){ # if this is a color:
        if(p==i){  # if it's the current color
          current=TRUE
        }else{
          current=FALSE
        }
      }else{ # it's some text
        if(current){
          # set as text
          strings = c(strings,paste('"',p,'"',sep=""))
        }else{
          # set as phantom
          strings = c(strings,paste("phantom(\"",p,"\")",sep=""))
        }
      }
    } # next item
    ## now plot this color
    prod=paste(strings,collapse="*")
    express = paste("expression(",prod,")",sep="")
    e=eval(parse(text=express))
    title(e,col.main=i)
  } # next color
  return()
}