r - How to add expressions to labels in facet_wrap? -


this question has answer here:

i have dataset plot small multiples, in 2-by-2 array, 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, want each facet label include expression, such as

expression(paste("a or ", alpha)) 

i can make 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 lose 2-by-2 array. how can rename facet_wrap() facet labels expression? or, how can solve recreating 2-by-2 array using facet_grid(), faceting single variable?

(this question builds off of parenthetical note in @baptiste's answer this previous question.)

thanks!

in order asked, first load labeller function @roland first appearing here:

facet_wrap_labeller <- function(gg.plot,labels=null) {   #works 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(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 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() , feed expression labels argument:

facet_wrap_labeller(myplot, labels = c(expression(paste("a or ", alpha)), expression(beta), expression(gamma), expression(delta))) 

the expressions should appear facet_wrap() labels.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -