In this article, you will learn how to save a ggplot to different file formats, including: PDF, SVG vector files, PNG, TIFF, JPEG, etc.
You can either print directly a ggplot into PNG/PDF files or use the convenient function ggsave()
for saving a ggplot.
The default of ggsave()
is to export the last plot that you displayed, using the size of the current graphics device. It also guesses the type of graphics device from the extension.
Contents:
Related Book
GGPlot2 Essentials for Great Data Visualization in RBasics
The standard procedure to save any graphics from R is as follow:
- Open a graphic device using one of the following functions:
- pdf(“r-graphics.pdf”),
- svg(“r-graphics.svg”),
- png(“r-graphics.png”),
- tiff(“r-graphics.tiff”),
- jpeg(“r-graphics.jpg”),
- and so on.
Additional arguments indicating the width and the height (in inches) of the graphics region can be also specified in the mentioned function.
- Create and print a plot
- Close the graphic device using the function
dev.off()
Save ggplot into a PDF file
For example, to export ggplot2 graphs to a pdf file, the R code looks like this:
# Create some plots
library(ggplot2)
myplot1 <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point()
myplot2 <- ggplot(iris, aes(Species, Sepal.Length)) +
geom_boxplot()
# Print plots to a pdf file
pdf("ggplot.pdf")
print(myplot1) # Plot 1 --> in the first page of PDF
print(myplot2) # Plot 2 ---> in the second page of the PDF
dev.off()
Print into a PNG file
For printing to a png file, use:
png("myplot.png")
print(myplot)
dev.off()
ggave
It’s also possible to make a ggplot and to save it from the screen using the function ggsave():
# 1. Create a plot: displayed on the screen (by default)
ggplot(mtcars, aes(wt, mpg)) + geom_point()
# 2.1. Save the plot to a pdf
ggsave("myplot.pdf")
# 2.2 OR save it to png file
ggsave("myplot.png")
Specify the name of the plot to export:
p1 <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
ggsave("myplot.png", plot = p1)
Recommended for you
This section contains best data science and self-development resources to help you on your path.
Books - Data Science
Our Books
- Practical Guide to Cluster Analysis in R by A. Kassambara (Datanovia)
- Practical Guide To Principal Component Methods in R by A. Kassambara (Datanovia)
- Machine Learning Essentials: Practical Guide in R by A. Kassambara (Datanovia)
- R Graphics Essentials for Great Data Visualization by A. Kassambara (Datanovia)
- GGPlot2 Essentials for Great Data Visualization in R by A. Kassambara (Datanovia)
- Network Analysis and Visualization in R by A. Kassambara (Datanovia)
- Practical Statistics in R for Comparing Groups: Numerical Variables by A. Kassambara (Datanovia)
- Inter-Rater Reliability Essentials: Practical Guide in R by A. Kassambara (Datanovia)
Others
- R for Data Science: Import, Tidy, Transform, Visualize, and Model Data by Hadley Wickham & Garrett Grolemund
- Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems by Aurelien Géron
- Practical Statistics for Data Scientists: 50 Essential Concepts by Peter Bruce & Andrew Bruce
- Hands-On Programming with R: Write Your Own Functions And Simulations by Garrett Grolemund & Hadley Wickham
- An Introduction to Statistical Learning: with Applications in R by Gareth James et al.
- Deep Learning with R by François Chollet & J.J. Allaire
- Deep Learning with Python by François Chollet
Version: Français
No Comments