Inverse of ggplotGrob?

I have a function which manipulates a ggplot object, by converting it to a grob and then modifying the layers. I would like the function to return a ggplot object not a grob. Is there a simple way to convert a grob back to gg?

The documentation on ggplotGrob is awfully sparse. Simple example:

P <- ggplot(iris) + geom_bar(aes(x=Species, y=Petal.Width), stat="identity")

G <- ggplotGrob(P)
... some manipulation to G ...

## DESIRED: 
P2 <- inverse_of_ggplotGrob(G)

such that, we can continue to use basic ggplot syntax, ie
`P2 + ylab ("The Width of the Petal")`

UPDATE:

To answer the question in the comment, the motivation here is to modify the colors of facet labels programmatically, based on the value of label name in each facet. The functions below work nicely (based on input from baptise in a previous question).

I would like for the return value from colorByGroup to be a ggplot object, not simply a grob.

Here is the code, for those interested

get_grob_strips <- function(G, strips=grep(pattern="strip.*", G$layout$name)) {

  if (inherits(G, "gg"))
    G <- ggplotGrob(G)
  if (!inherits(G, "gtable"))
    stop ("G must be a gtable object or a gg object")

  strip.type <- G$layout[strips, "name"]
  ## I know this works for a simple 
  strip.nms <- sapply(strips, function(i) {
     attributes(G$grobs[[i]]$width$arg1)$data[[1]][["label"]]
  })

  data.table(grob_index=strips, type=strip.type, group=strip.nms)
}


refill <- function(strip, colour){
  strip[["children"]][[1]][["gp"]][["fill"]] <- colour
  return(strip)
}

colorByGroup <- function(P, colors, showWarnings=TRUE) {
## The names of colors should match to the groups in facet
  G <- ggplotGrob(P)
  DT.strips <- get_grob_strips(G)

  groups <- names(colors)
  if (is.null(groups) || !is.character(groups)) {
    groups <- unique(DT.strips$group)
    if (length(colors) < length(groups))
      stop ("not enough colors specified")
    colors <- colors[seq(groups)]
    names(colors) <- groups
  }


  ## 'groups' should match the 'group' in DT.strips, which came from the facet_name
  matched_groups <- intersect(groups, DT.strips$group)
  if (!length(matched_groups))
    stop ("no groups match")
  if (showWarnings) {
      if (length(wh <- setdiff(groups, DT.strips$group)))
        warning ("values in 'groups' but not a facet label: \n", paste(wh, colapse=", "))
      if (length(wh <- setdiff(DT.strips$group, groups)))
        warning ("values in facet label but not in 'groups': \n", paste(wh, colapse=", "))
  }

  ## identify the indecies to the grob and the appropriate color
  DT.strips[, color := colors[group]]
  inds <- DT.strips[!is.na(color), grob_index]
  cols <- DT.strips[!is.na(color), color]

  ## Fill in the appropriate colors, using refill()
  G$grobs[inds] <- mapply(refill, strip = G$grobs[inds], colour = cols, SIMPLIFY = FALSE)

  G
}

I would say no. ggplotGrob is a one-way street. grob objects are drawing primitives defined by grid. You can create arbitrary grobs from scratch. There's no general way to turn a random collection of grobs back into a function that would generate them (it's not invertible because it's not 1:1). Once you go grob, you never go back.

You could wrap a ggplot object in a custom class and overload the plot/print commands to do some custom grob manipulation, but that's probably even more hack-ish.

r - Inverse of ggplotGrob?, The documentation on ggplotGrob is awfully sparse. Simple example: P <- ggplot​(iris) + geom_bar(aes(x=Species, y=Petal.Width), stat="identity") G  ## DESIRED: P2 <- inverse_of_ggplotGrob(G) such that, we can continue to use basic ggplot syntax, ie `P2 + ylab ("The Width of the Petal")` mise à jour: pour répondre à la question dans le commentaire, la motivation ici est de modifier les couleurs des étiquettes facet programmatically, basé sur la valeur du nom de l'étiquette dans chaque

You can try the following:

p = ggplotify::as.ggplot(g)

For more info, see https://cran.r-project.org/web/packages/ggplotify/vignettes/ggplotify.html

It involves a little bit of a cheat annotation_custom(as.grob(plot),...), so it may not work for all circumstances: https://github.com/GuangchuangYu/ggplotify/blob/master/R/as-ggplot.R

Ggplot grob, As usual, you can L'Inverse de ggplotGrob? j'ai une fonction qui manipule un objet ggplot, en le convertissant en grob puis en modifiant les couches. Search. Generate a ggplot2 plot grob. ggplotGrob (x) Arguments. x: ggplot2 object. Contents. ggplot2 is a part of the tidyverse, an ecosystem of packages designed with common

Have a look at the ggpubr package: it has a function as_ggplot(). If your grob is not too complex it might be a solution!

I would also advise to have a look at the patchwork package which combine nicely ggplots... it is likely to not be what you are looking for but... have a look.

[PDF] ggplot2, df2 <- data.frame(x = 1 , y = 1) g <- ggplotGrob(ggplot(df2, aes(x, y)) + geom_bar(aes(fill = drv), position = position_stack(reverse = TRUE)) +. For the sake of simplicity, the typical graph may be. ggplot (car, aes (x=speed, y=dist)) + geom_point () The function I would like to create is of the type. f <- function(DS, x, y) ggplot(DS, aes(x=x, y=y)) + geom_point() This however won't work, since x and y are not strings.

Drawing Survival Curves Using ggplot2, adhere, scales = "free") tbl_facet + theme(legend.position = "none") # Arrange faceted survival curves and risk tables g2 <- ggplotGrob(curv_facet) g3  This article will show you, step by step, how to combine multiple ggplots on the same page, as well as, over multiple pages, using helper functions available in the following R package: ggpubr R package, cowplot and gridExtra.

Inverse of ggplotGrob? r ggplot2 grob asked Oct 22 '14 at 2:54 stackoverflow.com 49 votes Manually setting group colors for ggplot2

Under the hood of ggplot2 graphics in R Posted on November 20, 2014 by zev@zevross.com · 2 Comments Originally, we were planning to include this material in our popular Beautiful plotting ggplot2 cheatsheet .

Comments
  • as.ggplot() doesn't convert grob to ggplot object. It only converts a plot into a ggplot.