I'm using ggsave() function to save my ggplots but I have a little problem with that. It occurs that my ggsave() is saving only my last plot (so for example if I have 6 side by side plots I have only saved last one). I know that it's not a problem, because ggsave by default is saving last plot, but I cannot find how can I change it to save all plots.
To summarize :
library(gridExtra)
grid.arrange(qplot(1:10,1:10),qplot(1:10,1:10))
ggsave(file=random.png)
what I want to have :
what I have :
ggsave default plot argument is last_plot(), you can change it and give your saved plot.
library(gridExtra)
temp <- grid.arrange(qplot(1:10,1:10),qplot(1:10,1:10))
ggsave(file='random.png', temp)
Related
In ggplot2, one can easily save a graphic into a R object.
p = ggplot(...) + geom_point() # does not display the graph
p # displays the graph
The standard function plot produces the graphic as a void function and returns NULL.
p = plot(1:10) # displays the graph
p # NULL
Is it possible to save a graphic created by plot in an object?
base graphics draw directly on a device.
You could use
1- recordPlot
2- the recently introduced gridGraphics package, to convert base graphics to their grid equivalent
Here's a minimal example,
plot(1:10)
p <- recordPlot()
plot.new() ## clean up device
p # redraw
## grab the scene as a grid object
library(gridGraphics)
library(grid)
grid.echo()
a <- grid.grab()
## draw it, changes optional
grid.newpage()
a <- editGrob(a, vp=viewport(width=unit(2,"in")), gp=gpar(fontsize=10))
grid.draw(a)
I am very late to this, but it was the first question which showed up when I searched for the question. So I'd like to add my solution for future viewers who come across the question.
I solved this by using a function instead of an object. For example, suppose we want to compare two beta distributions with different parameters. We can run:
z1<-rbeta(10000,5,5)
z2<-rbeta(10000,20,20)
plotit<-function(vector,alpha,beta){
plot(density(vector),xlim=c(0,1))
abline(v=alpha/(alpha+beta),lty="longdash")
}
And save the plots as functions rather than objects.
z.plot1<-function(){plotit(z1,5,5)}
z.plot2<-function(){plotit(z2,20,20)}
Next, we can call each plot as we want by simply calling the two plots as functions rather than objects.
z.plot1()
plots the first plot and
z.plot2()
plots the second.
Hope that helps someone who stumbles across this later!
You can use the active binding feature of the pryr package if you don't want to directly change the values of the object created.
library(pryr)
a %<a-% plot(1:10,1:10)
Each time you type a on the console the graph will be reprinted on the screen. The %<a-% operator will rerun the script every time (in case of one graph this is not a problem I think). So essentially every time you use a the code will be rerun resulting in your graph which of course you can manipulate (overlay another plot on top) or save using png for example. No value itself will be stored in a however. The value will still be NULL.
I don't know if the above is what you are looking for but it might be an acceptable solution.
library(ggplot2)
# if mygraph is a plot object
ggsave("myplot1.png",mygraph)
# if the plot is in a list (e.g. created by the Bibliometrics package)
ggsave("myplot1.png",mygraphs[[1]])
In ggplot2, one can easily save a graphic into a R object.
p = ggplot(...) + geom_point() # does not display the graph
p # displays the graph
The standard function plot produces the graphic as a void function and returns NULL.
p = plot(1:10) # displays the graph
p # NULL
Is it possible to save a graphic created by plot in an object?
base graphics draw directly on a device.
You could use
1- recordPlot
2- the recently introduced gridGraphics package, to convert base graphics to their grid equivalent
Here's a minimal example,
plot(1:10)
p <- recordPlot()
plot.new() ## clean up device
p # redraw
## grab the scene as a grid object
library(gridGraphics)
library(grid)
grid.echo()
a <- grid.grab()
## draw it, changes optional
grid.newpage()
a <- editGrob(a, vp=viewport(width=unit(2,"in")), gp=gpar(fontsize=10))
grid.draw(a)
I am very late to this, but it was the first question which showed up when I searched for the question. So I'd like to add my solution for future viewers who come across the question.
I solved this by using a function instead of an object. For example, suppose we want to compare two beta distributions with different parameters. We can run:
z1<-rbeta(10000,5,5)
z2<-rbeta(10000,20,20)
plotit<-function(vector,alpha,beta){
plot(density(vector),xlim=c(0,1))
abline(v=alpha/(alpha+beta),lty="longdash")
}
And save the plots as functions rather than objects.
z.plot1<-function(){plotit(z1,5,5)}
z.plot2<-function(){plotit(z2,20,20)}
Next, we can call each plot as we want by simply calling the two plots as functions rather than objects.
z.plot1()
plots the first plot and
z.plot2()
plots the second.
Hope that helps someone who stumbles across this later!
You can use the active binding feature of the pryr package if you don't want to directly change the values of the object created.
library(pryr)
a %<a-% plot(1:10,1:10)
Each time you type a on the console the graph will be reprinted on the screen. The %<a-% operator will rerun the script every time (in case of one graph this is not a problem I think). So essentially every time you use a the code will be rerun resulting in your graph which of course you can manipulate (overlay another plot on top) or save using png for example. No value itself will be stored in a however. The value will still be NULL.
I don't know if the above is what you are looking for but it might be an acceptable solution.
library(ggplot2)
# if mygraph is a plot object
ggsave("myplot1.png",mygraph)
# if the plot is in a list (e.g. created by the Bibliometrics package)
ggsave("myplot1.png",mygraphs[[1]])
I am using GGally::pairs to look at 63 columns of numerical data. The function runs fine but the result is a plot where I can't read any of the column names fully or the numbers (correlations etc.). The resolution is fine, it is just that the plot is compressed so that the plots are tiny and cant really be read (and only part of the text is visible). This is because it prints the plot to the plot viewer in Rstudio I suppose. How can I get the plot to be bigger so I can actually see the results
Revising my comment above, instead of ggsave() that I'm not sure can work, this might work, but assumes plotly accommodates the geoms in your plot (it does for a good number of ggplot geoms, but not all, eg many of the library(ggridges)'s geoms).
library(plotly)
library(htmlwidgets)
p <- ggplot(your ggplot data/parameters/geoms) #creates ggplot object
p <- ggplotly(p) #converts p to html, renders in RStudio console
htmlwidgets::saveWidget(as_widget(p), "filename.html") #saves as a single html file
I want to add an additional tick to my plot in ggplot2, and used Solution 2 as described in the post Annotate ggplot with an extra tick and label.
It worked fine for me, giving the following result in R Studio:
But when I try to save the result using ggsave() to create the a .pdf, .ps, or .png file, the red number is cut off half like this:
I have the feeling that the inner plot is printed first and later the margins are plotted on top of this.
Anybody has a hint?
Thank you Z. Lin! I just had a grid.draw(g) instead of g <- grid.draw(g). This dot in R always activates my python brain region :)
In ggplot2, one can easily save a graphic into a R object.
p = ggplot(...) + geom_point() # does not display the graph
p # displays the graph
The standard function plot produces the graphic as a void function and returns NULL.
p = plot(1:10) # displays the graph
p # NULL
Is it possible to save a graphic created by plot in an object?
base graphics draw directly on a device.
You could use
1- recordPlot
2- the recently introduced gridGraphics package, to convert base graphics to their grid equivalent
Here's a minimal example,
plot(1:10)
p <- recordPlot()
plot.new() ## clean up device
p # redraw
## grab the scene as a grid object
library(gridGraphics)
library(grid)
grid.echo()
a <- grid.grab()
## draw it, changes optional
grid.newpage()
a <- editGrob(a, vp=viewport(width=unit(2,"in")), gp=gpar(fontsize=10))
grid.draw(a)
I am very late to this, but it was the first question which showed up when I searched for the question. So I'd like to add my solution for future viewers who come across the question.
I solved this by using a function instead of an object. For example, suppose we want to compare two beta distributions with different parameters. We can run:
z1<-rbeta(10000,5,5)
z2<-rbeta(10000,20,20)
plotit<-function(vector,alpha,beta){
plot(density(vector),xlim=c(0,1))
abline(v=alpha/(alpha+beta),lty="longdash")
}
And save the plots as functions rather than objects.
z.plot1<-function(){plotit(z1,5,5)}
z.plot2<-function(){plotit(z2,20,20)}
Next, we can call each plot as we want by simply calling the two plots as functions rather than objects.
z.plot1()
plots the first plot and
z.plot2()
plots the second.
Hope that helps someone who stumbles across this later!
You can use the active binding feature of the pryr package if you don't want to directly change the values of the object created.
library(pryr)
a %<a-% plot(1:10,1:10)
Each time you type a on the console the graph will be reprinted on the screen. The %<a-% operator will rerun the script every time (in case of one graph this is not a problem I think). So essentially every time you use a the code will be rerun resulting in your graph which of course you can manipulate (overlay another plot on top) or save using png for example. No value itself will be stored in a however. The value will still be NULL.
I don't know if the above is what you are looking for but it might be an acceptable solution.
library(ggplot2)
# if mygraph is a plot object
ggsave("myplot1.png",mygraph)
# if the plot is in a list (e.g. created by the Bibliometrics package)
ggsave("myplot1.png",mygraphs[[1]])