Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert gtable into ggplot in R ggplot2

Tags:

r

ggplot2

Although gtable could be converted into ggplot using ggplotify::as.ggplot, the output is different from the origin ggplot. For example:

library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
g <- ggplot_gtable(ggplot_build(p))
p_new <- ggplotify::as.ggplot(p)

# origin
p + theme(aspect.ratio = 2)

# changed
p_new + theme(aspect.ratio = 2)   # different figure shape from the origin one

How could I covert gtable into the same ggplot p_new as the origin one p?

like image 204
Feng Tian Avatar asked Oct 21 '25 15:10

Feng Tian


1 Answers

You can use ggplotify::as.ggplot():

library("ggplotify")
as.ggplot(q)
like image 103
s__ Avatar answered Oct 23 '25 06:10

s__



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!