R Greek letters and normal letters for facet label in R facet grid

帅比萌擦擦* 提交于 2019-12-11 03:57:31

问题


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!


回答1:


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



来源:https://stackoverflow.com/questions/24579804/r-greek-letters-and-normal-letters-for-facet-label-in-r-facet-grid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!