forked from laderast/data_storytelling_bdc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
02-decluttering.Rmd
121 lines (77 loc) · 4.33 KB
/
02-decluttering.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
---
title: "Data Storytelling 2 - Decluttering"
output: html_notebook
---
If you've skipped ahead (and not run Part 1), run this code chunk to load the relevant data and plot:
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(here)
source(here("R/init.R"))
```
# Learning Objectives
- Understand why we need to declutter
- Reducing clutter using built in themes
- Removing elements using `theme()`
# A basic ggplot
We're going to take this basic plot and try to reduce the amount of clutter.
```{r}
my_plot
```
# Part 2: Simplifying your plot
The neat thing about `ggplot2` is that you can modify the plot code by adding *modifiers* with the `+` (plus) sign.
We're going to take a "paper doll" approach to our plot to modifying it - each *modifier* can be thought of as a set of "clothes" that we dress our plot in.
The defaults for the plot are decent, but have some distracting elements. We can declutter our plot by removing some of these distracting elements.
Before you move on, discuss on slack:
1) What parts of the plot are distracting from your message about tv shows that are `risers`?
# One note
In each section, we're trying different types of modification, so they're not cumulative. However, you can cut and paste the modifications together in the end to have your final customized plot.
# Intelligent Defaults: Using Built in Themes
A lot of the simplification can come from built in themes. Themes are like a full wardrobe for our paper dolls, specifying lots of different details. Adding `theme_minimal()` will remove a lot of the background elements. Try it out and compare it with the above plot.
Built in themes let you be more efficient in paring elements down, but you will often find that you need to customize them. That's what we'll look at next.
```{r}
my_plot + theme_minimal()
```
## Removing elements using `theme()`
We can customize our plot even further by adding a `theme()` function to the end of our plot. We'll use this to remove individual elements from the plot.
Note: If we change any theme attributes after calling `theme_minimal()`, it will basically *overwrite* the previous built-in theme. This means that order is important, so keep that in mind!
`theme()` looks very intimidating, because it has lots of different arguments. We'll only look at a few of these:
- `axis.title`, `axis.title.x` and `axis.title.y` (The labels for the axes)
- `panel.grid` (grid lines)
- `legend.position` (Placing the legend, including removing it)
How will we remove these elements? For most of them, we will specify an element called `element_blank()` to these arguments. What is this? Think of it as a special placeholder that says we don't want to see this element.
Let's try simplifying our plot by removing the x-axis text. By context, the label `seasonNumber` isn't that helpful.
```{r}
my_plot +
theme(axis.title.x = element_blank())
```
Try removing the gridlines. Is that helpful or not?
```{r}
my_plot +
theme(panel.grid = element_blank())
```
Sometimes, to remove some items, like the legend, you have to specify it not as `element_blank`, but as "none". Try removing the legend.
Hmm, it is more simplified, but we have lost all the information about the categories! We can add that information back with the use of color in section 3.
```{r}
my_plot +
theme(legend.position = "none")
```
## Your turn
Play with different combinations of these different `theme()` statements to customize your plot. If you want to remove a line, you can put a `#` in front of that line. That will make it a comment and that line of code won't run.
```{r}
my_plot +
theme_minimal() +
#theme(axis.title.x = element_blank()) +
theme(panel.grid = element_blank()) +
theme(legend.position = "none")
```
## For more information
Take a look at the ggthemes gallery for other pre-made themes that can help you declutter your plot.
https://www.datanovia.com/en/blog/ggplot-themes-gallery/
Look at the documentation for `theme()` to understand how to customize different elements. Each element maps to four different elements:
`element_blank()` - we've seen this.
`element_text()` - anything that's text (such as labels - this is where you can specify arguments like `angle`,`size`)
`element_rect()` - anything that's a rectangle (such as the panel)
`element_line()` - anything that's a line (such as axes and grid lines)
```{r}
?theme
```