How to Change Background Color in ggplot2

[This article was first published on finnstats », 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.

Story 480107782

When you change the background colors in ggplot, it will be more visually pleasing and attract more attention than the default ones.

p + theme(panel.background = element_rect(fill = 'white', color = 'blue'),
panel.grid.major = element_line(color = 'green', linetype = 'dotted'),
panel.grid.minor = element_line(color = 'green', size = 2))

Here are a few of the most popular themes, based on the below themes can automatically change the background colors.

How to Overlay Plots in R-Quick Guide with Example » finnstats

p + theme_bw() #white background and grey gridlines
p + theme_minimal() #no background annotations
p + theme_classic() #axis lines but no gridlines
p + theme_linedraw() #black lines around the plot 
p + theme_light() # light gray lines and axis (more attention towards the data)
p + theme_void() #Empty theme
p + theme_dark() #Dark background designed to make colours pop out

Approach 1: Change Background Color in ggplot2

Let’s start by loading the package and visualizing the ToothGrowth dataset.

library(ggplot2)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)
 len supp dose
1 4.2  VC 0.5
2 11.5 VC 0.5
3 7.3  VC 0.5
4 5.8  VC 0.5
5 6.4  VC 0.5
6 10.0 VC 0.5
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot()
p

The following code can be used to alter the panel’s background color as well as the major and minor gridlines:

How to Plot Categorical Data in R-Quick Guide »

p + theme(panel.background = element_rect(fill = 'white', color = 'blue'),
panel.grid.major = element_line(color = 'green', linetype = 'dotted'),
panel.grid.minor = element_line(color = 'green', size = 2))

Approach 2: Change background colors based on Built-in Themes

The following code demonstrates how to use multiple built-in ggplot2 themes to change the background color of plots automatically.

Plot Differences in Two Measurements-Bland-Altman Plot in R »

p + theme_bw() #white background and grey gridlines
p + theme_minimal() #no background annotations
p + theme_classic() #axis lines but no gridlines
p + theme_linedraw() #black lines around the plot 

Plot Differences in Two Measurements-Bland-Altman Plot in R »

p + theme_light() # light gray lines and axis (more attention towards the data)
p + theme_void() #Empty theme
p + theme_dark() #Dark background designed to make colours pop out

The following R code shows how to change the background and grid lines of the plot panel

Dot Plots in R-Strip Charts for Small Sample Size »

p + theme(
panel.background = element_rect(fill = "lightcyan",
                                  colour = "lightcyan",
  panel.grid.major = element_line(size = 0.5, linetype = 'solid',
                                  colour = "white"),
  panel.grid.minor = element_line(size = 0.25, linetype = 'solid',
                                  colour = "white"))
p + theme(plot.background = element_rect(fill = "lightpink")) #Change the plot background-color

Adding text labels to ggplot2 Bar Chart »

The post How to Change Background Color in ggplot2 appeared first on finnstats.

To leave a comment for the author, please follow the link and comment on their blog: finnstats ».

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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)