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

You may also like...

Leave a Reply

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

ten − two =