Open In App

Change Color Scheme of ggplot2 Plot Using ggthemr Package in R

Last Updated : 20 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The open-source tool ggplot2 in R is used for statistical data visualization. To make the plots made by ggplot2 more appealing and to apply colors, different designs, and styling ggthemr Package in R is a great help.

ggthemr Package in R Programming Language is developed by Ciarán Tobin and maintained by Mikata Project.

The idea behind this package is to set the theme once so that there’s no need to update the styling again and again.

The theme has several parts:

  • Spacing around the plot and between elements
  • The color palette for the background, axes, gridlines, text, etc.
  • Text size
  • Layout of axes lines and gridlines

Installing the ggthemr Package in R

To use this package, we need to install it using:

R




install.packages("ggthemr")
library(ggthemr)


Output:

ggthemr Package in R

On some versions of R there might be a warning like this.

If you also see the same warning, you need to install the different version of the package using.

R




install.packages("remotes")
library(remotes)
 
# Installing from GitHub
install_github("cttobin/ggthemr")
 
# Loading the package
library(ggthemr)


Output:

ggthemr Package in R

The ‘remotes’ package is used to fetch packages from various sources like GitHub.

  • Now you might see this.
  • Just press the Enter key to skip the updates and load the package.

Using ggthemr Package in R with ‘ggplot2’

Now that the package is installed and loaded, let’s use it with the ggplot2 for changing the color scheme.

In ggthemr we have these preset color palette to choose from:

flat, flat dark, dust, light, earth, fresh, chalk, lilac, carrot, pale, copper, grape, greyscale, sky, solarized, grass, sea, camouflage.

To do that just use any of the above themes with ggthemr:

R




# applying the lilac theme
ggthemr("lilac")
 
# load the 'ggplot2' package
library(ggplot2)
 
# Create some random data
set.seed(123)
x <- rnorm(100)
y <- rnorm(100)
 
# Create a scatter plot
ggplot() +
geom_point(aes(x = x, y = y))


Output:

ggthemr Package in R

From now onwards any plot created using ggplot2 will have the same theme ‘lilac’.

For resetting it to default ggplot2 settings, use:

R




# reset to the default values
ggthemr_reset()


‘flat dark’ theme in ggthemr Package in R

R




library(ggthemr)
library(ggplot2)
 
ggthemr("flat dark")
 
# example data
set.seed(123)
x <- rnorm(50)
y <- 0.5 * x + rnorm(50, sd = 0.2)
df <- data.frame(x = x, y = y)
 
# scatter plot
ggplot(df, aes(x = x, y = y)) +
geom_point(size = 3) +
geom_smooth(method = "lm", se = FALSE, size = 1) +
labs(x = "X", y = "Y", title = "Scatter Plot with a line")


Output:

ggthemr Package in R

Custom Palettes in ggthemr Package in R

define_palette( ) can be used to make customized theme and then can be passed to ggthemr similar to above palettes.

R




# load required packages
library(ggplot2)
library(ggthemr)
 
# define random colors
random_colors <- swatch <- c("#FFB6C1", "#FFD700", "#9370DB", "#FFA07A", "#00BFFF")
 
# define custom palette using 'define_palette()'
custom_palette <- define_palette(
swatch = random_colors,     # takes a vector of color codes
gradient = c(lower = "#FFB6C1", upper = "#9370DB")    
)
 
ggthemr(custom_palette)    
 
# example data
set.seed(123)
x <- rnorm(50)
y <- 0.5 * x + rnorm(50, sd = 0.2)
df <- data.frame(x = x, y = y)
 
# Create the scatter plot
ggplot(df, aes(x = x, y = y)) +
geom_point(size = 3) +
geom_smooth(method = "lm", se = FALSE, size = 1) +
labs(x = "X", y = "Y", title = "Scatter Plot with a line")


Output:

Custom color palette in ggthemr

Here with the use of define_palette( ) we are using colors from defined random_colors in swatch which takes a vector of color codes. Then gradient takes two values- lower and upper and is used to specify gradient of colors for any required continuous variables in the plot if available.

Then simply take sample data and plot a graph with ggplot2.

Sky Theme in ggthemr Package in R

R




library(ggthemr)
ggthemr("sky")
ggplot(mtcars, aes(x = mpg, y = disp, color = factor(cyl))) +
  geom_point() +
  ggtitle("sky Theme") +
  xlab("Miles per Gallon") +
  ylab("Displacement")


Output:

gh

ggthemr Package in R

let’s use the mtcars dataset in the ggplot2 package to make scatter plot.

R




# Geometric layer
ggplot(data = mtcars,
    aes(x = hp, y = mpg, col = disp)) + geom_point()


Output:

gh

ggthemr Package in R

Adding Size, color, and shape and then plotting .

R




# Adding size
ggplot(data = mtcars,
    aes(x = hp, y = mpg, size = disp)) + geom_point()


Output:

gh

ggthemr Package in R

Adding color and shape in ggthemr Package in R

R




# Adding color and shape
ggplot(data = mtcars,
    aes(x = hp, y = mpg, col = factor(cyl),
                        shape = factor(am))) +
geom_point()


Output:

ggthemr Package in R

Conclusion:

In this article, we have seen ways to change color scheme of plots that are plotted with ggplot2 using the package ggthemr. In this way more advanced plots plotted using ggplot2 can look good aesthetically and they are easy for understanding too!



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads