将表达式添加到ggplot 2 2.0中的facet标签编辑器 [英] Adding expressions to facet labeller in ggplot 2 2.0

查看:106
本文介绍了将表达式添加到ggplot 2 2.0中的facet标签编辑器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用一个复杂的表达式替换一个多面包装标签.但是,我无法使用 facet_wrap_labeller 来使用在较旧的ggplot2版本下创建的图.

I need to replace one facet wrap label with a complicated expression. However, i cannot get the function facet_wrap_labeller to work anymore with plots created under older ggplot2 versions.

        data <- as.data.frame(matrix(rnorm(60),20,3)) 
        data$factor <- c(rep("f1", 4),rep("f2", 4), rep("f3", 4), rep("f4",4),rep("f5", 4))
        names(data) <- c("A", "B", "C", "factor")
        melted <- melt(data)

        p <- ggplot(melted, aes(variable, value)) +
             stat_boxplot(geom ='errorbar') + 
             geom_boxplot()
        p1 <- p + facet_wrap(~factor)
        facet_wrap_labeller(p1, labels=c("A", "B", expression({}^2*italic(D)~textstyle(group("(", rarefied, ")")))))

我得到:

Error in getGrob(gg[[strips[ii]]], "strip.text", grep = TRUE, global = TRUE) : 
  it is only valid to get a child from a "gTree"
Called from: getGrob(gg[[strips[ii]]], "strip.text", grep = TRUE, global = TRUE)

我相信这是由于ggplot2 2.0与该功能不兼容而引起的.

I believe this is happening because of ggplot2 2.0 not being compatible to this function.

我也尝试了labeller=labeller()labeller=bquote()参数,但是当我尝试使用此向量时

I also tried the labeller=labeller(), and labeller=bquote() arguments, but when i tried to use this vector

newlabels <- c(A="A", B="B",,
    D=expression({}^0*italic(D)~textstyle(group("(", rarefied, ")"))))

p1 <- p + facet_wrap(~factor, 
           labeller = labeller(factor=newlabels))

该表达式将被忽略,并绘制原始因子水平.使用bquote,我不知道在参数中添加多个名称.

the expression is ignored and the original factor levels are plotted. With bquote, i dont know to add more than one name to the argument.

感谢您的帮助.

推荐答案

在您的示例中,似乎只有5个方面的4个标签,但是您可以使用labeller=label_parsed并相当容易地重命名因子级别,而无需另一个功能.

There only appear to be 4 labels for 5 facets in your example, but you could use labeller=label_parsed and rename the factor levels fairly easily, avoiding the need for another function.

## Rename levels (I used data.table::melt, so they were characters)
melted$factor <- factor(melted$factor, 
                        levels=paste0('f', 1:5),
                        labels=c('A', 'B', 'C', '{}^0*italic(D)~plain((rarefied))', 'E'))

p <- ggplot(melted, aes(variable, value)) +
  stat_boxplot(geom ='errorbar') + 
  geom_boxplot()
p + facet_wrap(~factor, labeller=label_parsed)

这篇关于将表达式添加到ggplot 2 2.0中的facet标签编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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