-
Notifications
You must be signed in to change notification settings - Fork 0
/
Datalab_4.qmd
323 lines (224 loc) · 9.02 KB
/
Datalab_4.qmd
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
---
title: "Datalab_4"
author: "Erika Scheibe"
format: html
editor: visual
---
```{r}
#Chapters 16-20 of Working in R
library(ggplot2)
library(palmerpenguins)
search()
ggplot(data = penguins,
mapping = aes(x = body_mass_g, y = bill_length_mm, colour = species)) +
geom_point() +
labs(x = "Body mass, g", y = "Bill length, mm", colour = "Species") +
theme_classic()
p1 <- ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm, colour = species)) +
geom_point()
p1
p2 <- ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm, colour = species)) +
geom_density2d()
p2
p3 <- ggplot(penguins, aes(x = species, fill = island)) +
geom_bar()
p3
p4 <- ggplot(penguins, aes(x = species, y = bill_depth_mm, fill = species)) +
geom_boxplot()
p4
```
```{r}
#Exercise 11.4.3
#make histogram
ggplot(data = penguins, mapping = aes(x = bill_length_mm)) +
geom_histogram(color = "red", fill = "pink")
#make violon plot
ggplot(data = penguins, mapping = aes(x = bill_length_mm, y = species)) +
geom_violin(color = "green", fill = "magenta")
#scatterplot
ggplot(data = penguins, mapping = aes(x = bill_length_mm, y = flipper_length_mm, color = species, shape = species)) +
geom_point() +
scale_color_brewer() +
facet_grid(rows = vars(island), cols = vars(sex)) +
theme_light() +
theme(axis.title = element_text(size = 18))
#boxplot bill length by species on top of jittered bill length data
ggplot( data = penguins, mapping = aes(x = bill_length_mm, y = species, color = species)) +
geom_jitter() +
geom_boxplot()
NULL
#exercise 17.5.3: debug these
ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm, color = species)) +
geom_point() +
labs(x = "Bill length", y = "Bill width")
#and debug this:
ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm)) +
geom_point() +
labs(x = "Bill length", y = "Bill width")
ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm, fill = species, color = species)) +
geom_point() +
scale_fill_viridis_d() +
labs(x = "Bill length", y = "Bill width")
```
```{r}
#Chapter 18
#same charts, diff ways:
ggplot(penguins, aes(x = species)) +
geom_bar()
library(tidyverse)
search()
penguins |>
count(species) |>
ggplot(aes(x = species, y = n)) +
geom_col()
base <- ggplot(penguins, aes(x = flipper_length_mm))
p_hist <- base + geom_histogram()
p_dens <- base + geom_density()
p_hist
p_dens
#other way to do one:
ggplot(penguins, aes(x = flipper_length_mm)) +
geom_density()
#exercise 18.2:
ggplot(penguins, aes(x = bill_length_mm)) +
geom_histogram(bins = 60)
ggplot(penguins, aes(x = bill_length_mm, fill = species)) +
geom_density(adjust = .1)
#18.3
base <- ggplot(penguins, aes(x = flipper_length_mm, fill = species))
p_hist <- base + geom_histogram()
p_dens <- base + geom_density(alpha = 0.4) # set alpha to make transparent
p_hist
p_dens
#exercise 18.3:
base <- ggplot(penguins, aes(x = bill_length_mm, fill = species))
p_his_e <- base + geom_histogram(position = "identity", alpha = 0.4)
p_his_e
# I think the position = "identity" is easier to interpret than stack. Stack makes it look taller, but actually the different species just overlap. It's more intuitive
#18.4
penguins |>
drop_na(flipper_length_mm, sex) |>
ggplot(aes(x = species, y = flipper_length_mm)) +
geom_violin(aes(fill = sex))
ggplot(penguins, aes(x = body_mass_g, y = bill_length_mm, colour = species)) +
geom_count(alpha = .8) +
labs(x = "Body mass, g", y = "Bill length, mm", colour = "Species")
#18.7.1 GGally::ggpairs
library(GGally)
penguins |>
select(-year) |>
ggpairs(mapping = aes(colour = species))
#18.7.2 Heatmap
# Select only numeric variables and remove all NAs
penguin_matrix <- penguins |>
select(bill_length_mm:body_mass_g) |>
drop_na(bill_length_mm)
penguin_matrix
# Calculate the correlation between all variables and rearrange the table
cor_matrix <- cor(penguin_matrix)
cor_matrix
# rearrange
cor_long <- cor_matrix |>
as.data.frame() |>
rownames_to_column() |>
pivot_longer(cols = -rowname, names_to = "colname", values_to = "cor") |>
#only upper triangle
filter(rowname < colname)
#Now we can use the function geom_tile() to plot the data.
ggplot(data = cor_long, aes(x = rowname, y = colname, fill = cor)) +
geom_tile() +
scale_fill_gradient2() +
theme(axis.title = element_blank())
#Alternatively, we can use geom_point() and use the size aesthetic to scale the points by the absolute value of the correlation to give more visual weight to the larger correlations.
ggplot(data = cor_long, aes(x = rowname, y = colname, colour = cor, size = abs(cor))) +
geom_point() +
scale_colour_gradient2() +
scale_size_continuous(range = c(2, 20)) +
theme(axis.title = element_blank())
```
```{r}
# CHapter 19: Themes
search()
p1 <- ggplot(penguins, aes(x = body_mass_g,
y = bill_length_mm,
colour = species)) +
geom_point()
p1
theme_set(theme_classic()) #set the default with theme_set(). I do this in the first chunk of quarto and R markdown documents.
#19.3.1 Rotating axis labels Sometimes long axis labels need rotating to stop them overlapping:
p2 <- ggplot(penguins, aes(x = species, y = body_mass_g)) +
geom_boxplot()
p2
p2 +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
#Reading angled text is hard work, so consider alternative solutions to this problem, such as dodging the axis labels:
p2 + scale_x_discrete(guide = guide_axis(n.dodge=2))
#Or flipping the plot:
p2 + coord_flip()
#19.3.2
#Themes let you customise the non-data elements of a plot. You can change the whole theme, or update just a few elements. Here is a plot with the default theme:
p1 + theme(legend.position = "bottom")
#You can change the whole theme to a new pre-built theme by adding one of the theme_*() functions:
p1 +
theme_classic()
#There are many pre-built themes, some in ggplot2, others in the ggthemes, cowplot, and other packages. You can also write your own
#19.1.1 Setting the font
#All the theme_*() functions have an argument to set the base size of the font. The default (about size 11 for most themes) is normally fine for manuscripts and theses, but too small for presentations. Increase it to 18 or 20 so the audience at the back of the auditorium can read the text.
p1 +
theme_classic(base_size = 18)
#19.3.2 Changing the legend position
#If there is space, the legend can be moved into the plot by giving legend.position the x and y coordinates relative to the plot (0 is left or bottom, 1 is top or right). Use legend.justification to specify which corner of the legend should be in the position set by legend.position justify the legend.
p1 +
theme(
legend.position = c(x = 0.01, y = 0.99),
legend.justification = c(x = "left", y = "top"))
#19.3.3 Removing an element
#You can remove non-data elements of the plot by setting them to element_blank().
p1 + theme(panel.grid = element_blank())
```
```{r}
#Chapter 20
#The colour aesthetic give colour for points and lines, while the fill aesthetic gives colour to areas. It is very easy to forget this.
ggplot(penguins, aes(x = bill_length_mm, colour = species)) +
geom_histogram()
#You can set the colour of points or lines and the fill of areas by setting it in the geom_*() used.
ggplot(penguins, aes(x = flipper_length_mm)) +
geom_histogram(colour = "cyan", fill = "turquoise")
#20.1.1 Discrete colour/fill scales
#stopped taking notes here and just read the chapter**********************
#my own random notes:
library(RColorBrewer)
library(raster)
palette = brewer.pal(11,'RdYlBu');
grid <- raster(ncols=11, nrows = 1, xmn=1, xmx=11, ymn=1,ymx=2);
values(grid) <- 1:11
par(mar=rep(0.5, 4))
plot(grid, col=palette, legend=FALSE, axes = 0, box=FALSE)
#20.2.3 random notes
p3 <- ggplot(penguins, aes(x = flipper_length_mm, fill = species)) +
geom_density(alpha = 0.9) +
scale_fill_discrete()
p3
#colorBlindness packages!
library(colorBlindness)
cvdPlot(p3)
#solution for colorBlindness:
p3_v <- p3 + scale_fill_viridis_d()
p3_v
cvdPlot(p3_v)
# Always test that your colour palette is colourblind friendly, or use one known to be ok. (above), 20.1.3
#20.2 Transparency
#random notes:
#see this chapter for shape aesthetic options displayed. and previous chapter section for colors displayed
ggplot(ChickWeight, aes(x = Time,
y = weight,
group = Chick,
colour = Diet)) +
geom_line(linetype = "dashed") +
labs(x = "Time", y = "Chick wt. in g" )
#change line types too:
ggplot(ChickWeight, aes(x = Time, y = weight, group = Chick, colour = Diet, linetype = Diet)) +
geom_line()
#If you need to change the linetypes assigned to each value, you can use scale_linetype_manual and specify them by name (Figure 20.1).
#/end
```