R刻面网格中的R希腊字母和刻面标签的普通字母 [英] R Greek letters and normal letters for facet label in R facet grid

查看:162
本文介绍了R刻面网格中的R希腊字母和刻面标签的普通字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改构面标签,以便在y轴上具有希腊字母,在x轴上具有普通文本.测试数据为:

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()

到目前为止,我已经在许多不同的星座中尝试了以下方法,但我却变得绝望了: 1)要使用粘贴表达式更改数据框中的名称,我使用了label_parsed并没有太大的成功.

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)而且我尝试将贴标签机与

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)

         }
 }

这是对类似问题的先前答案的更改形式: 包含希腊符号的Facet标签

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

将感谢您的帮助!

推荐答案

我们如何创建各种标签交换器工厂.这是一个通用功能,可以重命名构面标签中的值级别

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)))

因此,该函数查看命名的参数,并期望有一个列表,其中列表的名称是因子的值,而列表中的值是您要替换它们的值.因此,仅对于变量"gamma",它将使用适当的表达式交换"0"和"1".然后它返回一个函数,我们可以将它作为labeller=参数传递给ggplot.因此,我们用

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()

这将导致

这篇关于R刻面网格中的R希腊字母和刻面标签的普通字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆