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 -- Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology Dept. York University Voice: 416 736-5115 x66249 Fax: 416 736-5814 4700 Keele Street http://www.math.yorku.ca/SCS/friendly.html Toronto, ONT M3J 1P3 CANADA
2009/1/21 Michael Friendly <friendly at yorku.ca>:> 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.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:> 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. > >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