-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip33_convert_vector.R
More file actions
153 lines (106 loc) · 4.91 KB
/
ip33_convert_vector.R
File metadata and controls
153 lines (106 loc) · 4.91 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
library(dplyr)
library(tidyr)
library(sf)
library(terra)
library(ggplot2)
library(patchwork)
library(zip)
# Converting results of IP33 to spatial data for inclusion in data viewer.
# ---------- Copepod data_2018to20.csv ----------
# Read the data file.
file1 <- "Copepod data_2018to20.csv"
df1 <- read.csv(paste0("data/",file1), sep=",", header=T)
shp1 <- sf::st_as_sf(df1, coords = c("lon","lat"), crs=4326)
# Plot the data.
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)
# ---------- Fish acoustic abd data 2018to20_copy4OBAMA.csv ----------
# Read the data file.
file2 <- "Fish acoustic abd data 2018to20_copy4OBAMA.csv"
df2 <- read.csv(paste0("data/",file2), sep=",", header=T)
shp2 <- sf::st_as_sf(df2, coords = c("longiture","latitud"), crs=4326)
# Plot the data.
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)
# ---------- trawl_fish abd_size quantileranges_copy_OBAMAIP33.csv ----------
# Read the data file.
file3 <- "trawl_fish abd_size quantileranges_copy_OBAMAIP33.csv"
df3 <- read.csv(paste0("data/",file3), sep=",", header=T)
shp3 <- sf::st_as_sf(df3, coords = c("lon","lat"), crs=4326)
# Plot the data.
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)
# ---------- Save shape files ----------
# Some of the column names need to be shortened before saving as shape file.
# We will keep a record of the changes and store them in a text file.
# > 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)
# ---------- Zip the files ----------
# Collect shape files and png files in a zip file.
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)