Themes in ggplot2
In this article, we are going to learn the theme in the R system which is used to change the themes of the plot. theme in R is used when one wants to customize the fonts, ticks, and backgrounds of a graph. In R Programming Language the theming system is mainly composed of four components.
- Theme Elements – These are the non-data elements of a graph like a title, ticks, etc.
- Element Function – The element function is used to update and modify the elements of the graph.
- theme() Function – Used to upgrade to the new theme by overriding the existing theme of the plot.
- complete themes – These are used when one wants to design the background of the plot.
Let’s discuss the most popular theme-based packages ggthemes, ggdark, hrbrthemes.
ggthemes
Install and load the required packages.
R
# Install required package install.packages ( "ggplot2" ) install.packages ( "ggthemes" ) # Load the installed packages library (ggplot2) library (ggthemes) |
Lets us parse through the most used theme function of ggthemes package as follows.
theme_base() – it is similar to the default settings of the ‘base’ R graphics.
Syntax:
theme_base(base_size = 16, base_family = “”)
where,
- base_size – base font size, given in pts.(optional)
- base_family – base font family(optional)
R
# load the default iris dataset data (iris) # Plotting the density plot ggplot2:: ggplot (iris, aes (x = Sepal.Width,fill = Species)) + geom_density (alpha = 0.7) + ggthemes:: theme_base () + theme (legend.position = "top" ) |
Output:
theme_economist(): This theme in R based on the style of plots used by The Economist magazine.
R
# load the default iris dataset data (iris) # Plotting the density plot ggplot2:: ggplot (iris, aes (x = Sepal.Width,fill = Species)) + geom_density (alpha = 0.7) + ggthemes:: theme_economist () + theme (legend.position = "top" ) |
Output:
theme_fivethirtyeight()
: This theme in R based on the style of plots used by the websites.
R
# load the default iris dataset data (iris) # Plotting the density plot ggplot2:: ggplot (iris, aes (x = Sepal.Width,fill = Species)) + geom_density (alpha = 0.7) + ggthemes:: theme_fivethirtyeight () + theme (legend.position = "top" ) |
Output:
theme_solarized()
: This theme in R based on the Solarized color palette.
R
# load the default iris dataset data (iris) # Plotting the density plot ggplot2:: ggplot (iris, aes (x = Sepal.Width,fill = Species)) + geom_density (alpha = 0.7) + ggthemes:: theme_solarized () + theme (legend.position = "top" ) |
Output:
theme_wsj()
: This theme in R based on the style of plots used by The Wall Street Journal.
R
# load the default iris dataset data (iris) # Plotting the density plot ggplot2:: ggplot (iris, aes (x = Sepal.Width,fill = Species)) + geom_density (alpha = 0.7) + ggthemes:: theme_wsj () + theme (legend.position = "top" ) |
Output:
theme_excel()
: This theme in R based on the default color scheme used in Microsoft Excel.
R
# load the default iris dataset data (iris) # Plotting the density plot ggplot2:: ggplot (iris, aes (x = Sepal.Width,fill = Species)) + geom_density (alpha = 0.7) + ggthemes:: theme_excel () + theme (legend.position = "top" ) |
Output:
Bar Plot using ggplot theme_minimal()
R
# Load the required packages library (ggplot2) # Function to create and display bar plot with a specified theme create_bar_plot <- function (theme) { ggplot (iris, aes (x = Sepal.Width, y = Petal.Width, fill = Species)) + geom_bar (stat = "identity" ) + theme + theme (legend.position = "top" ) + ggtitle ( paste ( "BarPlot using" , deparse ( substitute (theme)))) } # Default theme create_bar_plot ( theme_minimal ()) |
Output:
Dark theme in ggplot2
R
# Dark theme create_bar_plot ( theme_void () + theme (panel.background = element_rect (fill = "black" ), plot.background = element_rect (fill = "black" ), text = element_text (color = "white" ), legend.position = "top" )) |
Output:
Economical theme in R
R
# Economical theme create_bar_plot ( theme_economist ()) |
Output:
ggdark
Simply as the name indicates ggdark is a package that is used to produce dark theme-based plots. By applying these functions the colors of the plot are inverted to ensure the plot is visible.dark_mode() is a function present in ggdark package to activate the dark mode.
Syntax:
dark_mode(.theme = theme_get(), verbose = TRUE, force_geom_invert = FALSE)
Where,
- theme – ggplot2 theme object
- verbose – print messages (default: TRUE)
- force_geom_invert – Force the inversion of geom defaults for fill and color/colour (default: FALSE)
R
# Install required package install.packages ( "ggdark" ) # Load the installed packages library (ggdark) # load the default iris dataset data (iris) # Plotting the Points(Dot) Plot plot<-ggplot2:: ggplot (iris, aes (x = Sepal.Width, y = Petal.Width, color = Species)) + geom_point () + ggthemes:: theme_fivethirtyeight () + theme (legend.position = "top" )+ ggtitle ( "DotPlot using ggthemes" ) plot<-plot+ggdark:: dark_mode () plot |
Output:
Hrbrthemes
The hrbrthemes package is specifically meant to provide typographic themes which are built on top of the ggplot2 package. There are many functions included in it to produce as follows
- ipsum_pal() – Used to build a qualitative color palette
- gg_check() – used to check the spelling of plot labels
- theme_ipsum_rc() – used to produce precise and pristine plots with optimized defaults on typography
Syntax :
theme_ipsum_rc(base_family, base_size, plot_title, axis_title, plot_margin, grid, axis)
Where,
- base_family, base_size – base family and it’s font size is specified here
- plot_title – plot title family is specified here
- axis_title – axis title font family, face and size is specified here
- plot_margin – margins of the plot is specified here
- grid – panel grid is specified here
- axis – used to add x or y axes
R
# install required packages install.packages ( "hrbrthemes" ) #load the installed packages library (hrbrthemes) # seminal scatterplot ggplot (mtcars, aes (mpg, wt)) + geom_point ( aes (colour=mpg)) + labs (x= "Fuel efficiency (mpg)" , y= "Weight (tons)" , title= "Seminal Scatterplot using hrbrthemes" ) + theme_ipsum_rc () |
Output:
Contact Us