-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallengeSolutions.Rmd
More file actions
219 lines (155 loc) · 8.43 KB
/
challengeSolutions.Rmd
File metadata and controls
219 lines (155 loc) · 8.43 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
---
title: "Challenge Solutions"
output:
github_document: default
html_notebook: default
---
Here are the solutions to the Challenges in the main tutorial `Plotting Ocean Data With R`
## Initialize session
We first need to load the `tidyverse` and akima libraries:
```{r}
library(tidyverse)
library(akima)
```
and the data:
```{r}
fieldData <- read_csv('data/DamariscottaRiverData.csv')
```
## Challenge A
1.a. Create a scatter plot of 'temperature_degC' by 'fluorescence_mg_m3' and color the points by 'station'.
```{r, fig.height = 5, fig.width=6}
ggplot(data = fieldData, mapping = aes(x = temperature_degC, y = fluorescence_mg_m3)) +
geom_point(aes(color=station))
```
1.b. Do the same as 1.a. but convert the station values to factors. What's the difference between the two plots?
```{r, fig.height = 5, fig.width=6}
ggplot(data = fieldData, mapping = aes(x = temperature_degC, y = fluorescence_mg_m3)) +
geom_point(aes(color=factor(station)))
```
In the first plot, the colors are on a continuous colorscale. In the second plot, the colors are discrete, separate colors. When the data was read into R, the station values were read in as numeric values, so R plotted them on a continuous color scale. But the station number can really be thought of as discrete data - they didn't *need* to be numbers, they could have been letters, or names. The station numbers are separate from each other - not part of a continuous scale. So, to plot them as four separate entities, we need to tell R to treat the station column as factors (as it would have automatically done if the stations were named "A", "B", "C", and "D").
2. Create boxplots looking at the distribution of temperature_degC by station (tip: change the station values to factors)
```{r, fig.height = 5, fig.width=5}
ggplot(data = fieldData, mapping = aes(x = factor(station), y = temperature_degC)) +
geom_boxplot()
```
3. Plot temperature by depth for samples from 2016, coloring the points by station
```{r, fig.height = 5, fig.width=6}
datasubset <- fieldData %>% filter(date==20160908)
ggplot(data = datasubset, mapping = aes(x = temperature_degC, y = depth_m)) +
geom_point(aes(color=factor(station)))
```
## Challenge B
What are the steps we need to take to manipulate our data into a data frame with three columns: cruise, station and fluorescence averaged over the top 2 m? Describe the step and say the associated `dplyr` data frame manipulation functions you would use to do it.
1. Select the rows where the depth is 2 m or less (use `filter`)
2. Separate the data into date and station groups (use `group_by`)
3. Take the average for each group of data (use `summarize`)
Use the pipes to connect all three steps together.
## Challenge C
1. Create a `geom_tile` plot for temperature_degC from the cruise that took place on Septh 12th 2017.
```{r, fig.height = 5, fig.width=7}
cruiseData <- filter(fieldData, date==20160908)
ggplot(cruiseData,aes(x=station,y=depth_m)) +
geom_tile(aes(fill=temperature_degC)) +
labs(fill='temperature (degC') +
scale_y_reverse()
```
2. Are there any examples from your own data that you could plot in this way?
[Here's an example with genomic data](https://science.sciencemag.org/content/sci/358/6366/1046/F2.large.jpg) that was created using `geom_tile`.
## Challenge D
Create a plot like the above that shows temperature interpolated by depth and latitude for the cruise that took place on Sept 12th 2017.
```{r}
#filter based on cruise date
cruiseData <- filter(fieldData, date==20170912)
#interpolate our data:
interpReference <- interp(cruiseData$latitude, cruiseData$depth_m, cruiseData$temperature_degC)
# making data frame from all combinations of latitude and depth
CruiseDataInterp <- expand.grid(latitude=interpReference$x,
depth = interpReference$y) %>%
mutate(temperature = as.vector(interpReference$z))
ggplot(CruiseDataInterp, aes(x=latitude, y=depth)) +
geom_tile(aes(fill = temperature)) +
geom_contour(aes(z = temperature),color="white") +
geom_point(data = cruiseData, aes(x=latitude, y=depth_m),color="black") + #adding in the measurement locations
scale_y_reverse() +
scale_x_reverse() + #so station1 is on the left and station4 is on the right
labs(y="Depth (m)", fill="temperature (degC)") +
scale_fill_distiller(palette="Greens",direction=1)
```
## Challenge E
Create a plot like the above that shows fluorescence interpolated by depth and latitude for every cruise in 2016 (one plot per cruise). To get a list of all the dates of cruises in 2016 do `unique(data2016$date)` and look at the object printed to the console.
Reminder of the function:
```{r}
cruiseInterpolationPlot <- function(dataframe, variable, cruiseDate, colorlabel){
# return a heatmap style plot for a variable interpolated over latitude and
# depth
# Inputs: dataframe = raw data frame of the DamariscottaRiverData
# variable = data frame column name (as a string) of the variable to
# interpolate
# cruiseData = numeric value of date in yyyymmdd (to compare with the
# dataframe$date column)
# colorlabel = label for the color bar (as a string)
cruiseData <- dataframe %>% filter(date == cruiseDate)
#interpolate our data:
interpReference <- interp(cruiseData$latitude, cruiseData$depth_m, cruiseData[[variable]])
newdf <- expand.grid(latitude=interpReference$x,
depth_m = interpReference$y) %>%
mutate(varname = as.vector(interpReference$z))
p <- ggplot(newdf, aes(x=latitude, y=depth_m)) +
geom_tile(aes(fill = varname)) +
geom_contour(aes(z = varname),color="white") +
geom_point(data = cruiseData, aes(x=latitude, y=depth_m),color="black") + #adding in the measurement locations
scale_y_reverse() +
scale_x_reverse() + #so station1 is on the left and station4 is on the right
labs(y="Depth (m)", fill=colorlabel, title = cruiseDate) +
scale_fill_distiller(palette="Greens",direction=1)
return(p)
}
```
```{r}
data2016 <- fieldData %>% filter(year == 2016)
unique(data2016$date)
cruiseInterpolationPlot(fieldData,'fluorescence_mg_m3',20160920,'fluorescence')
cruiseInterpolationPlot(fieldData,'fluorescence_mg_m3',20161004,'fluorescence')
cruiseInterpolationPlot(fieldData,'fluorescence_mg_m3',20161019,'fluorescence')
cruiseInterpolationPlot(fieldData,'fluorescence_mg_m3',20161101,'fluorescence')
```
## Challenge F
Adapt the `cruiseInterpolationPlot` function such that the x, y and color bar limits are determined from input arguments and re-plot all the fluorescence data for each cruise in 2016. (Hint: use the `xlim` and `ylim` functions with `ggplot` and include the `limits` argument in the `scale_fill_distiller` function.)
```{r}
cruiseInterpolationPlot <- function(dataframe, variable, cruiseDate, colorlabel, xlims, ylims, clims){
# return a heatmap style plot for a variable interpolated over latitude and
# depth
# Inputs: dataframe = raw data frame of the DamariscottaRiverData
# variable = data frame column name (as a string) of the variable to
# interpolate
# cruiseData = numeric value of date in yyyymmdd (to compare with the
# dataframe$date column)
# colorlabel = label for the color bar (as a string)
# xlims = vector with min and max values for x-axis
# ylims = vector with min and max values for y-axis
# clims = vector with min and max values for colorbar
cruiseData <- dataframe %>% filter(date == cruiseDate)
#interpolate our data:
interpReference <- interp(cruiseData$latitude, cruiseData$depth_m, cruiseData[[variable]])
newdf <- expand.grid(latitude=interpReference$x,
depth_m = interpReference$y) %>%
mutate(varname = as.vector(interpReference$z))
p <- ggplot(newdf, aes(x=latitude, y=depth_m)) +
geom_tile(aes(fill = varname)) +
geom_contour(aes(z = varname),color="white") +
geom_point(data = cruiseData, aes(x=latitude, y=depth_m),color="black") +
labs(y="Depth (m)", fill=colorlabel, title = cruiseDate) +
scale_fill_distiller(palette="Greens",direction=1, limits=clims) +
xlim(xlims) +
ylim(ylims)
return(p)
}
```
```{r}
data2016 <- fieldData %>% filter(year == 2016)
alldates <- unique(data2016$date)
for (dd in alldates){
print(cruiseInterpolationPlot(fieldData,'fluorescence_mg_m3',dd,
'fluorescence', c(43.91,43.75), c(105,0), c(0,12)))
}
```