Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • Remove Rows from the data frame in R
    Remove Rows from the data frame in R R
  • How to Turn Off Scientific Notation in R
    How to Turn Off Scientific Notation in R? R
  • Is Data Science a Dying Profession
    Is Data Science a Dying Profession? R
  • How to Use “not in” operator in Filter
    How to Use “not in” operator in Filter R
  • How to Use Spread Function in R
    How to Use Spread Function in R?-tidyr Part1 R
  • How to Label Outliers in Boxplots in ggplot2
    How to Label Outliers in Boxplots in ggplot2? R
  • R Percentage by Group Calculation
    R Percentage by Group Calculation R
  • The Uniform Distribution in R
    The Uniform Distribution in R R
Change ggplot2 Theme Color in R

Change ggplot2 Theme Color in R ggthemr Package

Posted on May 15May 15 By Jim No Comments on Change ggplot2 Theme Color in R ggthemr Package
Tweet
Share
Share
Pin

 In this article, you’ll learn how to change the colors of ggplot2 visuals in the R programming language by utilizing different themes.

Let’s start by making some sample data in R.

set.seed(123)                            
df <- data.frame(x = rnorm(100),
                   y = rnorm(100),
                   group = paste0("group_", LETTERS[1:5]))
head(df)                                      
        x          y   group
1  0.7842588 -0.3663221 group_A
2  1.2604525 -0.4016048 group_B
3  1.0959772  0.5219341 group_C
4 -2.4218639  1.1077222 group_D
5  0.7651984 -0.7216053 group_E
6 -0.5019940  1.3688690 group_A

The head of our sample data is visible above, as well as the fact that our data is made up of three variables. x and y are numerical variables, while the group is a character variable.

How to make a rounded corner bar plot in R? – Data Science Tutorial

To use the ggplot2 package to visualize our data, we must first install and import it into R.

install.packages("ggplot2")                     
library("ggplot2")                              

Then, using the R syntax below, we can make a ggplot2 scatterplot of our data.

plot<- ggplot(df, aes(x, y, col = group)) +
  geom_point()
plot

By using the preceding R syntax, we were able to produce a ggplot2 plot with default color parameters, as seen in Figure 1.

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

The following R code shows how to change the colors of our graph using the ggplot2 package’s predefined themes.

We can utilize the several pre-defined theme functions offered by the ggplot2 package for this task, as illustrated below.

plot_bw <- plot + theme_bw() + ggtitle("theme_bw()")
plot_classic <- plot + theme_classic() + ggtitle("theme_classic()")
plot_dark <- plot + theme_dark() + ggtitle("theme_dark()")

How to perform the MANOVA test in R? – Data Science Tutorials

plot_gray <- plot + theme_gray() + ggtitle("theme_gray()")
plot_linedraw <- plot + theme_linedraw() + ggtitle("theme_linedraw()")
plot_light <- plot + theme_light() + ggtitle("theme_light()")
plot_minimal <- plot + theme_minimal() + ggtitle("theme_minimal()")
plot_test <- plot + theme_test() + ggtitle("theme_test()")

Classification Problem in Machine Learning »

plot_void <- plot + theme_void() + ggtitle("theme_void()")

The previous R code generated nine ggplot2 charts, each with a unique theme design.

The patchwork package can then be used to create a comparison grid of these plots. We’ll need to install and load the patchwork package first.

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

Now we can arrange our nine plots in a grid as seen below:

(plot_bw + plot_classic + plot_dark) /
  (plot_gray + plot_linedraw + plot_light) /
  (plot_minimal + plot_test + plot_void)

The previous diagram depicts the many themes and their various color schemes.

As you can see, the majority of these themes are rather straightforward. Let’s make our plots more colorful!

Using the ggthemr package, change the color scheme of a ggplot2 plot.

The ggthemr package by Ciarán Tobin, which is now maintained by Mikata Project, is a powerful tool for designing, styling, and coloring ggplot2 plots.

The following R code can be used to install and load the package.

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

The ggthemr function can now be used to modify the ggplot2 theme layout. The R algorithms and graphical outputs below show how to change the theme choices of our example plot to suit other styles.

Intro to Tensorflow-Machine Learning with TensorFlow »

ggthemr("flat")
plot
ggthemr("flat dark")
plot
ggthemr("camouflage")
plot
ggthemr("chalk")
plot
ggthemr("copper")
plot

ggthemr(“dust”)

plot

ggthemr("earth")
plot
ggthemr("fresh")
plot
ggthemr("grape")
plot
ggthemr("grass")
plot
ggthemr("greyscale")
plot
ggthemr("light")
plot
ggthemr("lilac")
plot
ggthemr("pale")
plot
ggthemr("sea")
plot
ggthemr("sky")
plot

ggthemr("solarized")
plot

As you can see, the ggthemr package comes with a wide range of pre-defined themes.

You can also manually develop your own ggplot2 theme if you want complete control over your ggplot2 themes. More information about custom themes may be found here.

Data Mining and Knowledge Discovery »

Tweet
Share
Share
Pin
R Tags:ggplot2, theme

Post navigation

Previous Post: Best Books to Learn R Programming
Next Post: How to draw heatmap in r: Quick and Easy way

Related Posts

  • How to Label Outliers in Boxplots in ggplot2
    How to Label Outliers in Boxplots in ggplot2? R
  • How to Create Summary Tables in R
    How to Create Summary Tables in R R
  • How to add columns to a data frame in R
    How to add columns to a data frame in R R
  • How to compare the performance of different algorithms in R
    How to compare the performance of different algorithms in R? R
  • Credit Card Fraud detection in R
    Credit Card Fraud Detection in R R
  • How to Use Spread Function in R
    How to Use Spread Function in R?-tidyr Part1 R

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Defensive Programming Strategies in R
  • Plot categorical data in R
  • Top Data Modeling Tools for 2023
  • Ogive Graph in R
  • Is R or Python Better for Data Science in Bangalore

Check your inbox or spam folder to confirm your subscription.

  • Data Scientist Career Path Map in Finance
  • Is Python the ideal language for machine learning
  • Convert character string to name class object
  • How to play sound at end of R Script
  • Pattern Searching in R
  • How to Replace Inf Values with NA in R
    How to Replace Inf Values with NA in R R
  • pheatmap function in R
    The pheatmap function in R R
  • Ad Hoc Analysis
    What is Ad Hoc Analysis? Statistics
  • How to move from Junior Data Scientist
    How to move from Junior Data Scientist Machine Learning
  • Making games in R- Nara and eventloop Game Changers
    Making games in R- Nara and eventloop Game Changers Machine Learning
  • How to create a heatmap in R
    How to create a heatmap in R R
  • learn Hadoop for Data Science
    Learn Hadoop for Data Science Machine Learning
  • How to create Sankey plot in R
    How to create a Sankey plot in R? R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme