Skip to content

Data Science Tutorials

For Data Science Learners

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Contact
  • About Us
  • Toggle search form
  • Mastering the table() Function in R R
  • Creating a Histogram of Two Variables in R R
  • Arrange Data by Month in R
    Arrange Data by Month in R with example R
  • How to Compare Two Lists in Excel Using VLOOKUP
    How to Compare Two Lists in Excel Using VLOOKUP Excel
  • Jarque-Bera Test in R
    Jarque-Bera Test in R With Examples R
  • How to test the significance of a mediation effect
    How to test the significance of a mediation effect R
  • AI in Delivery Management
    AI in Delivery Management Machine Learning
  • Find the Maximum Value by Group in R
    Find the Maximum Value by Group in R R
Best GGPlot Themes

Best GGPlot Themes You Should Know

Posted on April 26April 30 By Admin No Comments on Best GGPlot Themes You Should Know

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")
  )
R

Post navigation

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

Related Posts

  • How to Use Bold Font in
    How to Use Bold Font in R with Examples R
  • How to Specify Histogram Breaks 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
  • Changing the Font Size in Base R Plots
    Changing the Font Size in Base R Plots R
  • How to Use Spread Function in R
    How to Use Spread Function in R?-tidyr Part1 R
  • Extract columns of data frame in R R

Leave a Reply Cancel reply

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

  • 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
  • Add Footnote to 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 Become a Business Intelligence Analyst
    How To Become a Business Intelligence Analyst Course
  • Ogive Graph in R
    Ogive Graph in R R
  • How to Use Spread Function in R
    How to Use Spread Function in R?-tidyr Part1 R
  • How to Calculate Ratios in R
    How to Calculate Ratios in R R
  • KPSS test in R
    KPSS Test in R With Examples R
  • How to create summary table in R
    How to create summary table in R R
  • How to Find the Size of a Data Frame in R R
  • Arrange the rows in a specific sequence in R
    Arrange the rows in a specific sequence in R R

Privacy Policy

Copyright © 2024 Data Science Tutorials.

Powered by PressBook News WordPress theme