Tutorial Gateway

  • C Language
  • Java
  • R
  • SQL
  • MySQL
  • Python
  • BI Tools
    • Informatica
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • QlikView
  • Js

Save R ggplot using ggsave

by suresh

The R ggplot2 package is useful to plot different types of charts, and graphs, but it is also important to save those charts. In order to save the graphs we can use the traditional approach (using the export option), or ggsave function provided by the ggplot2 package. In this article we will show you, How to Save the plots drawn by R ggplot using R ggsave function, and the export option with example.

Syntax of a R ggsave

The basic syntax to save the ggplot in R Programming is as shown below

ggsave(filename)

and the complex syntax behind this R ggsave is:

ggsave(filename, plot = last_plot(), device = NULL, path = NULL,
       scale = 1, width = NA, height = NA, dpi = 300, limitsize = TRUE, ..,
       units = c("in", "cm", "mm"))

TIP: ggplot2 package is not installed by default. Please refer Install R Packages article to understand the steps involved in installing a package.

Create R ggplot Scatter plot

In this example we will draw a scatter plot, and we are going to save this scatter plot. I suggest you to refer R ggplot2 Scatter Plot article to understand the steps involved in plotting the scatter plot.

R CODE

# Create R ggplot Scatter Plot

# Importing the ggplot2 library
library(ggplot2)

ggplot(diamonds) + 
  geom_point(aes(x = carat, y = price, color = cut)) +
  scale_color_manual(values = c("orchid", "chocolate4", 
                                "goldenrod2", "tomato2", "midnightblue"))

OUTPUT

Save R ggplot using ggsave 1

Save R ggplot as PNG using ggsave

In this example we will show you, How to save the ggplot as the png image using the R ggplot2 ggsave function

R CODE

# Saving R ggplot with R ggsave Function

# Importing the ggplot2 library
library(ggplot2)

ggplot(diamonds) + 
  geom_point(aes(x = carat, y = price, color = cut)) +
  scale_color_manual(values = c("orchid", "chocolate4", 
                                "goldenrod2", "tomato2", "midnightblue"))
# To save the ggplot as png
ggsave("diamonds.png")

OUTPUT

Save R ggplot using ggsave 2

Let us open the diamonds.png file and see

Save R ggplot using ggsave 3

Save R ggplot as JPEG using ggsave

In this example we will show you, How to save the ggplot as the jpeg image using the R ggplot2 ggsave function

R CODE

# Saving R ggplot with R ggsave Function

# Importing the ggplot2 library
library(ggplot2)

ggplot(diamonds) + 
  geom_point(aes(x = carat, y = price, color = cut)) +
  scale_color_manual(values = c("orchid", "chocolate4", 
                                "goldenrod2", "tomato2", "midnightblue"))
# saving the scatterplot as jpeg
ggsave("diamonds2.jpeg")

OUTPUT

Save R ggplot using ggsave 4

Let us open the diamonds2.jpeg file and see

Save R ggplot using ggsave 5

Save R ggplot as PDF using ggsave

This example will show, How to save the ggplot as the pdf file using the R ggplot2 ggsave function

R CODE

# Saving R ggplot with R ggsave Function

# Importing the ggplot2 library
library(ggplot2)

ggplot(diamonds) + 
  geom_point(aes(x = carat, y = price, color = cut)) +
  scale_color_manual(values = c("orchid", "chocolate4", 
                                "goldenrod2", "tomato2", "midnightblue"))
# Saving R ggplot as pdf
ggsave("diamonds3.pdf")

OUTPUT

Save R ggplot using ggsave 6

Let us open the diamonds.pdf file and see

Save R ggplot using ggsave 7

Save R ggplot with width & Height using ggsave

The ggsave function also allows us to specify the width and height of the image using the width, and Height parameters

R CODE

# Saving R ggplot with R ggsave Function

# Importing the ggplot2 library
library(ggplot2)

ggplot(diamonds) + 
  geom_point(aes(x = carat, y = price, color = cut)) +
  scale_color_manual(values = c("orchid", "chocolate4", 
                                "goldenrod2", "tomato2",
                                "midnightblue"))
ggsave("diamonds4.png", 
       width = 30, height = 20, units = "cm")

OUTPUT

Save R ggplot using ggsave 8

Let us open the diamonds4.png file and see

Save R ggplot using ggsave 9

Save R ggplot as Image using Export

In this example we will show you, How to save the R ggplot using the traditional approach. First, goto the Export option under the plot tab, and select the Save as Image.. option as we shown below.

Save R ggplot using ggsave 10

Once you select the Save as Image.. option, a new window called Save Plot as Image will be opened. Please select the image format you wish to save.

Save R ggplot using ggsave 12

Next, click on the Directory button to choose the file directory, or the location you want to save the image. From the below screenshot you can observe that, we are selecting the R Programs folder

Save R ggplot using ggsave 13

Next, you can change the Width, and height of an image.

Save R ggplot using ggsave 14

Once you finished the setting, click on the Save button to save the image in the respective format.

Save R ggplot as PDF using Export

In this example we will show you, How to save the ggplot as pdf using the traditional approach. For this, goto the Export option under the plot tab, and select the Save as PDF.. option as shown below.

Save R ggplot using ggsave 15

Once you select the Save as PDF.. option, a new window called Save Plot as PDF will be opened. Please select the directory, and change the pdf file name.

Save R ggplot using ggsave 16

From the below screenshot you can see the newly saved png, and pdf files.

Save R ggplot using ggsave 17

Thank You for Visiting Our Blog

Placed Under: R Programming

Stay in Touch!

Sign Up to receive Email updates on Latest Tutorials

  • C Programs
  • Java Programs
  • SQL FAQ’s
  • Python Programs
  • SSIS
  • Tableau
  • JavaScript

Copyright © 2019 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy