如何将表达式添加到facet_wrap中的标签? [英] How to add expressions to labels in facet_wrap?
问题描述
我有一个数据集,我想绘制小的倍数,特别是在一个2×2的数组中,像这样:
<$ (c(A,B,C,D),每个= 20)),x = rnorm(80), y = rnorm(80))
ggplot(mydf,aes(x = x,y = y))+ geom_smooth(method =lm)+ geom_point()+ facet_wrap(〜letter,ncol = 2)
然而,我希望每个facet标签都包含一个表达式,例如
表达式(paste(A或,alpha))
我可以通过 facet_grid()
通过
<$ c'=>表达式(paste(A或,alpha)),'B'=表达式(paste(B或,beta)),'C'=表达式粘贴(C或,伽玛)),'D'=表达式(粘贴(D或,delta)))
f_labeller< - function(variable,value){return(f_names [value]) }
ggplot(mydf,aes(x = x,y = y))+ geom_smooth(method =lm)+ geom_point()+ face t_grid(〜letter,labeller = f_labeller)
但是我失去了2×2的数组。我如何用表达式重命名 facet_wrap()
facet标签?或者,我怎么能通过使用 facet_grid()
重新创建2×2数组来解决这个问题,但只能由一个变量来分割?
(这个问题建立在@ baptiste对 谢谢!
facet_wrap_labeller< - 函数(gg.plot,labels = NULL){
#R和R 3.0.1和ggplot2 0.9.3.1
require(gridExtra)
g< ; - ggplotGrob(gg.plot)
gg< - g $ grobs
strips< - grep(strip_t,names(gg))
for(ii in seq_along(标签)){
mo dgrob< - getGrob(gg [[strip [ii]],strip.text,
grep = TRUE,global = TRUE)
gg [[strips [ii]]] $ children [ (modgrob $ name)]< - editGrob(modgrob,label = labels [ii])
}
g $ grobs< - gg
class(g)= c ggplot,class(g))
g
}
然后保存原始 ggplot()
对象:
myplot< - ggplot(mydf,aes(x = x,y = y))+ geom_smooth(method =lm)+ geom_point()+ facet_wrap(〜letter,ncol = 2)
然后调用 facet_wrap_labeller()
并将表达式标签作为参数提供:
表达式现在应该显示为 facet_wrap )
标签。
I have a dataset from which I would like to plot small multiples, specifically in a 2-by-2 array, like this:
mydf <- data.frame(letter = factor(rep(c("A", "B", "C", "D"), each = 20)), x = rnorm(80), y = rnorm(80))
ggplot(mydf, aes(x = x, y = y)) + geom_smooth(method = "lm") + geom_point() + facet_wrap(~ letter, ncol = 2)
However, I want each facet label to include an expression, such as
expression(paste("A or ", alpha))
I can make this happen using facet_grid()
via
f_names <- list('A' = expression(paste("A or ", alpha)), 'B' = expression(paste("B or ", beta)), 'C' = expression(paste("C or ", gamma)), 'D' = expression(paste("D or ", delta)))
f_labeller <- function(variable, value){return(f_names[value])}
ggplot(mydf, aes(x = x, y = y)) + geom_smooth(method = "lm") + geom_point() + facet_grid(~ letter, labeller = f_labeller)
But then I lose the 2-by-2 array. How can I rename the facet_wrap()
facet labels with an expression? Or, how can I solve this by recreating the 2-by-2 array using facet_grid()
, but only faceting by a single variable?
(This question builds off of the parenthetical note in @baptiste's answer to this previous question.)
Thanks!
In order to do what I asked, first load this labeller function from @Roland first appearing here:
facet_wrap_labeller <- function(gg.plot,labels=NULL) {
#works with R 3.0.1 and ggplot2 0.9.3.1
require(gridExtra)
g <- ggplotGrob(gg.plot)
gg <- g$grobs
strips <- grep("strip_t", names(gg))
for(ii in seq_along(labels)) {
modgrob <- getGrob(gg[[strips[ii]]], "strip.text",
grep=TRUE, global=TRUE)
gg[[strips[ii]]]$children[[modgrob$name]] <- editGrob(modgrob,label=labels[ii])
}
g$grobs <- gg
class(g) = c("arrange", "ggplot",class(g))
g
}
Then save the original ggplot()
object:
myplot <- ggplot(mydf, aes(x = x, y = y)) + geom_smooth(method = "lm") + geom_point() + facet_wrap(~ letter, ncol = 2)
Then call facet_wrap_labeller()
and feed the expression labels as an argument:
facet_wrap_labeller(myplot, labels = c(expression(paste("A or ", alpha)), expression(beta), expression(gamma), expression(delta)))
The expressions should now appear as the facet_wrap()
labels.
这篇关于如何将表达式添加到facet_wrap中的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!