-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy path11-communication.Rmd
273 lines (204 loc) · 8.78 KB
/
11-communication.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# Communication
***Note: This was chapter 28 when these slides were last edited. They likely need to be updated to match the current chapter order.***
**Learning objectives:**
- **Describe the contents** of a plot with **labels.**
- Call out **specific features** of your data with **annotations.**
- Control **how data maps** to things you can see with **`scale_` functions.**
- Control **which data displays** in a plot.
- **Customize** the non-data elements of your plot with **themes.**
- **Save** your plots.
-------------
## Tools you need to create good graphics
> The Truthful Art, by Albert Cairo
The book focuses on what you need to think about in order to create effective graphics.
```{r 13-01,fig.cap="Credit:The Truthful Art, by Albert Cairo",fig.align='center', include=FALSE}
knitr::include_graphics("images/28_book_cairo.png")
```
**Resources:**
- http://www.thefunctionalart.com/p/instructors-guide.html
- https://www.dropbox.com/s/gyqyz3hei7lhhmt/R_RESOURCES.txt?dl=0
- https://exts.ggplot2.tidyverse.org/gallery/
```{r 13-02,message=FALSE, warning=FALSE, include=FALSE, paged.print=FALSE}
library(tidyverse)
```
## Use labels and annotations
1. First data set is about the Biochemical Oxygen Demand, found in {datasets} package, it is made of two variables:
-Time
-Demand
```{r 13-03}
BOD
```
```{r 13-04,message=FALSE, warning=FALSE, paged.print=FALSE}
ggplot(BOD, aes(Time, demand)) +
geom_point(size=2) +
geom_smooth() +
labs(
title = "Biochemical Oxygen Demand",
subtitle = "versus Time in an evaluation of water quality",
caption = "Originally from Marske (1967), \nBiochemical Oxygen Demand Data Interpretation Using Sum of Squares Surface M.Sc. Thesis, \nUniversity of Wisconsin – Madison.",
y ="Demand") +
tvthemes::theme_avatar()
```
2. Second dataset is found in the book and made of some random uniform distributions.
Here we see how we can coustomise axis title with maths formula.
```{r 13-05}
df <- tibble(
x = runif(10),
y = runif(10)
)
ggplot(df, aes(x, y)) +
geom_point(shape=21, stroke=2,size=5,fill="grey67",alpha=0.7) +
labs(
x = quote(sum(x[i] ^ 2, i == 1, n)),
y = quote(alpha + beta + frac(delta, theta))
) +
tvthemes::theme_brooklyn99()
```
3. Third dataset is the Annual Precipitation in US Cities, the set is made of one observation each column, about the average amount of precipitation (rainfall) in inches for each of 70 United States (and Puerto Rico) cities.
Here we see how to add text to a plot with `geom_text()`
```{r 13-06}
precip[1:10]
length(precip)
```
```{r 13-07}
cities <- precip%>%names
df <- data.frame(cities,precip)
df %>%
arrange(precip) %>%
ggplot(aes(x=1:70,y=precip)) +
geom_point(shape=21,color="white") +
geom_line(size=0.5,linetype="dashed")+
geom_text(aes(label=cities,size=precip),
hjust = "right",nudge_x = -2,
check_overlap = T) +
labs(title="Trend of Annual Precipitation in US Cities",
x="70 United States Cities",
y="AVG precipitation (rainfall) in inches")+
tvthemes::theme_spongeBob()+
theme(legend.position = "none")
```
4. This is the Edgar Anderson's Iris Data. It gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are Iris setosa, versicolor, and virginica.
```{r 13-08}
ggplot(iris,aes(Sepal.Length,Sepal.Width,group=Species))+
geom_point(aes(color=Species)) +
geom_text(data=iris%>%
group_by(Species)%>%
filter(Sepal.Length==max(Sepal.Length))%>%
ungroup(),
aes(label=Species),
size=8, hjust="right",
check_overlap = T) +
hrbrthemes::theme_modern_rc()
```
5. This is Fuel economy data from 1999 to 2008 for 38 popular models of cars. This dataset contains a subset of the fuel economy data that the EPA makes available on https://fueleconomy.gov/. It contains only models which had a new release every year between 1999 and 2008 - this was used as a proxy for the popularity of the car.
Here we learn how to set the lable in specific place inside the plot.
```{r 13-09}
label <- mpg %>%
summarise(
displ = max(displ),
hwy = max(hwy),
label = "Increasing engine size is \nrelated to decreasing fuel economy."
)
ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(color=factor(hwy)),size=2) +
geom_text(aes(label = label), data = label,
color="grey40",size=6,
vjust = "top", hjust = "right")+
ggthemes::theme_fivethirtyeight() +
theme(legend.position = "none")
```
In particular for all the possibility we can refer to this plot for principal adjustments of the text inside the plot.
![Text adjutment position](images/28_just-1.png)
Other geoms for annotation:
- `geom_hline()` and `geom_vline()` to add reference lines. I often make them thick (size = 2) and white (colour = white), and draw them underneath the primary data layer. That makes them easy to see, without drawing attention away from the data.
- `geom_rect()` to draw a rectangle around points of interest. The boundaries of the rectangle are defined by aesthetics xmin, xmax, ymin, ymax.
- `geom_segment()` with the arrow argument to draw attention to a point with an arrow. Use aesthetics x and y to define the starting location, and xend and yend to define the end location.
## Scales
The `scale_<functions>` are very useful in many ways, among which is to set a color/fill scale for a particular plot, or to make modification of the x/y text axis.
Here the dataset is made of random normal of 10 000 values.
```{r 13-10, fig.asp = 0.618}
df <- tibble(
x = rnorm(10000),
y = rnorm(10000)
)
p1 <- ggplot(df, aes(x, y)) +
geom_hex() +
coord_fixed()
p2 <- ggplot(df, aes(x, y)) +
geom_hex() +
viridis::scale_fill_viridis() +
coord_fixed()
library(patchwork)
p1+p2 &
theme_linedraw() &
theme(legend.position = "top")
```
## Themes
There are interesting packages available for adjusting the them of your plot in addition to the themes that are provided with {ggplot2} package: **ggplot2 extensions - gallery**
- {viridis}
- {hrbrthemes}
- {ggthemes}
- {tvthemes}
- ...
source: [https://exts.ggplot2.tidyverse.org/gallery/](https://exts.ggplot2.tidyverse.org/gallery/)
![png](images/28_themes.png)
## Save the plot
`ggsave("my-plot.pdf")`
## Meeting Videos
### Cohort 5
`r knitr::include_url("https://www.youtube.com/embed/42cqVanImm0")`
<details>
<summary> Meeting chat log </summary>
```
00:08:21 Jon Harmon (jonthegeek): https://twitter.com/skyetetra/status/1501268379386081285
00:13:33 Federica Gazzelloni: http://www.thefunctionalart.com/p/instructors-guide.html
https://www.dropbox.com/s/gyqyz3hei7lhhmt/R_RESOURCES.txt?dl=0
https://exts.ggplot2.tidyverse.org/gallery/
00:36:52 Njoki Njuki Lucy: in the ggplot()
01:09:05 Njoki Njuki Lucy: I am so interested
01:11:48 Becki R. (she/her): See you all next week!
```
</details>
`r knitr::include_url("https://www.youtube.com/embed/mPCSgJAYLac")`
<details>
<summary> Meeting chat log </summary>
```
00:10:30 Ryan Metcalf: https://www.rstudio.com/conference/
00:33:38 shamsuddeen: Yes
00:33:53 shamsuddeen: ISLR should be first !
00:37:08 Ryan Metcalf: https://www.manning.com/books/human-in-the-loop-machine-learning?query=Human%20in%20the%20Loop
00:38:23 shamsuddeen: SMLTR
00:39:50 shamsuddeen: Human-in-the-Loop Machine Learning. When is this starting ?
00:40:10 shamsuddeen: ok
00:50:30 shamsuddeen: Any book on Torch coming?
00:54:25 shamsuddeen: Data Science at Command Line 2e: https://datascienceatthecommandline.com/2e/
00:54:38 shamsuddeen: Wanna start this !
01:01:01 Federica Gazzelloni: https://dslc.io/clubdeck
01:12:22 shamsuddeen: Applied Predictive Modelling?
```
</details>
### Cohort 6
`r knitr::include_url("https://www.youtube.com/embed/qQrnNef9fkM")`
<details>
<summary> Meeting chat log </summary>
```
00:09:51 Marielena Soilemezidi: http://www.thefunctionalart.com/p/instructors-guide.html
https://www.dropbox.com/s/gyqyz3hei7lhhmt/R_RESOURCES.txt?dl=0
https://exts.ggplot2.tidyverse.org/gallery/
00:26:20 Daniel: underscore
00:26:32 Daniel: It is called an “underscore”
00:42:17 Daniel: I can’t see the screen anymore
00:43:00 Daniel: All good now
00:44:57 Marielena Soilemezidi: https://github.com/jrnold/ggthemes)
00:55:42 Daniel: https://stackoverflow.com/questions/23957278/how-to-add-table-of-contents-in-rmarkdown
01:03:59 Daniel: It’s fine, sorry my internet went off
01:04:17 Marielena Soilemezidi: http://shiny.rstudio.com/.
01:05:04 Marielena Soilemezidi: http://rmarkdown.rstudio.com/rmarkdown_websites.html.
01:07:21 Marielena Soilemezidi: http://colinpurrington.com/tips/lab-notebooks
01:10:02 Marielena Soilemezidi: https://quarto.org/
```
</details>
### Cohort 7
`r knitr::include_url("https://www.youtube.com/embed/fOA271r3MGo")`
### Cohort 8
`r knitr::include_url("https://www.youtube.com/embed/etTi7a_HJTc")`