How to use ggthemr Package in R with ‘ggplot2’ In R Language
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:
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:
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:
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:
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:
Adding Size, color, and shape and then plotting .
R
# Adding size ggplot (data = mtcars, aes (x = hp, y = mpg, size = disp)) + geom_point () |
Output:
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:
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!
Change Color Scheme of ggplot2 Plot Using ggthemr Package in R
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