-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path04-highlighting.Rmd
88 lines (58 loc) · 2.85 KB
/
04-highlighting.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
---
title: "Data Storytelling 4 - Highlighting"
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"))
```
## Changing the base plot (slightly)
Remember our original plot mapped color to `title`. There's also a variable called `status` in our data. `status` has two different values: a tv show can be a `riser` (positive trend in ratings), or be a `faller` (negative trend in ratings).
Let's modify the plot to use this variable to `color` our lines. We'll save it as a different object, this time called `my_new_plot`.
```{r}
my_new_plot <-
ggplot(tv_shows) +
aes(x = seasonNumber,
y= av_rating,
group=title,
color = status) + #changing `color` to map to `status` here
geom_line()
my_new_plot
```
## Highlighting part of your data
What if we only want to highlight one group in the data? In this case, maybe we want to highlight the *risers* in our dataset. If we color them blue, and leave the others as `grey` we can immediately highlight them as important and worth noticing in the context of the other data.
We can actually manually color our traces by using `scale_color_manual()`. This lets us manually map our values in our variable (`riser` and `faller`) to colors: (`blue` and `grey`).
```{r}
my_new_plot +
scale_color_manual(values=c("riser"="blue",
"faller"="grey")) +
theme_minimal()
```
If you've mapped a variable to `fill`, you'll have to use `scale_fill_manual()` to map values to colors.
### Try out mapping colors
Try using different colors to contrast the lines in the `values` argument to `scale_color_manual()`. A small list of color names in R can be found here: https://www.r-graph-gallery.com/42-colors-names.html
```{r}
library(tidyverse)
my_new_plot +
scale_color_manual(values=c("riser"="blue", "faller"="grey"))
```
## Put it all together!
Cut and paste all your modifiers and make your final figure below!
```{r}
my_new_plot +
theme_minimal()
```
# Saving High Quality Plots
We can use `ggsave()` to save our plot. By default, it will save the last plot we made.
```{r eval=FALSE}
ggsave("movieplot.pdf")
```
# More Best Practices for figures:
Ten Simple Rules for Better Figures: https://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1003833
# More tips and tricks for using contrast and color
In `03-annotating`, we saw that we can specify the line type for a particular graph. We can also specify line type as an aesthetic. Be careful with line types - too many in a figure can obscure your point.
Shapes for points can also be helpful for highlighting particular data points. Here's a useful reference.
More information here:
http://www.cookbook-r.com/Graphs/Shapes_and_line_types/