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
- Fish acoustic abd data 2018to20_copy4OBAMA.csv
- trawl_fish abd_size quantileranges_copy_OBAMAIP33.csv
The data from each file is converted to spatial data, using the coordinates included. The spatial data are then exported as shape files.
The results are gathered in this zip file.
For a simple R script without any markdown, use ip33_convert_vector.R.
library(dplyr)
library(tidyr)
library(sf)
library(ggplot2)
library(patchwork)
library(zip)Read the data file.
file1 <- "Copepod data_2018to20.csv"
df1 <- read.csv(paste0("data/",file1), sep=",", header=T)
print(df1 %>% slice(1:10), row.names = FALSE)## X bindatetime lat lon cop_abun_m3 copepod_length_geomean_mm
## 1 07/10/2018 22:35 51.21887 -5.023092 140.90909 0.4370561
## 2 07/10/2018 22:45 51.21787 -5.022329 145.45455 0.4336911
## 3 07/10/2018 22:54 51.21597 -5.020511 140.90909 0.5735332
## 4 07/10/2018 23:05 51.21355 -5.011474 122.72727 0.4648441
## 5 07/10/2018 23:14 51.21328 -4.971726 122.72727 0.4992295
## 6 07/10/2018 23:24 51.21535 -4.917730 127.27273 0.4292085
## 7 07/10/2018 23:35 51.21711 -4.863540 95.45455 0.3693337
## 8 07/10/2018 23:44 51.22316 -4.817666 81.81818 0.4729216
## 9 07/10/2018 23:55 51.23268 -4.761857 86.36364 0.4780368
## 10 08/10/2018 00:04 51.24034 -4.718240 63.63636 0.4483736
## Lifeform_large_copepod.....2mm lifeform_small_copepod....2mm
## 0 31
## 0 32
## 0 31
## 0 27
## 0 27
## 0 28
## 0 21
## 0 18
## 0 19
## 0 14
Create spatial data.
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)Read the data file.
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)## date time latitud longiture taxa instrument
## 08/10/2018 07:23:10 51.51146 -4.790903 SPR AcousticsANDcatch
## 31/10/2018 15:06:51 50.45339 -3.289193 SPR AcousticsANDcatch
## 15/10/2019 09:43:00 50.23263 -3.424200 SPR AcousticsANDcatch
## 15/10/2019 11:18:00 50.49879 -3.436185 SPR AcousticsANDcatch
## 18/10/2020 16:12:00 48.75885 -5.135397 PIL AcousticsANDcatch
## 16/10/2020 09:00:00 50.13545 -4.584627 PIL AcousticsANDcatch
## 27/10/2020 14:11:00 52.34751 -4.309790 SPR AcousticsANDcatch
## 08/10/2019 16:01:00 50.29788 -6.968951 HOM AcousticsANDcatch
## 08/10/2020 07:57:00 50.48520 -3.436575 SPR AcousticsANDcatch
## 21/10/2020 17:30:00 49.86663 -6.099876 PIL AcousticsANDcatch
## abundance_indv_m2 X X.1 X.2 X.3
## 34.35667 NA NA
## 31.40450 NA NA
## 24.03991 NA NA
## 21.52060 NA NA
## 16.69828 NA NA
## 15.06479 NA NA
## 14.91213 NA NA SPR Sprat
## 14.30879 NA NA PIL Pilchard
## 13.86649 NA NA HOM Horse Mackerel
## 13.75491 NA NA HER Herring
Create spatial data.
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)Read the data file.
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)## X fldCruiseName fldCruiseStationNumber fldMainSpeciesCode year
## 1 CEND 15/19 5 HER 2019
## 2 CEND 15/19 5 HOM 2019
## 3 CEND 15/19 5 MAC 2019
## 4 CEND 15/19 5 SPR 2019
## 5 CEND 15/19 16 ANE 2019
## 6 CEND 15/19 16 HER 2019
## 7 CEND 15/19 16 HOM 2019
## 8 CEND 15/19 16 MAC 2019
## 9 CEND 15/19 16 PIL 2019
## 10 CEND 15/19 16 SPR 2019
## fldDateTimeShot lat lon total_fish pct_below_Q1 pct_above_Q3
## 01/10/2019 15:30 51.311 -3.823 119 98.319328 0.840336134
## 01/10/2019 15:30 51.311 -3.823 47 0.000000 0.000000000
## 01/10/2019 15:30 51.311 -3.823 1 100.000000 0.000000000
## 01/10/2019 15:30 51.311 -3.823 2302 72.980017 2.258905300
## 02/10/2019 17:28 51.583 -4.437 16 25.000000 0.000000000
## 02/10/2019 17:28 51.583 -4.437 804 75.000000 0.248756219
## 02/10/2019 17:28 51.583 -4.437 21 4.761905 4.761904762
## 02/10/2019 17:28 51.583 -4.437 190 3.684211 2.105263158
## 02/10/2019 17:28 51.583 -4.437 30 100.000000 0.000000000
## 02/10/2019 17:28 51.583 -4.437 85546 40.474131 0.004675847
Create spatial data.
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)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.
# > 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)## Deleting layer `Copepod data_2018to20' using driver `ESRI Shapefile'
## Writing layer `Copepod data_2018to20' to data source
## `shp/Copepod data_2018to20.shp' using driver `ESRI Shapefile'
## Writing 6961 features with 6 fields and geometry type Point.
sf::st_write(shp2, "shp/Fish acoustic abd data 2018to20_copy4OBAMA.shp", append=F)## Deleting layer `Fish acoustic abd data 2018to20_copy4OBAMA' using driver `ESRI Shapefile'
## Writing layer `Fish acoustic abd data 2018to20_copy4OBAMA' to data source
## `shp/Fish acoustic abd data 2018to20_copy4OBAMA.shp' using driver `ESRI Shapefile'
## Writing 16380 features with 9 fields and geometry type Point.
sf::st_write(shp3, "shp/trawl_fish abd_size quantileranges_copy_OBAMAIP33.shp", append=F)## Deleting layer `trawl_fish abd_size quantileranges_copy_OBAMAIP33' using driver `ESRI Shapefile'
## Writing layer `trawl_fish abd_size quantileranges_copy_OBAMAIP33' to data source
## `shp/trawl_fish abd_size quantileranges_copy_OBAMAIP33.shp' using driver `ESRI Shapefile'
## Writing 509 features with 9 fields and geometry type Point.
The column names conversions are in 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.
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)

