This question already has answers here:
How to save a plot as image on the disk?
(11 answers)
Closed 5 years ago.
Most of my plots are made with ggplot2 and the ggsave command saves them where they should be with one line. However, mosaic plots using the vcd package are best for my data.
Problem: I don't get an error message with the following code. R says that it has saved my plot, but the plot that gets saved is the last ggplot plot I created, not the mosaic plot I want. Of course I can manually save in RStudio, but I'm quite sure there is a better way. Any ideas?
Onlyaround <- subset(prepData, preposition=="around")
attach(Onlyaround)
mytable <- table(exp_group, session, result)
ftable(mytable)
mosaic(mytable, shade=TRUE, legend=TRUE, main = "Around by Group")
margin.table(mytable)
ggsave("pics/around_mosaic.png")
detach(Onlyaround)
ggsave() is in fact the command to save ggplots, so it's no surprise that it doesn't save you mosaic plot. The standard 'R' way to save plots will work just fine:
jpeg("pics/around_mosaic.png")
mosaic(mytable, shade=TRUE, legend=TRUE, main = "Around by Group")
dev.off()
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]])
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)
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]])
This question already has answers here:
Reduce PDF file size of plots by filtering hidden objects
(3 answers)
Closed 6 years ago.
I have saved several pdf plots with large file size in R, the problem is that I will need to import them into Latex, which takes a lot of time. I am wondering how to save a plot with smaller file size in R? Thanks.
Example 1.
seasonplot(ts(hdemand$Demand,frequency=24),
col=rainbow(length(hdemand$Demand)/48))
dev.print(device=pdf,file="hourdemand.pdf")
dev.off()
Example 2. (Even the fitted plot takes times because of the size of data)
par(mfrow=c(1,2))
plot(data$Temp,all.data$Demand)
abline(lm(data$Demand~data$Temp), col="red")
plot(data$APX,data$Demand)
abline(lm(data$Demand~data$APX), col="red")
dev.print("LR.pdf",device=pdf)
dev.off()
Reduce the number of points you are plotting.
But if you can't do that, and don't care about how it would look when zoomed in, write out as png at a convenient dpi and then convert to pdf outside R.
This question already has an answer here:
Call to plot doesn't actually produce plot
(1 answer)
Closed 9 years ago.
I am generating a plot which I am able to see in the RMarkdown output but whenever I am trying to save it, I am getting just a blank(white) image. I am just adding following two lines before and after plot
png("Output.png")
#Plot code
dev.off()
It was working. And suddenly it stopped working. Can someone help me on this??
edit
When I do it with pdf
pdf("output.pdf")
#plot code
dev.off()
I am getting error as:
There was error opening this document. This document cannot be opened because it has no pages.
Thanks.
From the comments I gather that I was right, you need to print the resulting ggplot2 object in order to show the plot, see also R FAQ 7.22.