I created a graph with ggplot
and later I used arrangeGrob
to combine those graphs. Is there a way to remove parts a graph from this combined plot? Or maybe extract?
Here is a minimal example:
library(ggplot2)
library(gridExtra)
df <- data.frame(x=rnorm(20), y=rnorm(20), y2=rnorm(20))
g1 <- ggplot(df, aes(x, y)) + geom_point()
g2 <- ggplot(df, aes(x, y2)) + geom_point()
g <- arrangeGrob(g1,g2, widths=c(3.5,7.5), ncol=2)
print(g)
I would like to remove one of the two plots.
First, use
grid.ls()
to see a listing of the grobs that make up the plot. Here, you'll be looking for the names of the twogTree
objects that encode the individual plots. (Compared to lattice, ggplot2's naming of component grobs is relatively unhelpful, although in this case, it's not too hard to see which pieces you'll want to extract.)Then, you can extract and plot them like this: