-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.Rmd
More file actions
234 lines (172 loc) · 4.06 KB
/
code.Rmd
File metadata and controls
234 lines (172 loc) · 4.06 KB
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
---
title: '2022 LJSA Data visualization with R Workshop '
author: "Sergio Uribe, @sergiouribe"
output:
pdf_document: default
html_notebook: default
---
```{r, setup, include=FALSE}
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
```
# Preparation, load the required packages
If not installed, first install the pacman package
```{r}
# install.packages("pacman") # uncomment this line, removing the first #
```
Load the required packages
```{r}
pacman::p_load(tidyverse, palmerpenguins)
```
# Load the data
```{r}
data(penguins)
```
# Basic data exploration
# Check the structure
```{r}
str(penguins)
```
Similar to structure, but printer-friendly
```{r}
glimpse(penguins)
```
# Check the first and last rows
```{r}
head(penguins)
```
```{r}
tail(penguins)
```
# View the dataset in spreadsheet format
```{r}
View(penguins)
```
# View the names of the columns
```{r}
names(penguins)
```
# Create a summary of the dataset
```{r}
summary(penguins)
```
## Access a specific column of the dataset
Using dataset**\$**column
```{r}
summary(penguins$sex)
```
```{r}
summary(penguins$bill_length_mm)
```
# My first plot
We need
1. data
2. some aesthetic
3. a geom to plot the data on the aesthetic
## The data
```{r}
penguins %>% # load the data, and with this data >
ggplot() # create a plot
```
## The aesthetic
```{r}
penguins %>% # load the data, and with this data >
ggplot(aes(x = body_mass_g)) # create a plot and add an aesthetic
```
```{r}
penguins %>% # load the data, and with this data >
ggplot(aes(x = body_mass_g)) + # create a plot and add an aesthetic
geom_histogram() # add the geom, that allow to represent the data inside the aesthetic with some specified geometry
```
# Make some transformations
Usually, it is necessary to make some transformations to the data before plotting them.
The general formula is
data %\>%
**some transformation** %\>%
the plot
In this case, we are going to filter a type of penguin to see its weight distribution.
```{r}
penguins %>% # this is the data
filter(species == "Gentoo") %>% # filter only the gentoo penguin
ggplot(aes(x = body_mass_g)) +
geom_histogram()
```
Alternatively, we can use all the data and make different graphs for some variables, e.g. sex and species.
```{r}
penguins %>% # this is the data
ggplot(aes(x = body_mass_g)) +
geom_histogram() +
facet_grid(sex ~ species)
```
```{r}
penguins %>%
drop_na() %>% # to remove the NA's we can make a transformation before plotting
ggplot(aes(x = body_mass_g)) +
geom_histogram() +
facet_grid(sex ~ species)
```
# Two variables
```{r}
penguins %>%
ggplot(aes(x = bill_length_mm,
y = bill_depth_mm)) +
geom_point()
```
## Is any correlation?
```{r}
penguins %>%
ggplot(aes(x = bill_length_mm,
y = bill_depth_mm)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE)
```
## Verify whether the correlation is robust, disaggregating by species
```{r}
penguins %>%
ggplot(aes(x = bill_length_mm,
y = bill_depth_mm,
shape = species,
color = species)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE)
```
An example of Simpon's paradox
# Polish the graph
## Change the theme
```{r}
penguins %>%
ggplot(aes(x = bill_length_mm,
y = bill_depth_mm,
shape = species,
color = species)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
# polishing the graph
theme_classic()
```
## Add labels
```{r}
penguins %>%
ggplot(aes(x = bill_length_mm,
y = bill_depth_mm,
shape = species,
color = species)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
# polishing the graph
theme_classic() +
labs(title = "The Title",
subtitle = "Some subtitle",
x = "Bill length (mm)",
y = "Bill depth (mm)",
shape = "Species",
color = "Species")
```
# Export the plot
Check <https://ggplot2.tidyverse.org/reference/ggsave.html>
```{r}
ggsave(filename = "myFirstPlot.pdf",
width = 12,
height = 10,
dpi = 300,
units = "cm")
```