-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathR_workshop_2_class_script.R
More file actions
312 lines (224 loc) · 6.87 KB
/
R_workshop_2_class_script.R
File metadata and controls
312 lines (224 loc) · 6.87 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
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
######################
#### R Workshop 2 ####
######################
# clear our environment
rm(list = ls()) # remove all objects from your working environment
dev.off() # clear your plot window
# install and load packages
# install tidyverse using the packages tab
library(tidyverse) # load the package into your global environment
# load data
data(starwars) # load the data set
head(starwars) # look at the first few lines
# piping
length(starwars$name) # example function
# example piping into this fuction
starwars$name %>%
length()
# dplyr
# select()
# select column with base R
starwars$name
select(starwars, name)
# piping into select function
starwars %>%
select(name)
# select more than 1 column
# with baseR
starwars[ ,c("name", "homeworld")]
# with dplyr
select(starwars, name, homeworld)
# with dplyr a pipe
starwars %>%
select(name, homeworld)
# select only columns with a certain character
starwars %>%
select(-contains("_"))
# select only columns without a certain character
starwars %>%
select(-starts_with("S"))
# select everything except the name column
starwars %>%
select(-name)
# filter
# baseR
starwars[starwars$species == "Human", ]
# with dplyr
filter(starwars, species == "Human")
# with dplyr and piping
starwars %>%
filter(species == "Human")
# baseR
# to fix the fact that we are also retaining NAs when we use base R we have to
# wrap our logical statement inside the which function
starwars[which(starwars$species == "Human"), ]
# filtering on multiple variables
filter(starwars, species == "Human" & homeworld == "Tatooine")
# with piping
starwars %>%
filter(species == "Human" & homeworld == "Tatooine")
# select and filter together
select(filter(starwars, species == "Human"), name, height, birth_year)
# and with piping
starwars %>%
filter(species == "Human") %>%
select(name, height, birth_year)
# SUMMARISING DATA
# grouping our data
starwars %>%
group_by(species)
# counting groups
starwars %>%
group_by(species) %>%
count()
# counting groups within groups
starwars %>%
group_by(species, gender) %>%
count()
# arrange output
starwars %>%
group_by(species, gender) %>%
count() %>%
arrange(desc(n))
# list distinct species
starwars %>%
distinct(species)
# arrange our species
# add the - before the desc() function to get ascending order
starwars %>%
distinct(species) %>%
arrange(-desc(species))
# count the distinct species
starwars %>%
distinct(species) %>%
count()
# summarise
# summarising a single column
# we create a new column called mean_height
# and call the mean function
starwars %>%
summarise(mean_height = mean(height, na.rm = T))
# we can use any function inside summarise
# e.g. to count:
starwars %>%
group_by(species) %>%
summarise(count = n())
# or to summarise the mean for each species seperately
# we can group first before summarising
starwars %>%
group_by(species) %>%
summarise(mean_height = mean(height, na.rm = T))
# multiple summaries using different functions
starwars %>%
group_by(species) %>%
summarise(max_height = max(height, na.rm = T),
min_height = min(height, na.rm = T))
# we can also use the same function to summarise multiple columns
# but note there is an easier way to do this below
starwars %>%
group_by(species) %>%
summarise(mean_height = mean(height, na.rm = T),
mean_mass = mean(mass, na.rm = T))
# summarise at
starwars %>%
group_by(species) %>%
summarise_at(vars(height, mass), mean, na.rm = T)
# GGPLOT2
# build up layers of our plot
ggplot()
# give ggplot our data frame
ggplot(starwars)
# add our x and y aesthetics
ggplot(starwars, aes(x = height, y = mass))
# we don't need to tell it which is x and which is y because x is always first
# so this works exactly the same way:
ggplot(starwars, aes(height, mass))
# then we tell it we want to add a layer of points
ggplot(starwars, aes(height, mass)) +
geom_point()
# Our plot has one quite serious outlier
# We can use dplyr to identify it
starwars %>%
filter(mass > 1000) %>%
select(name, species, mass)
# we can customise the points inside the geom_point brackets
ggplot(starwars, aes(height, mass)) +
geom_point(size = 4, shape = 22, alpha = 0.5, fill = "purple", colour = "orange")
# histograms
ggplot(starwars, aes(mass)) +
geom_histogram(binwidth = 20)
# boxplots
ggplot(starwars, aes(species, mass)) +
geom_boxplot()
# This boxplot is too busy, lets filter out most abundant species
# we will first identify them using dplyr
starwars %>%
group_by(species) %>%
count() %>%
arrange(desc(n))
# now we'll filter the data set for the most abundant species
new_starwars <- filter(starwars, species == "Human" | species == "Droid" | species == "Gungan")
# now plot our new dataset
ggplot(new_starwars, aes(species, mass)) +
geom_boxplot()
# CUSTOMISATION
# start witha basic plot
ggplot(starwars, aes(height, mass)) +
geom_point()
# we will filter out our outlier
starwars2 <- filter(starwars, mass <= 1000)
# then use the new data frame in ggplot
# play around with the different built in themes in R
ggplot(starwars2, aes(height, mass)) +
geom_point() +
theme_void()
# create our own cusom theme
ggplot(starwars2, aes(height, mass, colour = height)) +
annotate("rect", xmin = 80, xmax = 120, ymin = -Inf, ymax = Inf, fill = "pink", alpha = 0.5) +
geom_point() +
theme(axis.text = element_text(size = 16, colour = "red"),
axis.title.x = element_text(size = 20, colour = "purple"),
axis.title.y = element_text(size = 10, colour = "darkgreen", angle = 360, family = "serif", face = "italic"),
panel.background = element_rect(fill = "white", colour = "black"),
axis.ticks = element_line(linewidth = 3)) +
scale_colour_continuous(type = "viridis") +
xlab("Height (cm)") +
ylab("Mass (g)")
# reshaping data
head(iris)
# make a plot with the wide data frame
ggplot(iris, aes(Species, Sepal.Length)) +
geom_boxplot()
# pivot longer
# first we need to add an ID column as this data set does not already have one
iris$ID <- row.names(iris)
# convert data from wide to long format
iris2 <- pivot_longer(iris, col = 1:4, names_to = "measure")
# look at the new dataset we created
head(iris2)
# plot the long iris dataset
ggplot(iris2, aes(Species, value)) +
geom_boxplot()
# use facet wrap to split up the plots
ggplot(iris2, aes(Species, value)) +
geom_boxplot() +
facet_wrap(~measure)
# again we can play around with the theme
# we can also assign our plot to an object
plot1 <- ggplot(iris2, aes(Species, value)) +
geom_boxplot() +
facet_wrap(~measure) +
theme_dark()
# then look at it using the object name
plot1
# pivot_wider
# return the data frame to its previous wide state
pivot_wider(iris2, values_from = "value", names_from = "measure")
# saving plot as a jpeg
jpeg("our_R_plot.jpg", units = "cm", width = 10, height = 10, res = 300)
plot1
dev.off()
# saving our plot as a pdf
pdf("our_plot.pdf", width = 5, height = 5)
plot1
dev.off()