Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Greek letters in ggplot strip text

Tags:

r

ggplot2

I'm trying to override the text in some ggplot strips to incorporate Greek characters. Here's some sample data, and the base for the plot.

dfr <- data.frame(
   x = rep(1:10, times = 6),
   y = runif(60),
   fx = rep(c("foo", "bar"), each = 30),
   fy = rep(c("alpha", "beta", "gamma"), each = 10, times = 2)
)

p <- ggplot(dfr, aes(x, y)) + geom_point()

My first attempt at a plot has no Greek in the strip labels.

 p + facet_grid(fy ~ fx)

I gather that I'm supposed to add a labeller argument to facet_grid to override the text. I presumed that this should spit out an expression to handle the greek characters, but my code just throws an error when the graphic is printed.

lbl <- function(variable, value)
{
   if(variable == "fy") parse(text=as.character(value)) else value
}
p + facet_grid(fy ~ fx, labeller = lbl)


Error in aperm(X, c(s.call, s.ans)) : 
  unimplemented type 'expression' in 'aperm'

How should I be creating the strip labels?

like image 879
Richie Cotton Avatar asked Mar 08 '10 17:03

Richie Cotton


People also ask

How to use Greek letters in ggplot?

Adding Greek symbols to Plot Title In this method to use Greeks symbols in ggplot2 user need to call the expression function which is a base function of the R programming language, and pass the name of the Greek symbols to be used as the parameters to this function to get a Greek symbol to the ggplot2.

How do I put Greek letters in R?

To make a Greek letter in R, You just use \ and then the name of the letter. If you want a subscript, like β1 , you use $\beta_1$ .


1 Answers

Try this:

p + facet_grid(fy ~ fx, labeller = label_parsed)
like image 172
Leo Alekseyev Avatar answered Oct 03 '22 20:10

Leo Alekseyev