首页>Program>source

我想知道如何更改 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.1ggplot2 0.9.1

最新回答
  • 2021-1-12
    1 #

    也许有人在某个时候更改了edit-Grob函数的名称. (编辑:它已由@hadley于8个月前删除。)没有 geditGrob 但只是 editGrob 来自pkg:grid似乎有效:

    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)
    

  • 2021-1-12
    2 #

    您可以在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-12
    3 #

    只是从roland和baptiste遇到了这个非常有用的函数,但是需要一个稍微不同的用例,其中原始的wrap头应该由函数转换而不是提供为固定值.我发布了原始功能的稍作修改的版本,以防对其他人有用.它既允许使用包装带的命名(固定值)表达式,也可以使用自定义函数以及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 运行此功能时,因为 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-12
    4 #

    ggplot2 2.1.0 labeller 已针对 facet_wrap实施

  • python:比较两个CSV文件并搜索相似的项目
  • reflection:如何拦截具有标准Java功能(无AspectJ等)的方法调用?