在ggplot2中将facet标签更改为math公式

我不知道如何将ggplot2facet标签更改为math公式。

 d <- ggplot(diamonds, aes(carat, price, fill = ..density..)) + xlim(0, 2) + stat_binhex(na.rm = TRUE) + opts(aspect.ratio = 1) d + facet_wrap(~ color, ncol = 4) 

在这里输入图像描述

例如,我想将facet标签从D更改为Y[1] ,其中1是下标。 在此先感谢您的帮助。

我find了这个答案,但它不适合我。 我使用R 2.15.1ggplot2 0.9.1

也许有人在某个时候改变了编辑Grob函数的名字。 (编辑:它大约8个月前被@hadley删除。)没有geditGrob但只是从pkg editGrob :网格似乎工作:

  d <- ggplot(diamonds, aes(carat, price, fill = ..density..)) + xlim(0, 2) + stat_binhex(na.rm = TRUE) + opts(aspect.ratio = 1) #Note: changes in ggplot2 functions cause this to fail from the very beginning now. # Frank Harrell's answer this year suggests `facet_warp` now accepts `labeller` d <- d + facet_wrap(~ color, ncol = 4) grob <- ggplotGrob(d) strip_elem <- grid.ls(getGrob(grob, "strip.text.x", grep=TRUE, global=TRUE))$name #strip.text.x.text.1535 #strip.text.x.text.1541 #strip.text.x.text.1547 #strip.text.x.text.1553 #strip.text.x.text.1559 #strip.text.x.text.1565 #strip.text.x.text.1571 grob <- editGrob(grob, strip_elem[1], label=expression(Y[1])) grid.draw(grob) 

你可以编辑gtable中的grobs,

 ggplot(diamonds, aes(carat, price, fill = ..density..)) + xlim(0, 2) + stat_binhex(na.rm = TRUE) + facet_wrap(~ color, ncol = 4) for(ii in 1:7) grid.gedit(gPath(paste0("strip_t-", ii), "strip.text"), grep=TRUE, label=bquote(gamma[.(ii)])) 

在这里输入图像描述

或者,如果你想保存一个grob,

 g <- ggplotGrob(d) gg <- g$grobs strips <- grep("strip_t", names(gg)) for(ii in strips) gg[[ii]] <- editGrob(getGrob(gg[[ii]], "strip.text", grep=TRUE, global=TRUE), label=bquote(gamma[.(ii)])) g$grobs <- gg 

使用ggsave会需要额外的(丑陋的)工作,因为我们必须愚弄类ggplot的testing…我认为这将更容易调用pdf() ; grid.draw(g); dev.off() pdf() ; grid.draw(g); dev.off() pdf() ; grid.draw(g); dev.off()明确。


罗兰编辑:

我做了一个小小的修改,并把它包装在一个函数中:

 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 } 

这可以很好地打印,甚至可以使用ggsave

刚刚从roland和baptiste中遇到了这个非常有用的函数,但是需要一个稍微不同的用例,其中原始的wrap头文件应该由函数转换而不是作为固定值提供。 我发布了原始函数的一个稍微修改过的版本,以防其他人使用。 它允许为包装条使用命名(固定值)expression式,以及使用自定义函数和ggplot2为facet_grid labeller参数(如label_parsedlabel_bquote )提供的函数。

 facet_wrap_labeller <- function(gg.plot, labels = NULL, labeller = label_value) { #works with R 3.1.2 and ggplot2 1.0.1 require(gridExtra) # old labels g <- ggplotGrob(gg.plot) gg <- g$grobs strips <- grep("strip_t", names(gg)) modgrobs <- lapply(strips, function(i) { getGrob(gg[[i]], "strip.text", grep=TRUE, global=TRUE) }) old_labels <- sapply(modgrobs, function(i) i$label) # find new labels if (is.null(labels)) # no labels given, use labeller function new_labels <- labeller(names(gg.plot$facet$facets), old_labels) else if (is.null(names(labels))) # unnamed list of labels, take them in order new_labels <- as.list(labels) else { # named list of labels, go by name where provided, otherwise keep old new_labels <- sapply(as.list(old_labels), function(i) { if (!is.null(labels[[i]])) labels[[i]] else i }) } # replace labels for(i in 1:length(strips)) { gg[[strips[i]]]$children[[modgrobs[[i]]$name]] <- editGrob(modgrobs[[i]], label=new_labels[[i]]) } g$grobs <- gg class(g) = c("arrange", "ggplot",class(g)) return(g) } 

更新/警告

对于更新版本的gridExtra软件包,你会得到错误Error: No layers in plot当运行这个函数时Error: No layers in plot因为arrange不再在gridExtra ,R试图把它解释为ggplot 。 您可以通过(重新)介绍arrange类的printfunction来解决这个问题:

 print.arrange <- function(x){ grid::grid.draw(x) } 

这应该现在允许情节呈现,你可以使用ggsave()例如像这样: ggsave("test.pdf", plot = facet_wrap_labeller(p, labeller = label_parsed))

例子

几个用例的例子:

 # artificial data frame data <- data.frame(x=runif(16), y=runif(16), panel = rep(c("alpha", "beta", "gamma","delta"), 4)) p <- ggplot(data, aes(x,y)) + geom_point() + facet_wrap(~panel) # no changes, wrap panel headers stay the same facet_wrap_labeller(p) # replace each panel title statically facet_wrap_labeller(p, labels = expression(alpha^1, beta^1, gamma^1, delta^1)) # only alpha and delta are replaced facet_wrap_labeller(p, labels = expression(alpha = alpha^2, delta = delta^2)) # parse original labels facet_wrap_labeller(p, labeller = label_parsed) # use original labels but modifying them via bquote facet_wrap_labeller(p, labeller = label_bquote(.(x)^3)) # custom function (eg for latex to expression conversion) library(latex2exp) facet_wrap_labeller(p, labeller = function(var, val) { lapply(paste0("$\\sum\\", val, "$"), latex2exp) }) 

ggplot2 2.1.0的ggplot2已经实现facet_wrap