-
Notifications
You must be signed in to change notification settings - Fork 6
/
03-annotating.Rmd
104 lines (70 loc) · 3.83 KB
/
03-annotating.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
---
title: "Data Storytelling 3 - Annotating"
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"))
```
# Part 3: Annotating
## Learning Objectives
- Learn about how annotations can help your viewer
- Modify titles using `labs()`
- Adding reference lines
- Annotating directly onto the graph
- Changing the scale
# Before you move on, discuss:
What TV Show do you want to highlight with text? Why is it interesting to you? Is there a trend you want to highlight?
# Changing Titles
There is a function called `labs()` that will let you change the titles for a graph. Figure out where these titles are added to the graph below.
```{r}
my_plot +
labs(title = "Is it better to burn out than fade away?",
x="Season Number",
y="Average Rating")
```
Why is `labs()` not part of `theme()`? This is one confusing thing about `ggplot2`. `theme()` is about the *appearance* (position, angle, font size) of an element, whereas the `labs()` function actually provides the *content*.
## Adding a Reference Line
Using `geom_hline()` to show the average value across a time period can provide a useful reference for viewers:
```{r}
my_plot +
geom_hline(yintercept = 7.8, lty=3)
```
We can add a vertical reference line instead using `geom_vline()` (note the argument, `xintercept` is different than `geom_hline()`):
```{r}
my_plot +
geom_vline(xintercept = 5, lty=3)
```
## Adding text annotations
Adding text annotations directly to the graph can be extremely helpful, especially if there are points of interest you want users to look at. (For adding text information per data point, look at `geom_text()` and `geom_repel()` from the `ggrepel` package).
The first argument to `annotate()` is "text".
It takes `x` and `y` arguments to determine the position of our annotation. These values are dependent on the scale - since we have a numeric scale for both the `x` and `y` axes, we'll use numbers to specify the position.
Our actual text goes in the `label` argument.
Run the code block below. Try adjusting the `y` argument for `annotate()` to get the annotation more centered around the mean reference line.
```{r}
my_plot +
geom_hline(yintercept = 7.8, lty=3) +
annotate(geom="text", x = 2, y=10 , label="Mean Rating")
```
# One last thing: changing the numbers in the ticks using `scale_x_continuous()`
One last thing that's been bugging me - the values of the ticks in the `Season Number` axis. We can specify these numbers using the `breaks` argument. Note that `c(1:10)` is a shortcut for specifying `c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)`.
```{r}
my_plot +
scale_x_continuous(breaks = c(1:10))
```
### Part 3: Your Turn
Experiment with the following modifications to the graph. If you have time, cut and paste the modifications you decided on in part 2 to your graph.
If there's a show that you want to highlight, try adding an annotation to highlight it. Or try adding an annotation at `Roseanne`'s lowest rating!
```{r}
#put this in order
my_plot +
labs(title = "Is it better to burn out than fade away?",x="Season Number", y="Average Rating") +
geom_vline(xintercept = 5, lty=3) +
annotate(geom="text", x = 7, y=8.5, label = "The Walking\n Dead (brainzzz)") +
scale_x_continuous(breaks = c(1:10))
```
# For more information
For more examples of how annotating can make figures more clear, please check out Storytelling with Data: https://storytellingwithdata.com, especially the linegraphs with annotations for examples: http://www.storytellingwithdata.com/blog/2018/1/22/88-annotated-line-graphs
Making annotations first class citizens in data visualization: https://medium.com/@Elijah_Meeks/making-annotations-first-class-citizens-in-data-visualization-21db6383d3fe