[R] Greek letter included in a character vector

David Winsemius dwinsemius at comcast.net
Sat Sep 11 02:20:33 CEST 2010


On Sep 10, 2010, at 7:58 PM, Judith Flores wrote:

> Hello,
>
>   In the past I have used "expression" to include greek letters in  
> axis labels,
> but this time I need to include the greek letter as part of a  
> legend. Basically,
> I need to create the following vector to rename the levels of a  
> factor:
>
> c("Interferon-gamma", "IL-10", "IL-5"), where "gamma" obviously  
> needs to be
> printed as the greek letter gamma. I have tried  
> expression("IFN-"*gramma), but
> it only works when it is isolated, not as part of a vector.
>

An example would have been nice. You should note that expressions are  
vectors of mode expression and that is what text and legend functions  
expect. The other thing I have learned is that quoting items  
unnecessarily inside expression() generally causes more harm than  
good. Learn to use plotmath syntax and separate items with "~" and  
"*". A comma (outside a quoted string) separates individual expression  
items.

x <- seq(-pi, pi, len = 65)
plot(x, sin(x), type = "l", ylim = c(-1.2, 1.8), col = 3, lty = 2)
points(x, cos(x), pch = 3, col = 4)
lines(x, tan(x), type = "b", lty = 1, pch = 4, col = 6)
title("legend(..., lty = c(2, -1, 1), pch = c(-1,3,4), merge = TRUE)",
       cex.main = 1.1)
legend(-1, 1.9, expression(Interferon-gamma, IL-10, IL-5), col =  
c(3,4,6),
        text.col = "green4", lty = c(2, -1, 1), pch = c(-1, 3, 4),
        merge = TRUE, bg = 'gray90')

Also look at plotmath-paste:

legend(-1, 1.9, expression(paste(Interferon,"-",gamma), IL-10, IL-5),  
col = c(3,4,6),
         text.col = "green4", lty = c(2, -1, 1), pch = c(-1, 3, 4),
         merge = TRUE, bg = 'gray90')

-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list