editGrob on a ggplotGrob returned object

462 views
Skip to first unread message

Tengfei

unread,
Sep 4, 2012, 11:34:15 PM9/4/12
to ggp...@googlegroups.com
Dear all,

in ggplot2 0.9.1, following code would work, but in 0.9.2, the ggplotGrob return a gtable and following code give some errors

> library(ggplot2)
> p <- qplot(wt, mpg, data = mtcars, colour = cyl, main = "title")
> g <- ggplotGrob(p)
> g <- editGrob(g, "title-2-2", gp = gpar(fontface = "italic"),
+                  grep = TRUE, global = TRUE)
Error in editGrob(g, "title-2-2", gp = gpar(fontface = "italic"), grep = TRUE,  : 
  It is only valid to edit a child of a 'gTree'

so what's the equivalent for the code above to edit a specific grob on the fly?

Thanks

Tengfei

Winston Chang

unread,
Sep 5, 2012, 12:29:15 AM9/5/12
to Tengfei, ggp...@googlegroups.com
g is a gtable object. You can inspect it with the following:

# Show the structure of the gtable object, but don't recurse into sub-components
str(g, max.level=1)

# Print the data frame recording layout
g$layout

# Get the list of grobs in the gtable
g$grobs

# Get a 1x1 gtable that contains just the title
g[2,4]



This code will have the effect you're looking for. It might be worth
thinking about how to modify the gtable API to make this sort of
access easier.

library(ggplot2)
library(grid)
library(gtable)
p <- qplot(wt, mpg, data = mtcars, colour = cyl, main = "Title text")
g <- ggplotGrob(p)

# Get index of the title grob
idx <- which(g$layout$name == "title")

# Modify the title grob and save it back
g$grobs[[idx]] <- editGrob(g$grobs[[idx]], gp = gpar(fontface =
"italic", col="red"))

grid.draw(g)



It would also be nice to be able to do something like grid.ls() on a
gtable and get back a useful result.

-Winston
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

Tengfei Yin

unread,
Sep 5, 2012, 12:35:50 AM9/5/12
to Winston Chang, ggp...@googlegroups.com
On Tue, Sep 4, 2012 at 11:29 PM, Winston Chang <winsto...@gmail.com> wrote:
g is a gtable object. You can inspect it with the following:

# Show the structure of the gtable object, but don't recurse into sub-components
str(g, max.level=1)

# Print the data frame recording layout
g$layout

# Get the list of grobs in the gtable
g$grobs

# Get a 1x1 gtable that contains just the title
g[2,4]



This code will have the effect you're looking for. It might be worth
thinking about how to modify the gtable API to make this sort of
access easier.
Yes, a more easier API would be nice. 

library(ggplot2)
library(grid)
library(gtable)
p <- qplot(wt, mpg, data = mtcars, colour = cyl, main = "Title text")
g <- ggplotGrob(p)

# Get index of the title grob
idx <- which(g$layout$name == "title")

# Modify the title grob and save it back
g$grobs[[idx]] <- editGrob(g$grobs[[idx]], gp = gpar(fontface =
"italic", col="red"))

grid.draw(g)

 
Thanks a lot for this clear elucidation and the examples!

Tengfei  



--
Tengfei Yin
MCDB PhD student
1620 Howe Hall, 2274,
Iowa State University
Ames, IA,50011-2274



Reply all
Reply to author
Forward
0 new messages