Save R ggplot using ggsave

The R ggplot2 package is useful to plot different types of charts and graphs, but it is also essential to save those charts. To save the graphs, we can use the traditional approach (using the export option), or ggsave function provided by the ggplot2 package. Let us see how to Save the plots drawn by R ggplot using R ggsave function, and the export option with example.

The syntax to save the ggsave in R Programming is

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"))

Create R ggplot Scatter plot

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

TIP: ggplot2 package not installed by default. Please refer Install R Packages article to install the R Programming packages.

# 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"))
Save R ggplot using ggsave 1

Save R ggplot as PNG using ggsave

Let us see how to save the ggplot as the png image using the R ggplot2 ggsave function

# 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")
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 show how to save the ggplot as the jpeg image using the R ggplot2 ggsave function

# 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")
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 shows how to save the ggplot as the pdf file using the R ggplot2 ggsave function

# 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")
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

# 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")
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

Let us see how to save the ggplot using the traditional approach. First, go to the Export option under the plot tab, and select the Save as Image.. option

Save R ggplot using ggsave 10

Once you select the Save as Image.. option, a new window called Save Plot as Image open, 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 show how to save the ggplot as pdf using the traditional approach. For this, go to the Export option under the plot tab, and select the Save as PDF.. option.

Save R ggplot using ggsave 15

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

Save R ggplot using ggsave 16

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

Save R ggplot using ggsave 17