Skip to content

Data Science Tutorials

For Data Science Learners

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Contact
  • About Us
  • Toggle search form
  • Comparison between Statistics and Luck
    Lottery Prediction-Comparison between Statistics and Luck Machine Learning
  • pheatmap function in R
    The pheatmap function in R R
  • How to Join Data Frames for different column names in R
    How to Join Data Frames for different column names in R R
  • Replace first match in R R
  • Dynamic data visualizations in R
    Dynamic data visualizations in R R
  • Best Books on Data Science with Python
    Best Books on Data Science with Python Course
  • Extract values from vector in R: dplyr R
  • glm function in R
    glm function in r-Generalized Linear Models R
Change ggplot2 Theme Color in R

Change ggplot2 Theme Color in R ggthemr Package

Posted on May 15May 15 By Admin No Comments on Change ggplot2 Theme Color in R ggthemr Package

 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 »

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

  • Group By Maximum in R
    Group By Maximum in R R
  • What Is the Best Way to Filter by Date in R
    What Is the Best Way to Filter by Date in R? R
  • How to Perform Bootstrapping in R
    How to Perform Bootstrapping in R R
  • How to Calculate Lag by Group in R
    How to Calculate Lag by Group in R? R
  • How to Create an Interaction Plot in R
    How to Create an Interaction Plot in R? R
  • Mastering R Programming for Data Science: Tips and Tricks R

Leave a Reply Cancel reply

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

  • Mastering R Programming for Data Science: Tips and Tricks
  • R-Change Number of Bins in Histogram
  • How to Specify Histogram Breaks in R
  • Creating a Histogram of Two Variables in R
  • Adding Subtitles in ggplot2
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy

https://www.r-bloggers.com

  • YouTube
  • Twitter
  • Facebook
  • Course
  • Excel
  • Machine Learning
  • Opensesame
  • R
  • Statistics

Check your inbox or spam folder to confirm your subscription.

  • how to create a hexbins chart in R
    How to create a hexbin chart in R R
  • Calculate the P-Value from Chi-Square Statistic in R
    Calculate the P-Value from Chi-Square Statistic in R R
  • Comparison between Statistics and Luck
    Lottery Prediction-Comparison between Statistics and Luck Machine Learning
  • OLS Regression in R
    OLS Regression in R R
  • Convert characters to time in R R
  • Convert a continuous variable to a categorical in R R
  • Solving Systems of Equations in R R
  • How to choose optimal number of epochs in R
    How to choose optimal number of epochs in R Machine Learning

Privacy Policy

Copyright © 2024 Data Science Tutorials.

Powered by PressBook News WordPress theme