Change Theme Color in ggplot2 Plot in R (Example) | Introduction to ggthemr Package

 

In this tutorial you’ll learn how to modify the colors of ggplot2 graphics using different themes in the R programming language.

Table of contents:

Let’s do this.

 

Example Data

Let’s first create some example data in R:

set.seed(2342435)                                # Create example data
data <- data.frame(x = rnorm(100),
                   y = rnorm(100),
                   group = paste0("group_", LETTERS[1:5]))
head(data)                                       # Print head of example data

 

table 1 data frame color themes ggplot2 plots

 

Table 1 shows the head of our example data – it is also shown that our data is made up of three variables. The variables x and y are numerical, and the variable group is a character.

To draw our data using the ggplot2 package, we first need to install and load the ggplot2 package to R:

install.packages("ggplot2")                      # Install ggplot2 package
library("ggplot2")                               # Load ggplot2

Next, we can create a ggplot2 scatterplot of our data using the R syntax below:

ggp <- ggplot(data, aes(x, y, col = group)) +    # Create ggplot2 plot with default colors
  geom_point()
ggp                                              # Draw ggplot2 plot

 

r graph figure 1 color themes ggplot2 plots

 

In Figure 1 it is shown that we have created a ggplot2 plot with default color specifications by running the previous R syntax.

Let’s change these colors!

 

Example 1: Change Color Scheme of ggplot2 Plot Using Default Themes

The following R code demonstrates how to use the default themes of the ggplot2 package to adjust the colors of our graphic.

For this task, we can use the different pre-defined theme functions that are provided by the ggplot2 package as shown below:

ggp_bw <- ggp + theme_bw() + ggtitle("theme_bw()") # Switch ggplot2 themes
ggp_classic <- ggp + theme_classic() + ggtitle("theme_classic()")
ggp_dark <- ggp + theme_dark() + ggtitle("theme_dark()")
ggp_gray <- ggp + theme_gray() + ggtitle("theme_gray()")
ggp_linedraw <- ggp + theme_linedraw() + ggtitle("theme_linedraw()")
ggp_light <- ggp + theme_light() + ggtitle("theme_light()")
ggp_minimal <- ggp + theme_minimal() + ggtitle("theme_minimal()")
ggp_test <- ggp + theme_test() + ggtitle("theme_test()")
ggp_void <- ggp + theme_void() + ggtitle("theme_void()")

The previous R code has created nine ggplot2 plots with different theme design.

Next, we can use the patchwork package to draw a comparison grid of these plots. For this, we first need to install and load the patchwork package:

install.packages("patchwork")
library("patchwork")

Now, we can draw a grid layout of our nine plots as shown below:

(ggp_bw + ggp_classic + ggp_dark) / # Draw layout of ggplot2 plots
  (ggp_gray + ggp_linedraw + ggp_light) / 
  (ggp_minimal + ggp_test + ggp_void)

 

r graph figure all plots color themes ggplot2 plots

 

The previously shown figure visualizes the different themes and their different color codings.

As you can see, most of these themes are relatively plain and simple. Let’s add some more colors to our plots!

 

Example 2: Change Color Scheme of ggplot2 Plot Using ggthemr Package

A powerful package when it comes to designs, styles, and colors of ggplot2 plots is the ggthemr package by Ciarán Tobin that is nowadays maintained by Mikata Project.

The package can be installed and loaded via the R code below:

library("devtools")
devtools::install_github("Mikata-Project/ggthemr")
library("ggthemr")

Now, we can apply the ggthemr function to change the ggplot2 theme layout. The following R codes and their graphical outputs demonstrate how to adjust the theme options of our example plot to different styles.

ggthemr("flat")
ggp

 

r graph figure 3 color themes ggplot2 plots

 

ggthemr("flat dark")
ggp

 

r graph figure 4 color themes ggplot2 plots

 

ggthemr("camouflage")
ggp

 

r graph figure 5 color themes ggplot2 plots

 

ggthemr("chalk")
ggp

 

r graph figure 6 color themes ggplot2 plots

 

ggthemr("copper")
ggp

 

r graph figure 7 color themes ggplot2 plots

 

ggthemr("dust")
ggp

 

r graph figure 8 color themes ggplot2 plots

 

ggthemr("earth")
ggp

 

r graph figure 9 color themes ggplot2 plots

 

ggthemr("fresh")
ggp

 

r graph figure 10 color themes ggplot2 plots

 

ggthemr("grape")
ggp

 

r graph figure 11 color themes ggplot2 plots

 

ggthemr("grass")
ggp

 

r graph figure 12 color themes ggplot2 plots

 

ggthemr("greyscale")
ggp

 

r graph figure 13 color themes ggplot2 plots

 

ggthemr("light")
ggp

 

r graph figure 14 color themes ggplot2 plots

 

ggthemr("lilac")
ggp

 

r graph figure 15 color themes ggplot2 plots

 

ggthemr("pale")
ggp

 

r graph figure 16 color themes ggplot2 plots

 

ggthemr("sea")
ggp

 

r graph figure 17 color themes ggplot2 plots

 

ggthemr("sky")
ggp

 

r graph figure 18 color themes ggplot2 plots

 

ggthemr("solarized")
ggp

 

r graph figure 19 color themes ggplot2 plots

 

As you have seen, the ggthemr package provides many different pre-defined themes.

In case you want to take over the full control of your ggplot2 themes, you may also create your own theme manually. You can find more information on custom themes here.

 

Video & Further Resources

I have recently released a video on my YouTube channel, which demonstrates the content of this post. You can find the video below:

 

The YouTube video will be added soon.

 

Below, you may also find tutorials on how to create different kinds of graphics such as scatterplots, density plots, barcharts, boxplots, histograms, and line plots using different ggplot2 themes:

Furthermore, you may read the other tutorials on this website.

 

Summary: You have learned in this tutorial how to change the colors of ggplot2 plots using different themes in the R programming language. Please let me know in the comments section, if you have any further questions. Furthermore, don’t forget to subscribe to my email newsletter in order to receive updates on new posts.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top