5
I would recommend using either the ggplot2
or the lattice
, both work so that the graphics are R objects, which can be modified, saved etc in an easy way. For example:
library(ggplot2)
grafico_ggplot <- ggplot(mtcars, aes(mpg, cyl)) + geom_point()
grafico_ggplot
library(lattice)
grafico_lattice <- xyplot(cyl~mpg, mtcars)
grafico_lattice
However, if you want to do it with the base graph of R, you can use the function recordPlot
:
plot(1:10)
grafico <- recordPlot()
dev.off()
grafico
It is also possible to transform the basic graphics into graphics grid
, which allows you greater flexibility in changing the elements of the graphics and combining them with graphics from ggplot2
and lattice
. For this you have to use the package gridGraphics
that turns basic graphics into graphics grid
:
library(gridGraphics)
plot(1:10)
grid.echo()
grafico_grid <- grid.grab()
grid.draw(grafico_grid)