ggplot2 Plot with Transparent Background in R (2 Examples)
In this post, I’ll illustrate how to export a ggplot2 graph with transparent background in R programming.
The tutorial consists of the following information:
Here’s the step-by-step process…
Example Data, Add-On Packages & Default Graph
To start with, let’s construct some exemplifying data:
data <- data.frame(x = 1:5, # Create example data y = 5:9) data # Print example data
Table 1 shows the structure of the example data – It has five rows and two columns.
To be able to use the functions of the ggplot2 package, we also need to install and load ggplot2:
install.packages("ggplot2") # Install & load ggplot2 package library("ggplot2")
As next step, we can draw our data with default background:
ggp <- ggplot(data, aes(x, y)) + # Create default ggplot2 plot geom_bar(stat = "identity") ggp # Draw default ggplot2 plot
Figure 1 shows the output of the previous syntax: A ggplot2 barchart without opacity.
Example 1: Make Rectangle Elements of ggplot2 Plot Transparent Using element_rect
Example 1 shows how to export a ggplot2 plot with transparent rectangle elements.
For this, we can use the theme function and the rect argument as shown below:
ggp_transparent1 <- ggp + # Make background transparent theme(rect = element_rect(fill = "transparent"))
Next, we can use the ggsave function to write our transparent plot to a PNG file:
ggsave(ggp_transparent1, # Save transparent png file filename = "ggp_transparent1.png", bg = "transparent")
Have a look at your current working directory after running the previous R code. You should find a ggplot2 plot with transparent background.
Example 2: Make All Background Elements of ggplot2 Plot Transparent Using theme
This example shows how to manually specify all background elements that you want to remove.
Have a look at the following R code:
ggp_transparent2 <- ggp + # Remove background elements manually theme(legend.background = element_rect(fill = "transparent"), legend.box.background = element_rect(fill = "transparent"), panel.background = element_rect(fill = "transparent"), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), plot.background = element_rect(fill = "transparent", color = NA))
Next, we can export our plot using the ggsave function as we already did in Example 1:
ggsave(ggp_transparent2, # Save transparent png file filename = "ggp_transparent2.png", bg = "transparent")
Video & Further Resources
Have a look at the following video of my YouTube channel. In the video, I illustrate the topics of the present tutorial in RStudio.
In addition to the video, you may want to read the other articles on this homepage. Some tutorials are shown below.
- Beginner/Advanced Tutorial for the ggplot2 Package
- Change Background Color of ggplot2 Plot
- Remove Grid, Background Color, Top & Right Borders from ggplot2 Plot
- Graphics Gallery in R
- R Programming Overview
To summarize: At this point of the tutorial you should know how to make the background of a ggplot2 graphic transparent with alpha = 0 in the R programming language. Don’t hesitate to let me know in the comments, in case you have further questions.