Saving a graph with ggsave after using ggplot_build and ggplot_gtable
it does not work because ggsave
wants an object of class ggplot
, while you're passing a grob. arrangeGrob
will sometimes trick ggsave
in pretending inheritance from ggplot
, but only when at least one of the grobs belongs to this class; here, however, you're only passing a gtable
.
Perhaps the easiest workaround is to clone ggsave and bypass the class check,
ggsave <- ggplot2::ggsave; body(ggsave) <- body(ggplot2::ggsave)[-2]
Edit: The dev version of ggplot2 no longer requires this hack*, as ggsave
now works with any grob.
*PS: this hack works no longer, as arrangeGrob now returns a gtable, and its print method does not draw on a device.
How to save a customize plot using ggsave with all customized properties?
Just change theme_bw()
with theme()
. Hopefully, this will work.
ggsave("~/My/plot.png", plot = plot+
theme(), width = 10, height = 8, dpi = 150, units = "in", device='png')
Why is ggsave not saving my plot to my computer?
It works fine if you use ggplot()
from ggplot2
instead of plot()
Packages and data
library(ggplot2)
library(gapminder)
data("gapminder")
attach(gapminder)
Solution
ggplot(gapminder,
aes(x = log(gdpPercap), y = lifeExp)) +
geom_point()
ggsave("filename.png",dpi = 300)
Here are some tweaks you came make to make it more similar to plot()
appearance:
ggplot(gapminder,
aes(x = log(gdpPercap), y = lifeExp)) +
geom_point(shape = 1) +
theme_linedraw()
output from last code
R: Saving a graph with ggsave after using north2() to create map with north arrow
You need to use the base function pdf
instead of ggsave
.
pdf(file="Figure 2.2A.pdf",width=11.5,height=8)
north2(graph1, x = 0.73, y = 0.89, scale = 0.1, symbol = 3)
dev.off()
This is because the north2
function works in a somewhat non-standard way; it plots the resulting plot instead of returning it. In the help it explains why this variant exists.
The plain north
function is more standard; you would add this to your plot instead, like
graph1 + north(data=df, ...)
Then you could use the ggsave
function as expected (after this, though, not before).
Note that for traditional plots you open the file first with pdf()
, then run your plotting code, then close the file with dev.off
; but for ggplots, you make your plot first and then call ggsave
, with no dev.off
needed.
Saving grid.arrange() plot to file
grid.arrange
draws directly on a device. arrangeGrob
, on the other hand, doesn't draw anything but returns a grob g
, that you can pass to ggsave(file="whatever.pdf", g)
.
The reason it works differently than with ggplot objects, where by default the last plot is being saved if not specified, is that ggplot2 invisibly keeps track of the latest plot, and I don't think grid.arrange
should mess with this counter private to the package.
Save ggplot with twice with different file formats with ggsave (ggplot2)
You could use mapply()
in this way:
#Code
mapply(function(x) ggsave(x,plot = Yourplot,width = 25, height = 18, units = 'cm'),
x=c('plot.pdf','plot.png'))
Related Topics
Ggmap Error: Geomrasterann Was Built with an Incompatible Version of Ggproto
Emulate Split() with Dplyr Group_By: Return a List of Data Frames
What Are the Differences Between Community Detection Algorithms in Igraph
Ggplot2 Plot Without Axes, Legends, etc
Remove Grid, Background Color, and Top and Right Borders from Ggplot2
Find Which Interval Row in a Data Frame That Each Element of a Vector Belongs In
What Is the Meaning of the Dollar Sign "$" in R Function()
Using Stargazer with Rstudio and Knitr
Conditional Coloring of Cells in Table
Variable Name Restrictions in R
Shiny Slider on Logarithmic Scale
Format for Ordinal Dates (Day of Month with Suffixes -St, -Nd, -Rd, -Th)
Fill Missing Combinations in a Dataframe
Split Dataframe by Levels of a Factor and Name Dataframes by Those Levels