Search code examples
rggplot2labellatticefacet

How to insert Greek letters in lattice conditional labels strips?


I am producing a simple xyplot and I want to include Greek characters and mathematical equations on the conditional label strip/facet see below ("tau" and "cond").

I know that mathematical expressions and special characters can be added in lattice and ggplot2 such as here. I also know that with ggplot2 you can add a legend with facet_grid command (here).

I have not yet succeeded to make either the expression() command to work with lattice or to make it happen in any other way.

# Load packages
require(lattice)
require(gridExtra)
require(grid)

# Generate some values
x<-rnorm(100,10,4)
y<-rnorm(100,10,1)
cond1<-rbinom(100,1,0.5)
cond2<-rbinom(100,1,0.5)

groups<-sample(c(0:10),100,replace=TRUE)
dataa<-data.frame(y,x1,cond1,cond2,groups)
cond1<-factor(cond1,labels = c(expression(tau),"cond1"))
cond2<-factor(cond2,labels = c(expression(tau),"cond2"))
# ploting function
  xyplot(y~x|cond1*cond2,groups=groups,
         col = gray(seq(0.01,0.7,length=length(levels(as.factor(groups))))),
         pch = 1:length(levels(as.factor(groups))),
         key = NULL)]

I want to be able to change "tau", "cond1" and "cond2" into Greek letters


Solution

  • Instead of expression, you can just use the unicode for "tau":

    # Generate some values
    x<-rnorm(100,10,4)
    y<-rnorm(100,10,1)
    cond1<-rbinom(100,1,0.5)
    cond2<-rbinom(100,1,0.5)
    
    groups<-sample(c(0:10),100,replace=TRUE)
    dataa<-data.frame(y,x,cond1,cond2,groups)
    cond1<-factor(cond1,labels = c("\u03C4","cond1"))
    cond2<-factor(cond2,labels = c("\u03C4","cond2"))
    
    # ploting function
    xyplot(y~x|cond1*cond2,groups=groups,
           col = gray(seq(0.01,0.7,length=length(levels(as.factor(groups))))),
           pch = 1:length(levels(as.factor(groups))),
           key = NULL)
    

    Output:

    > levels(cond1)
    [1] "τ"     "cond1"
    

    enter image description here