I want to alter a ggplot2
plot in R using the ggplot_build
and ggplot_gtable
functions and use it afterwards in a plot_grid
.
Example code to make the plot:
library(ggplot2)
library(cowplot)
p1 <- ggplot(iris) +
aes(x = Sepal.Length, y = Sepal.Width, colour = Species) +
geom_point()
p2 <- ggplot(iris) +
aes(x = Petal.Length, y = Petal.Width, colour = Species) +
geom_point()
plot_grid(p1, p2)
Then I change p1 using ggplot_build and ggplot_gtable:
q1 <- ggplot_build(p1)
q1$data[[1]]$colour <- "black"
q1 <- ggplot_gtable(q1)
plot(q1)
plots the plot that I want but I can not use it in plot_grid
with plot_grid(q1, p2)
. How can I solve this?
Edit: Code should have indeed worked. After a complete R restart everything worked as expected. Should have tried this first, apologies!