Facet labels involving a greek symbol

前端 未结 2 1358
野的像风
野的像风 2021-02-10 07:22

Suppose I have the data and plot as follows:

mydata = data.frame(x=rnorm(4), y=runif(4), tau=c(0,0,1,1))
ggplot(mydata) + geom_point(aes(x=x, y=y)) + facet_wrap(         


        
相关标签:
2条回答
  • 2021-02-10 07:51

    There is an easier solution to this. The simulated data variable names have been changed for clarity in how they work within the labeller argument.

    mydata = data.frame(x=rnorm(4), y=runif(4), tauvar=c(0,0,1,1),sigmavar=c(2,2,3,3))
    
    ggplot(mydata) + 
       geom_point(aes(x=x, y=y)) +
       facet_grid(sigmavar ~ tauvar,labeller = label_bquote(sigma==.(sigmavar),
                                                       tau==.(tauvar)))
    
    0 讨论(0)
  • 2021-02-10 08:06

    here a solution with facet_grid adn indexing the tau by its levels.

    mydata = data.frame(x=rnorm(4), y=runif(4), tau=c(0,0,1,1))
    ggplot(mydata) + geom_point(aes(x=x, y=y)) +
             facet_grid(~ tau,labeller = label_bquote(tau ^ .(x)))
    

    enter image description here

    Edit To get the "tau=0" and "tau=1"

    facet_grid(~ tau,labeller = label_bquote(tau == .(x)))
    

    Edit2 second variable sigma

    I find this solution, by defining a custom labeller. Hope someone ( ggplot2 guys ) give me a simpler solution.

    enter image description here

    my.label_bquote <- function (expr1 = (tau == .(x)),expr2 = (sigma == .(x))) 
    {
       quoted1<- substitute(expr1)
       quoted2 <- substitute(expr2)
       function(variable, value) {
          value <- as.character(value)
          if(variable == 'tau')
             lapply(value, function(x)
                   eval(substitute(bquote(expr1, list(x = x)),list(expr1 = quoted1))))
          else
             lapply(value, function(x) 
                   eval(substitute(bquote(expr2, list(x = x)),list(expr2 = quoted2))))
       }
    }
    
    mydata = data.frame(x=rnorm(4), y=runif(4), tau=c(0,0,1,1),sigma=c(2,2,3,3))
    ggplot(mydata) + geom_point(aes(x=x, y=y)) +
      facet_grid(sigma ~ tau,labeller = my.label_bquote())
    
    0 讨论(0)
提交回复
热议问题