Change Color Scheme of ggplot2 Plot Using ggthemr Package in R
Last Updated :
24 Apr, 2025
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 ROn 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 RThe '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 RFrom 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 RCustom 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 ggthemrHere 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:
ggthemr Package in Rlet’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:
ggthemr Package in RAdding Size, color, and shape and then plotting .
R
# Adding size
ggplot(data = mtcars,
aes(x = hp, y = mpg, size = disp)) + geom_point()
Output:
ggthemr Package in RAdding 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 RConclusion:
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!
Similar Reads
Change Theme Color in ggplot2 Plot in R
A theme in ggplot2 is a collection of settings that control the non-data elements of the plot. These settings include things like background colors, grid lines, axis labels, and text sizes. we can use various theme-related functions to customize the appearance of your plots, including changing theme
4 min read
How to change Colors in ggplot2 Line Plot in R ?
A line graph is a chart that is used to display information in the form of series of data points. It utilizes points and lines to represent change over time. Line graphs are drawn by plotting different points on their X coordinates and Y coordinates, then by joining them together through a line from
2 min read
Pie and Donut chart on same plot in ggplot2 using R
Visualizing categorical data using pie and donut charts can communicate proportions and distributions effectively. While pie charts show data in a circular layout divided into slices, donut charts add a "hole" in the center, providing a different visual perspective. In this article, we'll explore ho
4 min read
How to Change the Color of Points for ggplot2 Scatterplot Using ColorBrewer in R
When visualizing data using scatter plots, coloring the points can help distinguish between categories or highlight certain patterns in the data. In this article, we will explore how to change the color of points in a ggplot2 scatterplot using RColorBrewer in R.Introduction to ColorBrewerRColorBrewe
4 min read
Change the Outline Color for Histogram Bars Using ggplot2 in R
In data visualization, customizing the appearance of a plot can greatly enhance its readability and presentation. One common customization when working with histograms is changing the outline color of the bars. By default, ggplot2 may not always add outlines, but you can easily modify this behavior
4 min read
Change Color of Range in ggplot2 Heatmap in R
A heatmap represents the connection between two properties of a dataframe as a color-coded tile. A heatmap generates a grid with several properties of the dataframe, showing the connection between the two properties at a time.Creating a Basic Heatmap in ggplot2Let us first create a regular heatmap w
3 min read
How to change plot area margins using ggplot2 in R?
In the R programming language, ggplot2 is a popular library for creating data visualizations. One of the key benefits of using ggplot2 is the ability to customize the appearance of plots in a variety of ways. In this article, we will explore some of the ways you can customize the appearance of your
3 min read
How to change the legend shape using ggplot2 in R?
In this article, we will discuss how to change only legend shape using ggplot2 in R programming language. Here ScatterPlot is used the same can be applied to any other plot. Syntax : sample(x, size, replace = TRUE) Parameters : x : either a vector of one or more values from which we want to choose t
3 min read
Remove grid and background from plot using ggplot2 in R
A plot by default is produced with a grid background and grayish colored background. In this article we will see, how a gird can be removed from a plot. We will use a line plot, for demonstration but the same can be employed for any other Visualization.To understand the difference better let us firs
1 min read
Create a Scatter Plot with Multiple Groups using ggplot2 in R
In this article, we will discuss how to create a scatter plot with multiple groups in R Programming Language. Geoms can be added to the plot to compute various graphical representations of the data in the plot (points, lines, bars). The geom_point() method is used to create scatter plots in R. The g
2 min read