[R] Elements of a character vector in different colours?

jasont@indigoindustrial.co.nz jasont at indigoindustrial.co.nz
Thu Jan 9 22:53:03 CET 2003

> is it possible to format elements of a character vector in different colors
> and how would this be achieved?

On graphs, with the text() command.  Colour isn't an inherient property
of a character vector.


> outcome <- c("positive","negative","neutral")
> forecast <- 60
> int <- c(55,65)
> #
> text <- outcome[3]
> text[forecast > 50 & int[1] > 50] <- outcome[1]
> text[forecast < 50 & int[2] < 50] <- outcome[2]
> text

By the way, it's a good idea to check if your variable names conflict 
with function names - after you've done the above, try 

> conflicts(text)

> I tried the following
> outcome <- c("positive","negative","neutral")[col=c("green","red","black"]
> without success, i.e. three times "na" formatted in green is returned.

Nope.  A syntax error is returned.  Cut-and-paste error?  ;)

I think you'll find the word "green" in the above, and the colour green
in the NA printout is a coincidence.  Try for yourself, and use "purple", 
or "volvo", or "fish" instead of "green".

Looks like you're a bit shaky on R's vector indexing syntax, and what 
that means.

Text in R is just a vector of characters - there is no formatting, font,
colour, weight, or hyperlink information stored in it.  If you want to supply
colour information, you'll have to provide it some other way.  I've never
used R2HTML, so I can't help you there.

Hope that clears a few things up.


More information about the R-help mailing list