Site icon R-bloggers

ggplot2 Transparent Background Quick Guide

[This article was first published on Data Analysis in R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post ggplot2 Transparent Background Quick Guide appeared first on finnstats.

We encourage that you read this article from finnstats to stay up to date.

ggplots Transparent Background, the ggplot2 syntax for adding a translucent background to a plot is as follows.

p +
  theme(
    panel.background = element_rect(fill='transparent'), #transparent panel bg
    plot.background = element_rect(fill='transparent', color=NA), #transparent plot bg
    panel.grid.major = element_blank(), #remove major gridlines
    panel.grid.minor = element_blank(), #remove minor gridlines
    legend.background = element_rect(fill='transparent'), #transparent legend bg
    legend.box.background = element_rect(fill='transparent') #transparent legend panel
  )

Make sure to indicate that the backdrop should be translucent when exporting the plot with ggsave().

ggsave('myplot.png', p, bg='transparent')

The usage of this syntax in practice is demonstrated by the example that follows.

How to Use ggplot2 Transparent Background

The ggplot2 code below demonstrates how to make a straightforward grouped boxplot:

library(ggplot2)

Make this illustration repeatable

set.seed(123)

Now we can create a dataset

df<- data.frame(team=rep(c('P1', 'P2', 'P3'), each=50),
program=rep(c('LOW', 'MEDIUM'), each=25),
values=seq(1:150)+sample(1:100, 150, replace=TRUE))
head(df)
team program values
1   P1     LOW     84
2   P1     LOW     58
3   P1     LOW     42
4   P1     LOW     72
5   P1     LOW      6
6   P1     LOW     46

Yes, let’s create a boxplot

ggplot(df, aes(x=team, y=values, fill=program)) +
  geom_boxplot()

To give the plot a transparent background, we can use the following code.

p <- ggplot(df, aes(x=team, y=values, fill=program)) +
       geom_boxplot() +
       theme(
         panel.background = element_rect(fill='transparent'),
         plot.background = element_rect(fill='transparent', color=NA),
         panel.grid.major = element_blank(),
         panel.grid.minor = element_blank(),
         legend.background = element_rect(fill='transparent'),
         legend.box.background = element_rect(fill='transparent')
       )

Now we can display the boxplot

p

We can then export this plot to a PNG file, specifying that the background should be transparent in the exported image.

ggsave(‘boxplot.png’, p, bg=’transparent’)

Check your inbox or spam folder to confirm your subscription.

To read more visit ggplot2 Transparent Background Quick Guide.

If you are interested to learn more about data science, you can find more articles here finnstats.

The post ggplot2 Transparent Background Quick Guide appeared first on finnstats.

To leave a comment for the author, please follow the link and comment on their blog: Data Analysis in R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.