2

I would like to change the facet labels, such that I have greek letters on the y axis and normal text on the x axis. The testing data is:

testdata<-data.frame(Method2=c("a","a","b","b"),
 gamma=c(0,1,0,1),values=c(1,2,3,4),x=rep(1,4),y=rep(1,4))

testplot2<-ggplot(data=testdata,aes(x=Method2,y=gamma))
testplot2<-testplot2+facet_grid(gamma~Method2 )
testplot2+geom_point()

So far I have tried the following in many differnt constellations and I'm getting rather desperate: 1) To change the names in the data frame with the paste expression and I used label_parsed without much success.

gamma<- factor(sapply(testdata$gamma, function(n){
 if(n %in% c("0")) paste(expression(gamma),"0") else
 if(n %in% c("1")) paste(expression(gamma),"1") 
}), levels=c(paste(expression(gamma),"0"),
         paste(expression(gamma),"1")
))
testdata$gamma <- gamma

2) And I have tried to use a labeller with

my.label_bquote <- function (expr1 = (gamma == .(x)),expr2 = x) 
 {
     quoted1<- substitute(expr1)

    function(variable, value) {
         value <- as.character(value)
         browser()
         if(variable == gamma)
             lapply(value, function(x) eval(substitute(bquote(expr1, list(x = x)),list(expr1 = quoted1))))

        else if(variable == Method2){

           value[value=="a"]   <- "Whatever"
           value[value=="b"]   <- "Whatever2"

         }
         return(value)

         }
 }

which is a changed form of a previous answer given to a similar question: Facet labels involving a greek symbol

Would be grateful for any help!

Community
  • 1
  • 1

1 Answers1

3

How about we create a label swapper factory of sorts. Here's a generic function that will allow for renaming of levels of values in facet labels

get_label_swap <- function(...) {
    dots<-list(...)
    function(variable, value) {
        if(variable %in% names(dots)) {
            swaps <- dots[[variable]]
            vals <- as.character(value)
            lapply(vals, function(v) {if(v %in% names(swaps)) swaps[[v]] else v })
        } else {
            label_value(variable, value)
        }
    }
}

Then, to get one specific to your problem, we would do

label_swap <- get_label_swap(gamma=list("0"=expression(gamma*0), 
                                        "1"=expression(gamma*1)))

So the function looks at the named parameters and expects a list where the names of the list are the values of the factor, and the values in the list are what you want to replace them with. So only for the variable "gamma" will it swap "0" and "1" with the appropriate expressions. Then it returns a function we can pass as a labeller= parameter to ggplot. Thus we plot with

testplot2 <- ggplot(data=testdata,aes(x=Method2,y=gamma))
testplot2 <- testplot2+facet_grid(gamma~Method2, labeller=label_swap)
testplot2 + geom_point()

which results in

enter image description here

The newest version of ggplot no longer supports these types of labellers, here's a possible alternative solution using the new labeller_bquote function

ggplot(data=testdata,aes(x=Method2,y=gamma)) + 
  geom_point() + 
  facet_grid(gamma~Method2, labeller=label_bquote(rows=gamma*.(gamma)))
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Fantastic, thank you so much! I had a bit of a fuss with getting the expression right, as I wanted "gamma=1", which has been another problem, which I had in the first example. So if anybody is interested in this – user3805780 Jul 06 '14 at 19:05
  • If you want `gamma=0/1` in the facet label, just use `get_label_swap(gamma=list("0"=expression(gamma==0), "1"=expression(gamma==1)))` – MrFlick Jul 06 '14 at 19:07
  • Sorry I could not finish my comment and you were faster. I circumvented the problem with writing "expression(gamma*"=0.001")". Anyway thank you so much, this is fantastic! :) You made my day. – user3805780 Jul 06 '14 at 19:10
  • Doesn't work with ggplot2 version 3.3.2, but was close. So I would appreciate if anyone could produce an update on this great solution. – Clem Snide Oct 02 '20 at 11:58
  • @ClemSnide Things have changed a lot I guess. I updated with a new solution using `label_bquote` that works for this particular case but it's a different approach. – MrFlick Oct 03 '20 at 02:49