-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay2-ExploringGraphs.R
More file actions
281 lines (135 loc) · 5.62 KB
/
Day2-ExploringGraphs.R
File metadata and controls
281 lines (135 loc) · 5.62 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
### Limpopo Resilience Lab Virtual R Workshop
### August 18-20, 2021
### Instructor: Max Glines
### original code provided by Rachel Pilla
### DAY 2: Exploring Data with Graphs ###
######################################
### Structure and design of graphs ###
######################################
### PPT:
# - components of a graph
# - understanding/perceiving a graph
# - common types of graphs for 1, 2, 3+ variables
# - keys to good graphs
# - examples of good/bad graphs
# Load libraries and data files from yesterday:
library(dplyr)
library(tidyr)
library(lubridate)
setwd("")
beit.bridge <- read.csv("A7H008YRPK.CSV", skip = 7)
beit.bridge.clean <- beit.bridge[1:28, ] %>%
mutate(Year = as.numeric(Year))
mutale <- read.csv("Limpopo_Resilience_Lab__Mutale_Weir_Dataset.csv")
mutale.clean <- mutale %>%
mutate(Date_Time = ymd_hm(paste(YEAR, MONT, DAYN, HOUR, MINU))) %>%
na_if(-9999) %>%
na_if(-8888) %>%
na_if(-7777) %>%
select(-YEAR, -MONT, -DAYN, -HOUR, -MINU) %>%
rename(Precipitation_mm = PRCP,
AirTemp_degC = TEMP,
RelHumidity_percent = RHMD,
SolarRad_W_m2 = SRAD,
AirPressure_kPa = APRS,
WindSpeed_m_s = WSPD,
WindDir_deg = WDIR,
RiverStage_m = RIVS,
WaterTemp_degC = WTMP,
Conductivity_uS_cm = COND,
Turbidity_NTU = TRBD)
##########################
### Base plotting in R ###
##########################
###############################
### Introduction to ggplot2 ###
###############################
### PPT:
# - "Grammar of Graphics" by Hadley Wickham
# - layers of ggplot2
# - geometries, aesthetics, options/customizations
# - building and customizing plots in layers
# - order of the plot layers can be important!
# Recommend keeping the "ggplot()" statement BLANK
# and instead adding the specific data frame(s) in use for each layer
library(ggplot2)
# if using only one dataset
ggplot(data=,aes=(*column names are used here*))+
geom_***(color,size,shape,...)+
add_layers(*scaling, formatting, etc.*) +
adjust_style(*theme, fonts, tick marks, etc.*)
# if using multiple datasets
ggplot() +
geom_***(data = , mapping = aes(*column names are used here*),
*generic/consistent arguments are used here*) +
add_layers(*scaling, formatting, etc.*) +
adjust_style(*theme, fonts, tick marks, etc.*)
# simple scatterplot
?geom_point
ggplot(data = beit.bridge.clean, mapping = aes(x = Year, y = Level..m.)) +
geom_point()
# add lines to connect points (time series visual)
# add some general customizations (colors, axes labels)
### PRACTICE
# Create a plot of annual maximum flow over time, with
# appropriate title and axis labels,
# points connected by lines,
# BLUE color for points and lines,
# DOTTED lines (HINT: "linetype" argument -- see "help(lines)"),
# OPEN TRIANGLE point symbols (HINT: "shape" argument -- see "help(points)")
###########################
### Customizing ggplot2 ###
###########################
# add linear model and trend/regression line
### repeat above for bottom water temperature with trend line
# Can we add multiple lines to the same plot?
# Let's look at DAILY air temperature and water temperature at Mutale Weir
head(mutale.clean)
daily.airtemp.watertemp <- mutale.clean %>%
mutate(DateOnly = date(Date_Time)) %>%
group_by(DateOnly) %>%
summarize(DailyAirTemp_degC = mean(AirTemp_degC, na.rm = T),
DailyWaterTemp_degC = mean(WaterTemp_degC, na.rm = T))
## x-y scatterplot:
daily.temps.long <- daily.airtemp.watertemp %>%
pivot_longer(cols = c(DailyAirTemp_degC, DailyWaterTemp_degC),
names_to = "Variable",
values_to = "Temperature")
## FACETING:
# histograms
ggplot() +
geom_histogram(data = beit.bridge.clean, mapping = aes(Level..m.))
# adjust binwidth, and add customizations (axes labels, color)
# boxplots
ggplot() +
geom_boxplot(data = daily.temps.long, mapping = aes(x = Variable, y = Temperature))
# customize (axes labels, color)
# barplots
MonthlyTurbidity <- mutale.clean %>%
mutate(Date_Only = date(Date_Time),
Month = month(Date_Time)) %>%
filter(!is.na(Turbidity_NTU)) %>%
group_by(Date_Only, Month) %>%
summarize(DailyTurbidity = sum(Turbidity_NTU, na.rm = T)) %>%
ungroup() %>%
group_by(Month) %>%
summarize(MeanTurbidity = mean(DailyTurbidity),
StErrTurbidity = sd(DailyTurbidity) / sqrt(NROW(DailyTurbidity)))
# add error bars
# other customizations (color, labels, remove legend)
### PRACTICE
# Create a barplot of average monthly WATER temperature,
# starting from the "daily.airtemp.watertemp" object.
# Include error bars of +/- 1 standard deviation,
# and appropriate names and axis labels
##########################
### PRACTICE EXERCISES ###
##########################
# using these data, make three plots:
# 1) Boxplot of total daily precipitation by month (Mutale Weir)
# 2) Line plots with FACETING of DAILY precipitation and river stage (Mutale Weir)
# 3) Scatterplot of precipitation vs. river stage with trend line (Mutale Weir)
# REMEMBER YOU MIGHT HAVE TO CLEAN SOME DATA BEFORE PLOTTING!
# 1: Boxplot of total daily precipitation by month (Mutale Weir)
# 2: Line plots with FACETING of DAILY precipitation and river stage (Mutale Weir)
# 3: Scatterplot of precipitation vs. river stage with trend line (Mutale Weir)