Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • Top Reasons To Learn R
    Top Reasons To Learn R in 2023 Machine Learning
  • How to Label Outliers in Boxplots in ggplot2
    How to Label Outliers in Boxplots in ggplot2? R
  • How to compare the performance of different algorithms in R
    How to compare the performance of different algorithms in R? R
  • How to Calculate Ratios in R
    How to Calculate Ratios in R R
  • Subsetting with multiple conditions in R
    Subsetting with multiple conditions in R R
  • Load Multiple Packages in R
    Load Multiple Packages in R R
  • Hypothesis Testing Examples
    Hypothesis Testing Examples-Quick Overview Statistics
  • How to Use Italic Font in R
    How to Use Italic Font in R R
Best GGPlot Themes

Best GGPlot Themes You Should Know

Posted on April 26April 30 By Jim No Comments on Best GGPlot Themes You Should Know
Tweet
Share
Share
Pin

Best GGPlot Themes, This tutorial covers everything you need to know about the greatest ggplot2 themes, including.

How to use the built-in ggplot2 themes to change the appearance of plots.

How to change the appearance of plots using the ggthemes library’s predefined themes.

How to change the plot panel background and gridlines, as well as other theme elements.

Best GGPlot Themes

How to Change the Look of a Plot Using Built-in ggplot2 Themes

We’ll use the built-in R dataset iris for each of the following examples.

look at the first six rows of the iris dataset

head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

We’ll start by loading the ggplot2 package and making a scatterplot of Sepal. On the x-axis, length and Sepal. On the y-axis, width is colored by species.

Load the ggplot2 library

library(ggplot2)

Now we can create a scatterplot

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +  geom_point()

Following that, we’ll show how each of the built-in ggplot2 themes affects the plot’s appearance.

Theme_gray

With a grey background and white gridlines, this is the default theme.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +  geom_point() +  theme_gray()

theme_bw

The color scheme is black and white.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +  geom_point() +  theme_bw()

theme_linedraw

Only black lines of varying widths on white backgrounds make up this theme.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +  geom_point() +  theme_linedraw()

theme_light

A theme similar to theme line draw, but with grey lines and axes to help the data stand out.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +  geom_point() +  theme_light()

theme_dark

Theme with a dark background, comparable to theme light. A handy theme for highlighting fine colored lines.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +  geom_point() +  theme_dark()

theme_minimal

There are no background annotations on this topic.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +  geom_point() +  theme_minimal()

theme_classic

There are no gridlines in this theme.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +  geom_point() +  theme_classic()

theme_void

A theme with no content.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +  geom_point() +  theme_void()

How to change the appearance of plots using the ggthemes library’s predefined themes

To change the aesthetics of plots, we can utilize the predefined themes from the ggthemes library in addition to the built-in ggplot2 themes.

We’ll start by loading the ggthemes library.

library(ggthemes)

Next, we’ll look at a few examples of how predefined themes can be used to change the aesthetics of plots:

theme_wsj

A theme from the Wall Street Journal.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +  geom_point() +  theme_wsj()

theme_tufte

A minimalist theme based on statistician Edward Tufte’s work.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +  geom_point() +  theme_tufte()

theme_solarized

Colors from the solarized palette are used in this theme.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +  geom_point() +  theme_solarized()

To utilize a dark background on the plot, we may alternatively use the option light = FALSE:

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +  geom_point() +  theme_solarized(light = FALSE)

theme_gdocs

Google Docs Chart defaults are used in this theme.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +  geom_point() +  theme_gdocs()

theme_fivethirtyeight

Theme based on plots from fivethirtyeight.com.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +  geom_point() +  theme_fivethirtyeight()

theme_economist

The Economist provided inspiration for this theme.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +  geom_point() +  theme_economist()

How to Change Specific Plot Components

To alter the plot panel background colour, we can use the theme() and element_rect() functions:

theme(panel.background = element_rect(fill, color, size))
fill: fill color for rectangle
color: border color
size: border size

To adjust the size and appearance of the gridlines, we can utilize the element_line() function.

theme(panel.grid.major = element_line(color, size, linetype),
      panel.grid.minor = element_line(color, size, linetype))
color: border color
size: border size
linetype: line type (“blank”, “solid”, “dashed”, “dotted”, “dotdash”, “longdash”, “twodash”)

The code below shows how to get rid of the plot panel borders and gridlines.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme(panel.border = element_blank(),
  panel.grid.major = element_blank(),
  panel.grid.minor = element_blank())

The following code shows how to change the background and gridlines of the plot panel.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme(
    panel.background = element_rect(fill = "powderblue",
    color = "powderblue",
    size = 0.5, linetype = "solid"),
    panel.grid.major = element_line(size = 0.5, linetype = 'solid', color = "white"),
    panel.grid.minor = element_line(size = 0.25, linetype = 'solid', color = "white")
  )
Tweet
Share
Share
Pin
R

Post navigation

Previous Post: Descriptive statistics vs Inferential statistics: Guide
Next Post: Checking Missing Values in R

Related Posts

  • How to add columns to a data frame in R
    How to add columns to a data frame in R R
  • display the last value of each line in ggplot
    How to add labels at the end of each line in ggplot2? R
  • How to Standardize Data in R
    How to Standardize Data in R? R
  • How to create a ggalluvial plot in r
    How to create a ggalluvial plot in R? R
  • How to apply a transformation to multiple columns in R?
    How to apply a transformation to multiple columns in R? R
  • Radar plot in R
    How to create Radar Plot in R-ggradar 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
  • What is the best way to filter by row number in R?
    What is the best way to filter by row number in R? R
  • Check whether any values of a logical vector are TRUE
    Check whether any values of a logical vector are TRUE R
  • Radar plot in R
    How to create Radar Plot in R-ggradar R
  • A Side-by-Side Boxplot in R
    A Side-by-Side Boxplot in R: How to Do It R
  • R Percentage by Group Calculation
    R Percentage by Group Calculation R
  • Two-Way ANOVA Example in R
    Two-Way ANOVA Example in R-Quick Guide R
  • Bind together two data frames by their rows or columns in R
    Bind together two data frames by their rows or columns in R R
  • How to Turn Off Scientific Notation in R
    How to Turn Off Scientific Notation in R? R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme