Skip to content

Instantly share code, notes, and snippets.

@kelly-sovacool
Last active June 1, 2020 14:37
Show Gist options
  • Save kelly-sovacool/037397dbf0d43e869bb93b8bbd9a757a to your computer and use it in GitHub Desktop.
Save kelly-sovacool/037397dbf0d43e869bb93b8bbd9a757a to your computer and use it in GitHub Desktop.
Pat’s code club on ggplot themes

2020-05-04

Pat’s code club on ggplot themes

library(tidyverse)
library(ggthemes)

mtcars2 <- within(mtcars, {
    vs <- factor(vs, labels = c("V-shaped", "Straight"))
    am <- factor(am, labels = c("Automatic", "Manual"))
    cyl  <- factor(cyl)
    gear <- factor(gear)
})

p1 <- ggplot(mtcars2) +
    geom_point(aes(x = wt, y = mpg, colour = gear)) +
    labs(title = "Fuel economy declines as weight increases",
             subtitle = "(1973-74)",
             caption = "Data from the 1974 Motor Trend US magazine.",
             tag = "Figure 1",
             x = "Weight (1000 lbs)",
             y = "Fuel economy (mpg)",
             colour = "Gears")

p1

p1 + theme_gray() # the default

p1 + theme_bw()

p1 + theme_linedraw()

p1 + theme_light()

p1 + theme_dark()

p1 + theme_minimal()

p1 + theme_classic()

p1 + theme_void()

p1 + theme_economist() # https://yutannihilation.github.io/allYourFigureAreBelongToUs/ggthemes/

p1 + theme_excel()

p1 + theme_fivethirtyeight()

(checkout the xkcd theme package!)

You can manipulate a lot in a figure withough needing to go deep into the theme options

p1 + theme_classic()

p1 + theme_classic(base_size = 20) #a number, default = 11

p1 + theme_classic(base_family = "mono") #a font family, default = sans (serif, mono, symbol); see extrafont package

p1 + theme_classic(base_line_size = 2) #a number, size for line elements, default = base_size/22

p2 <- p1 + facet_grid(vs ~ am) 
p2 + theme_classic()

p2 + theme_classic(base_rect_size = 2) #a number, size for rectangular elements, default = base_size/22

p2 + theme_classic(base_rect_size = 0) #a number, size for rectangular elements, default = base_size/22

theme function - allows you to manipulate everything on the plot

?theme # look at the "Arguments" section for and easier view of the various things you can manipulate

# Generally a heirarchy to how they're named (they are listed alphabetically not by level of figure)
# * the first word indicates what gets manipulated (e.g. plot, legend, etc)
# * the second word indicates what about the first word gets manipulated (e.g. plot.background)
# * the third word indicateds a finer level of specificity (e.g. legend.box.background)
# * sometimes the third word overwrites what happens with the second word (e.g. axis.title vs axis.title.x)

# When setting the values for the theme parameters for some arguments they take
# a character or logical value. More frequently they take one of five functions 
# 
# margin(t = 0, r = 0, b = 0, l = 0, unit = "pt") #points is default
# 
# element_blank()
# 
# element_rect(fill = NULL, colour = NULL, size = NULL,
#                        linetype = NULL, color = NULL, inherit.blank = FALSE)
# 
# element_line(colour = NULL, size = NULL, linetype = NULL,
#                        lineend = NULL, color = NULL, arrow = NULL,
#                        inherit.blank = FALSE)
# 
# element_text(family = NULL, face = NULL, colour = NULL,
#                        size = NULL, hjust = NULL, vjust = NULL, angle = NULL,
#                        lineheight = NULL, color = NULL, margin = NULL, debug = NULL,
#                        inherit.blank = FALSE)
# 
# unit(x, units) # see ?grid::unit for various units
#
# Arguments
#   fill / color / colour ~ colors
#   linetype = integer (0:8), name of line type, hexidecimal coding
#   linend = round, butt, square
#   arrow = see ?grid::arrow
#   face = plain, italic, bold, bold.italic
#   hjust = 0,1 - horizontal justification (0 left, 0.5 center, 1 right justification)
#   vjust = 0,1 - vertical justification (0 top, 0,5 center, 1, bottom)

p1 +
    theme_classic() +
    theme(plot.title = element_text(color="red", family="mono", size=18),
                axis.title = element_text(color="blue"),
                axis.title.x = element_text(size = 20),
                legend.background = element_rect(color="black", fill=NA),
                plot.background = element_rect(fill="lightgray"),
                panel.grid.major.y = element_line(),
                panel.background = element_rect("pink"))

Assignment

  1. Without using theme_xxxxxx functions, make p1 look like p1 + theme_classic() using theme
  2. Pick a theme from ggthemes and recreate it using theme().

NOTE: No cheating. Cheating = running theme_classic at the prompt without the ()

# original
p1

# theme classic
p1 + theme_classic()

# our attempt
p1 + theme(panel.background = element_rect(fill = "white"),
           axis.line = element_line(color = "black"),
           legend.key = element_rect(fill = NA))

# theme void
p1 + theme_void()

# our attempt
p1 + theme()

---
date: "2020-05-04"
output:
github_document:
html_preview: false
---
```{r, include=FALSE}
knitr::opts_chunk$set(fig.path = here::here())
```
# Pat's code club on ggplot themes
```{r}
library(tidyverse)
library(ggthemes)
mtcars2 <- within(mtcars, {
vs <- factor(vs, labels = c("V-shaped", "Straight"))
am <- factor(am, labels = c("Automatic", "Manual"))
cyl <- factor(cyl)
gear <- factor(gear)
})
p1 <- ggplot(mtcars2) +
geom_point(aes(x = wt, y = mpg, colour = gear)) +
labs(title = "Fuel economy declines as weight increases",
subtitle = "(1973-74)",
caption = "Data from the 1974 Motor Trend US magazine.",
tag = "Figure 1",
x = "Weight (1000 lbs)",
y = "Fuel economy (mpg)",
colour = "Gears")
p1
p1 + theme_gray() # the default
p1 + theme_bw()
p1 + theme_linedraw()
p1 + theme_light()
p1 + theme_dark()
p1 + theme_minimal()
p1 + theme_classic()
p1 + theme_void()
p1 + theme_economist() # https://yutannihilation.github.io/allYourFigureAreBelongToUs/ggthemes/
p1 + theme_excel()
p1 + theme_fivethirtyeight()
```
(checkout the xkcd theme package!)
You can manipulate a lot in a figure withough needing to go deep into the theme options
```{r}
p1 + theme_classic()
p1 + theme_classic(base_size = 20) #a number, default = 11
p1 + theme_classic(base_family = "mono") #a font family, default = sans (serif, mono, symbol); see extrafont package
p1 + theme_classic(base_line_size = 2) #a number, size for line elements, default = base_size/22
p2 <- p1 + facet_grid(vs ~ am)
p2 + theme_classic()
p2 + theme_classic(base_rect_size = 2) #a number, size for rectangular elements, default = base_size/22
p2 + theme_classic(base_rect_size = 0) #a number, size for rectangular elements, default = base_size/22
```
theme function - allows you to manipulate everything on the plot
```{r}
?theme # look at the "Arguments" section for and easier view of the various things you can manipulate
# Generally a heirarchy to how they're named (they are listed alphabetically not by level of figure)
# * the first word indicates what gets manipulated (e.g. plot, legend, etc)
# * the second word indicates what about the first word gets manipulated (e.g. plot.background)
# * the third word indicateds a finer level of specificity (e.g. legend.box.background)
# * sometimes the third word overwrites what happens with the second word (e.g. axis.title vs axis.title.x)
# When setting the values for the theme parameters for some arguments they take
# a character or logical value. More frequently they take one of five functions
#
# margin(t = 0, r = 0, b = 0, l = 0, unit = "pt") #points is default
#
# element_blank()
#
# element_rect(fill = NULL, colour = NULL, size = NULL,
# linetype = NULL, color = NULL, inherit.blank = FALSE)
#
# element_line(colour = NULL, size = NULL, linetype = NULL,
# lineend = NULL, color = NULL, arrow = NULL,
# inherit.blank = FALSE)
#
# element_text(family = NULL, face = NULL, colour = NULL,
# size = NULL, hjust = NULL, vjust = NULL, angle = NULL,
# lineheight = NULL, color = NULL, margin = NULL, debug = NULL,
# inherit.blank = FALSE)
#
# unit(x, units) # see ?grid::unit for various units
#
# Arguments
# fill / color / colour ~ colors
# linetype = integer (0:8), name of line type, hexidecimal coding
# linend = round, butt, square
# arrow = see ?grid::arrow
# face = plain, italic, bold, bold.italic
# hjust = 0,1 - horizontal justification (0 left, 0.5 center, 1 right justification)
# vjust = 0,1 - vertical justification (0 top, 0,5 center, 1, bottom)
p1 +
theme_classic() +
theme(plot.title = element_text(color="red", family="mono", size=18),
axis.title = element_text(color="blue"),
axis.title.x = element_text(size = 20),
legend.background = element_rect(color="black", fill=NA),
plot.background = element_rect(fill="lightgray"),
panel.grid.major.y = element_line(),
panel.background = element_rect("pink"))
```
## Assignment
1. Without using `theme_xxxxxx` functions, make `p1` look like `p1 + theme_classic()` using `theme`
2. Pick a theme from ggthemes and recreate it using `theme()`.
NOTE: No cheating. Cheating = running `theme_classic` at the prompt without the `()`
```{r classic}
# original
p1
# theme classic
p1 + theme_classic()
# our attempt
p1 + theme(panel.background = element_rect(fill = "white"),
axis.line = element_line(color = "black"),
legend.key = element_rect(fill = NA))
```
```{r}
# theme void
p1 + theme_void()
# our attempt
p1 + theme()
```
Version: 1.0
RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 4
Encoding: UTF-8
RnwWeave: Sweave
LaTeX: pdfLaTeX
AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment