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:
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.