-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.Rmd
More file actions
216 lines (157 loc) · 6.6 KB
/
README.Rmd
File metadata and controls
216 lines (157 loc) · 6.6 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
---
title: "IP33 results conversion"
output: github_document
---
Converting results of IP33 to spatial data for inclusion in data viewer.
This script reads data from three text (csv) files:
- [Copepod data_2018to20.csv](#copepod)
- [Fish acoustic abd data 2018to20_copy4OBAMA.csv](#acoustic)
- [trawl_fish abd_size quantileranges_copy_OBAMAIP33.csv](#trawl)
The data from each file is converted to spatial data, using the coordinates included. The spatial data are then [exported](#export) as shape files.
The results are gathered in this [zip file](obama_next_ip33.zip).
For a simple R script without any markdown, use [ip33_convert_vector.R](R/ip33_convert_vector.R).
```{r libraries, message=FALSE, warning=FALSE}
library(dplyr)
library(tidyr)
library(sf)
library(ggplot2)
library(patchwork)
library(zip)
```
## Copepod data_2018to20.csv []{#copepod}
Read the data file.
```{r read-data-1}
file1 <- "Copepod data_2018to20.csv"
df1 <- read.csv(paste0("data/",file1), sep=",", header=T)
print(df1 %>% slice(1:10), row.names = FALSE)
```
Create spatial data.
```{r convert-sf-1}
shp1 <- sf::st_as_sf(df1, coords = c("lon","lat"), crs=4326)
```
Plot the data.
```{r plot-data-1}
p1 <- ggplot() +
geom_sf(data = shp1, aes(colour=cop_abun_m3), size=0.2) +
scale_x_continuous(breaks=c(-6,-4,-2,0)) +
scale_y_continuous(breaks=c(49,50,51,52)) +
theme_minimal() +
scale_color_distiller(palette = "Spectral")
ggsave(p1, filename="plot/Copepod data_2018to20.png",
height=12, width=16, units="cm", dpi=300)
```
{width=500}
## Fish acoustic abd data 2018to20_copy4OBAMA.csv []{#acoustic}
Read the data file.
```{r read-data-2}
file2 <- "Fish acoustic abd data 2018to20_copy4OBAMA.csv"
df2 <- read.csv(paste0("data/",file2), sep=",", header=T)
print(df2 %>% slice(1:10), row.names = FALSE)
```
Create spatial data.
```{r convert-sf-2}
shp2 <- sf::st_as_sf(df2, coords = c("longiture","latitud"), crs=4326)
```
Plot the data.
```{r plot-data-2}
p2 <- ggplot() +
geom_sf(data = shp2, aes(colour=abundance_indv_m2), size=0.2) +
theme_minimal() +
scale_x_continuous(breaks=c(-6,-4,-2,0)) +
scale_y_continuous(breaks=c(49,50,51,52)) +
facet_wrap(taxa~., ncol=3) +
theme(legend.position.inside = c(0.85, 0.2),
legend.position = "inside") +
scale_color_distiller(palette = "Spectral")
ggsave(p2, filename="plot/Fish acoustic abd data 2018to20_copy4OBAMA.png",
height=12, width=16, units="cm", dpi=300)
```
{width=500}
## trawl_fish abd_size quantileranges_copy_OBAMAIP33.csv []{#trawl}
Read the data file.
```{r read-data-3}
file3 <- "trawl_fish abd_size quantileranges_copy_OBAMAIP33.csv"
df3 <- read.csv(paste0("data/",file3), sep=",", header=T)
print(df3 %>% slice(1:10), row.names = FALSE)
```
Create spatial data.
```{r convert-sf-3}
shp3 <- sf::st_as_sf(df3, coords = c("lon","lat"), crs=4326)
```
Plot the data.
```{r plot-data-3}
p3a <- ggplot() +
geom_sf(data = shp3, aes(colour=pct_below_Q1), size=1) +
theme_minimal() +
scale_x_continuous(breaks=c(-6,-4,-2,0)) +
scale_y_continuous(breaks=c(49,50,51,52)) +
facet_wrap(fldMainSpeciesCode~., ncol=2) +
theme(legend.position = "bottom",
legend.title = element_text(size=rel(0.8))) +
scale_color_distiller(palette = "Spectral", direction = 1)
p3b <- ggplot() +
geom_sf(data = shp3, aes(colour=pct_above_Q3), size=1) +
theme_minimal() +
scale_x_continuous(breaks=c(-6,-4,-2,0)) +
scale_y_continuous(breaks=c(49,50,51,52)) +
facet_wrap(fldMainSpeciesCode~., ncol=2) +
theme(legend.position = "bottom",
legend.title = element_text(size=rel(0.8))) +
scale_color_distiller(palette = "Spectral")
p3 <- p3a + p3b
ggsave(p3, filename="plot/trawl_fish abd_size quantileranges_copy_OBAMAIP33.png",
height=16, width=16, units="cm", dpi=300)
```
{width=500}
## Save shape files []{#export}
Some of the column names in the data frames need to be shortened before exporting to shape file.
We will keep a record of the changes and store them in a text file.
```{r save-shape-files}
# > names(shp1)
# [1] "X" "bindatetime" "cop_abun_m3" "copepod_length_geomean_mm"
# [5] "Lifeform_large_copepod.....2mm" "lifeform_small_copepod....2mm" "geometry"
names_dat1 <- names(shp1)
names(shp1) <- c("id","datetime", "abund_m3", "len_mm", "n_large", "n_small", "geometry")
names_shp1 <- names(shp1)
# > names(shp2)
# [1] "date" "time" "taxa" "instrument" "abundance_indv_m2" "X"
# [7] "X.1" "X.2" "X.3" "geometry"
names_dat2 <- names(shp2)
names(shp2)[names(shp2)=="abundance_indv_m2"] <- "abund_m2"
names_shp2 <- names(shp2)
# > names(shp3)
# [1] "X" "fldCruiseName" "fldCruiseStationNumber" "fldMainSpeciesCode" "year"
# [6] "fldDateTimeShot" "total_fish" "pct_below_Q1" "pct_above_Q3" "geometry"
names_dat3 <- names(shp3)
names(shp3) <- c("id","cruisename", "stn_no", "spcs_code", "year",
"time", "tot_fish", "pct_lt_Q1","pct_gt_Q3", "geometry")
names_shp3 <- names(shp3)
file1 <- rep("Copepod data_2018to20.shp", length(names_shp1))
file2 <- rep("Fish acoustic abd data 2018to20_copy4OBAMA.shp", length(names_shp2))
file3 <- rep("trawl_fish abd_size quantileranges_copy_OBAMAIP33.shp", length(names_shp3))
df <- data.frame(file = c(file1, file2, file3),
column_shape=c(names_shp1,names_shp2,names_shp3),
column_data=c(names_dat1,names_dat2,names_dat3))
# save the table with column names
write.table(df, file = "shp/columns_renamed.csv", row.names = F, sep=",")
# save the shape files
sf::st_write(shp1, "shp/Copepod data_2018to20.shp", append=F)
sf::st_write(shp2, "shp/Fish acoustic abd data 2018to20_copy4OBAMA.shp", append=F)
sf::st_write(shp3, "shp/trawl_fish abd_size quantileranges_copy_OBAMAIP33.shp", append=F)
```
The column names conversions are in [columns_renamed.csv](shp/columns_renamed.csv)
This file has 3 columns:
* _file_ - the name of the shape file
* _column_shape_ - the name of a column in the shape file
* _column_data_ - the name of the corresponding column in the original data file*
\*Note: column names beginning with **X** in _column_data_ were automatically
assigned by the ´read.csv()´ function. This is because these columns did not
have names in the original csv files.
## Zip the files
Collect shape files and png files in a zip file.
```{r zip-files}
files_plot <- list.files(path="plot", full.names = T)
files_shp <- list.files(path="shp", full.names = T)
files_zip <- c(files_shp, files_plot)
zip(zipfile="obama_next_ip33.zip", files=files_zip)
```