Themes and background colors in ggplot2 in R
In this article, we will discuss how to change the look of a plot theme (background color, panel background color, and gridlines) using the R Programming Language and ggplot2 package.
Themes in ggplot2 package
The ggplot2 package in R Language has 8 built-in themes. To use these themes we just need to add that theme function to the plot. These functions change the look and feel of a plot by manipulating three key aspects of the plot that are background color, panel background color, and gridlines
Syntax: plot + theme_function()
Here, are the following 8 prebuilt themes in the ggplot2 package in R Language:
- theme_grey(): Creates a gray background color and white grid lines with no border.
- theme_bw(): Creates a white background and gray grid lines with a black border.
- theme_linedraw(): Creates a white background color and black grid lines with a thick black border.
- theme_light(): Creates a white background color and light gray grid lines with a light gray border.
- theme_dark(): Creates a dark gray background color and gray grid lines with no border.
- theme_minimal(): Creates a white background color and has no grid lines with no border.
- theme_classic(): Creates a white background color and no grid lines. It only has black axis lines.
- theme_void(): Creates a white background with no border, gridlines, or axis lines.
Example:
A Simple bar plot with all 8 themes combined using the grid.arrange function of gridExtra package.
R
# Create sample data set.seed (5642) sample_data <- data.frame (name = c ( "Geek1" , "Geek2" , "Geek3" , "Geek4" , "Geeek5" ) , value = c (31,12,15,28,45)) # Load ggplot2 package and gridExtra library ( "ggplot2" ) library ( "gridExtra" ) # Create bar plot using ggplot() function basic_plot <- ggplot (sample_data, aes (name,value)) + geom_bar (stat = "identity" ) # add theme function to plot all 8 themes theme_grey <- basic_plot + ggtitle ( "theme_grey" )+ theme_grey () theme_bw <- basic_plot+ ggtitle ( "theme_bw" )+ theme_bw () theme_linedraw <- basic_plot+ ggtitle ( "theme_linedraw" )+ theme_linedraw () theme_light <- basic_plot+ ggtitle ( "theme_light" )+ theme_light () theme_dark <- basic_plot+ ggtitle ( "dark" )+ theme_dark () theme_minimal <-basic_plot+ ggtitle ( "minimal" )+ theme_minimal () theme_classic <- basic_plot+ ggtitle ( "classic" )+ theme_classic () theme_void <- basic_plot+ ggtitle ( "theme_void" )+ theme_void () # arrange all the plots with different themes together grid.arrange (theme_grey, theme_bw, theme_linedraw, theme_light, theme_dark, theme_minimal, theme_classic, theme_void, ncol = 4) |
Output:
Background Colors in ggplot
To create a manual theme for users’ liking, we can change the background color of the panel as well as plot using the panel.background and plot.background argument of the theme function of the ggplot2 package.
Syntax: plot + theme(plot.background = element_rect( fill ) , panel.background = element_rect( fill ) )
Example:
Here, is a bar plot with a green panel background and yellow plot background color.
R
# Create sample data set.seed (5642) sample_data <- data.frame (name = c ( "Geek1" , "Geek2" , "Geek3" , "Geek4" , "Geeek5" ) , value = c (31,12,15,28,45)) # Load ggplot2 package library ( "ggplot2" ) # Create bar plot using ggplot() function # theme function is used to change the # background colors of plot ggplot (sample_data, aes (name,value)) + geom_bar (stat = "identity" )+ theme (plot.background = element_rect (fill = "yellow" ), panel.background = element_rect (fill = "green" )) |
Output:
Contact Us