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 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
Let us open the diamonds.png file and see
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
Let us open the diamonds2.jpeg file and see
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
Let us open the diamonds.pdf file and see
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
Let us open the diamonds4.png file and see
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.
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.
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
Next, you can change the Width, and height of an image.
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.
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.
From the below screenshot you can see the newly saved png, and pdf files.
Thank You for Visiting Our Blog