1 Goal


The goal of this tutorial is to show a sample of complete themes that can be used to customize plots in order to use them in presentations or reports. We can change the colours of the plot by defining a theme rather than changing everything by hand.


2 Drawing with ggplot


# First we load the libraries
library(ggplot2)

# In this example we will use the open repository of plants classification Iris. 
data("iris")
str(iris)
## 'data.frame':    150 obs. of  5 variables:
##  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
# Let's plot the petal length vs petal width
ggplot() + geom_point(data = iris, aes(x = Petal.Width, y = Petal.Length,color = Species))


3 Basic themes

3.1 Default theme


# The default theme of ggplot if the grey theme
# It can be called as theme_grey or theme_gray
ggplot() + geom_point(data = iris, aes(x = Petal.Width, y = Petal.Length,color = Species)) + theme_grey()


3.2 Black and white theme


# We can use a white and black and white theme that is more useful for presentations
# It can be called as theme_bw
ggplot() + geom_point(data = iris, aes(x = Petal.Width, y = Petal.Length,color = Species)) + theme_bw()


3.3 Light theme


# We can use a lighter theme that has thinner border lines
# It can be called as theme_light
ggplot() + geom_point(data = iris, aes(x = Petal.Width, y = Petal.Length,color = Species)) + theme_light()


3.4 Dark theme


# We can use a darker theme that has dark background
# It can be called as theme_dark
ggplot() + geom_point(data = iris, aes(x = Petal.Width, y = Petal.Length,color = Species)) + theme_dark()


3.5 Empty theme


# We can use a theme that removes all elements from the plots
# It can be called as theme_void
ggplot() + geom_point(data = iris, aes(x = Petal.Width, y = Petal.Length,color = Species)) + theme_void()


4 Personalized theme


# We can define our own theme 
# Source: https://alstatr.blogspot.com.es/2015/02/r-how-to-layout-and-design-infographic.html
# We have added some small modifications to the theme

kobe_theme <- function() {
  theme(
    plot.background = element_rect(fill = "#E2E2E3", colour = "#E2E2E3"),
    panel.background = element_rect(fill = "#E2E2E3"),
    legend.key = element_rect(fill = "#E2E2E3"),
    legend.background = element_rect(fill = "#E2E2E3"),
    axis.text = element_text(colour = "#E7A922", family = "Impact"),
    plot.title = element_text(colour = "#552683", face = "bold", size = 18, vjust = 1, 
                              family = "Impact", hjust = 0.5),
    axis.title = element_text(colour = "#552683", face = "bold", size = 13, family = "Impact"),
    panel.grid.major.x = element_line(colour = "#E7A922"),
    panel.grid.minor.x = element_blank(),
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),
    strip.text = element_text(family = "Impact", colour = "white"),
    strip.background = element_rect(fill = "#E7A922"),
    axis.ticks = element_line(colour = "#E7A922")
  )
}


ggplot() + geom_point(data = iris, aes(x = Petal.Width, y = Petal.Length,color = Species)) + kobe_theme() +
  ggtitle("KOBE BRYANT THEME")


5 Conclusion


In this tutorial we have learnt how to define a default theme from ggplot as well as how to define our own theme.