gpt4 book ai didi

r - ggplot2 0.9.2 中的数学符号

转载 作者:行者123 更新时间:2023-12-02 10:25:35 28 4
gpt4 key购买 nike

当您想要向 facet_wrap 添加数学符号时,您过去可以使用 grid 包,如 (example taken from here) 所示。 :

library(ggplot2); library(grid)
d <- ggplot(diamonds, aes(carat, price, fill = ..density..))
xlim(0, 2) + stat_binhex(na.rm = TRUE) + opts(aspect.ratio = 1)
d <- d + facet_wrap(~ color, ncol = 4)
grob <- ggplotGrob(d)
strip_elem <- grid.ls(getGrob(grob, "strip.text.x", grep=TRUE, global=TRUE))$name
grob <- editGrob(grob, strip_elem[1], label=expression(Y[1]))
grid.draw(grob)

这不再起作用,因为它给出了这个错误:

>  strip_elem <- grid.ls(getGrob(grob, "strip.text.x", grep=TRUE, global=TRUE))$name
Error in getGrob(grob, "strip.text.x", grep = TRUE, global = TRUE) :
It is only valid to get a child from a 'gTree'
> grob <- editGrob(grob, strip_elem[1], label=expression(Y[1]))
Error in editGrob(grob, strip_elem[1], label = expression(Y[1])) :
object 'strip_elem' not found

如何在0.9.2版本中添加数学符号?

最佳答案

更新使用facet_wrap_labeller函数可能会更容易available from here .

library(ggplot2)
library(gtable)

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

facet_wrap_labeller <- function(gg.plot, labels=NULL) {
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))
return(g)
}

# Some arbitrary strip texts
StripTexts = expression(Y[1], E, F, G, H, I, J)

# Apply the facet_wrap_labeller function
g = facet_wrap_labeller(d, StripTexts)

# Draw it
g

原始解决方案进行了少量修改,使选择 strip 文本变得更加容易。

这可行,但中间有点困惑。它使用 ggplot2 0.9.2 以及 grid 和 gtable 包中的函数。必须有一种更简单的方法,我希望您可以从中吸取一些东西来制定更简单的解决方案。

步骤是:

  • 绘制原始图;
  • 找到需要更改的 strip 元素;
  • 提取该 strip 元素;
  • 将“D”更改为表达式(Y[1])
  • 将修改后的 strip 元素插入到原始图中。

    library(ggplot2); library(grid); library(gtable)
    d <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
    xlim(0, 2) + stat_binhex(na.rm = TRUE) + theme(aspect.ratio = 1)
    (d <- d + facet_wrap(~color, ncol = 4))
    # The aim is to change the strip text "D" to expression(Y[1])

    # Winston's accessor function:
    gt_getgrob <- function(gt, pattern) {
    idx <- grep(pattern, gt$layout$name)
    if (length(idx) > 1)
    stop("More than one match for pattern '", pattern, "'")
    gt$grobs[[idx]]
    }

    g = ggplotGrob(d)
    g$layout # We want "strip_t-1"
    strip <- gt_getgrob(g, "t-1") # Use the accessor function to extract it.
    str(strip) # Locate the "D" label

    strip$children[[2]]$label = expression(Y[1]) # Change the label.
    grid.draw(strip) # Yes, it's worked

    gt <- ggplot_gtable(ggplot_build(d))
    gtable_show_layout(gt) # Get the layout of the original plot.

    gt = gtable_add_grob(gt, strip, t=3, l=4, b=3, r=4) # Insert the modified strip element into the plot.
    grid.newpage()
    grid.draw(gt)

结果是:

enter image description here

关于r - ggplot2 0.9.2 中的数学符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12629492/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com