我想知道如何更改
facet
标签到
ggplot2
中的数学公式
.
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)
例如,我想从
D
更改构面标签
到
Y[1]
,其中1是下标.预先感谢您的帮助。
我找到了这个答案,但这对我不起作用.我正在使用
R 2.15.1
和
ggplot2 0.9.1
- 2021-1-121 #
- 2021-1-122 #
您可以在gtable中编辑grob,
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)]))
或者,如果您想保存一个文件,
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类的测试……我认为调用
pdf() ; grid.draw(g); dev.off()
会更容易 明确地。
罗兰(Roland)编辑:我做了一个小的更正,并将其包装在一个函数中:
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 }
这可以很好地print甚至
ggsave
- 2021-1-123 #
只是从roland和baptiste遇到了这个非常有用的函数,但是需要一个稍微不同的用例,其中原始的wrap头应该由函数转换而不是提供为固定值.我发布了原始功能的稍作修改的版本,以防对其他人有用.它既允许使用包装带的命名(固定值)表达式,也可以使用自定义函数以及ggplot2已为
facet_grid
提供的函数labeller
参数(例如label_parsed
和label_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
运行此功能时,因为arrange
不再在gridExtra
并且R试图将其解释为"ggplot
" .您可以通过(重新)引入print
来解决此问题arrange
的功能 Class: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 (e.g. for latex to expression conversion) library(latex2exp) facet_wrap_labeller(p, labeller = function(var, val) { lapply(paste0("$\\sum\\", val, "$"), latex2exp) })
- 2021-1-124 #
自
ggplot2
2.1.0labeller
已针对facet_wrap
实施
相关问题
- r:ggplot2-大小单位rggplot22021-01-11 23:55
- r:ggplot2条形图的多个子组rggplot22021-01-11 18:55
- r:通过过滤隐藏对象来减少绘图的PDF文件大小rpdfvectorplotggplot22021-01-10 19:26
- r:如何在ggplot2的构面中添加常规标签?rlabelfacetggplot22021-01-09 22:55
- r:ggplot结合了来自不同dataframes的两个图rggplot22021-01-09 10:25
也许有人在某个时候更改了edit-Grob函数的名称. (编辑:它已由@hadley于8个月前删除。)没有
geditGrob
但只是editGrob
来自pkg:grid似乎有效: