To get some hands-on experience with the Toolbox, we will explore how Ocean Health Index (OHI) scores respond when add different data to the LSP goal.
You will be using the OHI Toolbox software to do this, and follow the instructions here (workshop-toolbox.Rmd
). We will walk through line-by-line together and execute the code in R; the order does matter so be sure to proceed line-by-line.
Your final figures will be saved as .pngs within the sam
folder you downloaded: sam/toolbox-demo/reports/figures
.
sam
repositoryWe’ll be playing with the Toolbox for the global OHI assessments; we calculate scores each year for 220 coastal nations and territories. We have copied this into a GitHub repository called sam
for Samoa. Let’s get it on your computer.
sam
repository (called ‘repo’).sam
repo on your computer and rename it to sam
if it has any other name. Double-click the sam.Rproj
file. This will open RStudio (which is how you should always interface with R).toolbox-demo
folder. Inside is a mix of .R
scripts and .csv
files. We will just focus on a few of them.Since we’re in RStudio with a .Rproj
file, our current working directory is wherever/you/saved/it/sam
. All of our work in the Toolbox happens in the toolbox-demo
folder within the sam
folder. (Even these instructions are within the toolbox-demo
folder!) But we need to let R know that we want to be working inside the toolbox-demo
folder. Let’s change the working directory to the toolbox-demo
folder, and install the packages we need to get going.
Run the following code:
## set working directory to sam/toolbox-demo with setwd()
setwd('toolbox-demo')
## install the packages we need.
## tidyverse is by RStudio, for "data wrangling"
install.packages("tidyverse")
library(tidyverse)
## devtools helps install our ohicore software
install.packages('devtools') # do this just one time!
library(devtools) # do this every time you begin
devtools::install_github("ohi-science/ohicore@dev") # do this just one time!
library(ohicore) # do this every time you begin
## source the function to make flower plots; this won't do anything yet, but it makes it available to us.
source('plot_flower_local.r')
There are 220 countries that will be in our data set below, and we will just want to change information for Samoa. Samoa’s region identifier number is 152 (full list here).
## identify region identifier for Samoa
rgn_id_sam <- 152
First read in new data. In this example we have saved the new WDPA data from May 2018 in a folder called workshop-data.
The data is already tidy!
wdpa <- read_csv("workshop-data/WDPA_May2018.csv")
## Parsed with column specification:
## cols(
## .default = col_character(),
## WDPAID = col_integer(),
## WDPA_PID = col_integer(),
## PA_DEF = col_integer(),
## MARINE = col_integer(),
## REP_M_AREA = col_double(),
## REP_AREA = col_double(),
## NO_TK_AREA = col_integer(),
## STATUS_YR = col_integer(),
## METADATAID = col_integer()
## )
## See spec(...) for full column specifications.
wdpa %>%
select(NAME, REP_M_AREA, STATUS, STATUS_YR, NO_TK_AREA)
## # A tibble: 6 x 5
## NAME REP_M_AREA STATUS STATUS_YR NO_TK_AREA
## <chr> <dbl> <chr> <int> <int>
## 1 Ava o sina 0.0100 Designated 1999 0
## 2 punaoa 0.0700 Designated 1999 0
## 3 Afaga Tele 0.510 Designated 1999 0
## 4 Vaiee 0.770 Designated 1999 0
## 5 Fausaga 0.280 Designated 1999 0
## 6 Faaofi laulu 0.200 Designated 1999 0
Summarize the total areas within MPAs:
mpa_area_total <- wdpa %>%
group_by(STATUS_YR) %>%
mutate(AREA_KM2 = sum(REP_M_AREA)) %>%
select(NAME, REP_M_AREA, STATUS, STATUS_YR, NO_TK_AREA, AREA_KM2)
mpa_area_total
## # A tibble: 6 x 6
## # Groups: STATUS_YR [1]
## NAME REP_M_AREA STATUS STATUS_YR NO_TK_AREA AREA_KM2
## <chr> <dbl> <chr> <int> <int> <dbl>
## 1 Ava o sina 0.0100 Designated 1999 0 1.84
## 2 punaoa 0.0700 Designated 1999 0 1.84
## 3 Afaga Tele 0.510 Designated 1999 0 1.84
## 4 Vaiee 0.770 Designated 1999 0 1.84
## 5 Fausaga 0.280 Designated 1999 0 1.84
## 6 Faaofi laulu 0.200 Designated 1999 0 1.84
mpa_area_total_sam <- unique(mpa_area_total$AREA_KM2)
So since all were designated in the same year, 1.84 is the total recorded since 1999.
Inspect LSP data layer that already exists:
lsp_prot_area_offshore3nm <- read_csv("layers/lsp_prot_area_offshore3nm.csv")
## Parsed with column specification:
## cols(
## rgn_id = col_integer(),
## year = col_integer(),
## a_prot_3nm = col_double()
## )
## display
lsp_prot_area_offshore3nm %>%
filter(rgn_id == rgn_id_sam)
## Warning: package 'bindrcpp' was built under R version 3.4.4
## # A tibble: 18 x 3
## rgn_id year a_prot_3nm
## <int> <int> <dbl>
## 1 152 2000 1.84
## 2 152 2001 1.84
## 3 152 2002 1.84
## 4 152 2003 1.84
## 5 152 2004 1.84
## 6 152 2005 1.84
## 7 152 2006 1.84
## 8 152 2007 1.84
## 9 152 2008 1.84
## 10 152 2009 1.84
## 11 152 2010 1.84
## 12 152 2011 1.84
## 13 152 2012 1.84
## 14 152 2013 1.84
## 15 152 2014 1.84
## 16 152 2015 1.84
## 17 152 2016 1.84
## 18 152 2017 1.84
Since the WDPA data shows that all were designated in 1999, and the OHI LSP data layer begins with 2000, we can inject 1.84 for all years.
Inject in LSP data layer.
## base-R
# lsp_prot_area_offshore3nm$a_prot_3nm[lsp_prot_area_offshore3nm$rgn_id == rgn_id_sam] <- mpa_area_total_sam
## tidyverse
lsp_new <- rbind(
lsp_prot_area_offshore3nm %>%
filter(rgn_id != rgn_id_sam),
lsp_prot_area_offshore3nm %>%
filter(rgn_id == rgn_id_sam) %>%
mutate(a_prot_3nm = mpa_area_total_sam)) %>%
arrange(rgn_id, year)
## save new data in existing layer
write_csv(lsp_new, "layers/lsp_prot_area_offshore3nm.csv")
We can check in the differences in GitHub and see that Samoa’s data have been updated.
source("calculate_scores.R")
## cntry_rgn
## rgn_area
## rgn_georegions
## rgn_global
## rgn_labels
## element_wts_cp_km2_x_protection
## element_wts_cs_km2_x_storage
## element_wts_hab_pres_abs
## ao_access
## ao_need
## ss_spi
## ss_wgi
## hd_intertidal
## sp_alien
## hd_subtidal_hb
## fp_art_hb
## fp_com_hb
## fp_com_lb
## hd_subtidal_sb
## res_spi
## wgi_all
## hd_habitat
## hd_mpa_coast
## fp_habitat
## fp_mora
## fp_mpa_coast
## species_diversity_3nm
## hab_coral_extent
## hab_seaice_extent
## cc_acid
## cc_sst
## cc_slr
## cc_uv
## po_water
## hab_coral_health
## hab_coral_trend
## hab_seaice_health
## hab_seaice_trend
## hab_mangrove_extent
## hab_saltmarsh_extent
## hab_seagrass_extent
## hab_mangrove_health
## hab_mangrove_trend
## hab_saltmarsh_health
## hab_saltmarsh_trend
## hab_seagrass_health
## hab_seagrass_trend
## cw_chemical_trend
## cw_nutrient_trend
## cw_pathogen_trend
## cw_trash_trend
## po_nutrients_3nm
## po_chemicals_3nm
## po_trash
## po_pathogens
## le_gdp
## le_rev_cur_adj_value
## le_rev_cur_base_value
## le_rev_ref_adj_value
## le_rev_ref_base_value
## le_rev_sector_year
## le_revenue_adj
## fp_art_lb
## po_chemicals
## po_nutrients
## sp_genetic
## li_gci
## le_sector_weight
## fis_b_bmsy
## fis_meancatch
## species_diversity_eez
## fp_mora_artisanal
## fp_mpa_eez
## hd_mpa_eez
## fp_wildcaught_weight
## hab_softbottom_health
## hab_softbottom_trend
## g_mariculture
## g_tourism
## ico_spp_iucn_status
## fp_targetharvest
## g_cites
## le_gdp_pc_ppp
## le_jobs_cur_adj_value
## le_jobs_cur_base_value
## le_jobs_ref_adj_value
## le_jobs_ref_base_value
## le_jobs_sector_year
## le_popn
## le_unemployment
## le_wage_cur_adj_value
## le_wage_cur_base_value
## le_wage_ref_adj_value
## le_wage_ref_base_value
## le_wage_sector_year
## le_workforcesize_adj
## li_sector_evenness
## lsp_prot_area_inland1km
## lsp_prot_area_offshore3nm
## rgn_area_inland1km
## rgn_area_offshore3nm
## mar_coastalpopn_inland25mi
## mar_harvest_tonnes
## mar_sustainability_score
## g_msi_gov
## hab_softbottom_extent
## sp_alien_species
## hab_rockyreef_extent
## np_blast
## np_cyanide
## np_harvest_product_weight
## np_harvest_tonnes
## np_harvest_tonnes_relative
## spp_status
## spp_trend
## tr_jobs_pct_tourism
## tr_sustainability
## tr_travelwarnings
## Unused fields...
## le_rev_sector_year: analysis_year
## ico_spp_iucn_status: iucn_sid
## le_jobs_sector_year: analysis_year
## le_wage_sector_year: analysis_year
## Rows duplicated...
## le_rev_sector_year: 3272
## ico_spp_iucn_status: 8738
## le_jobs_sector_year: 2174
## le_wage_sector_year: 546
## Layers missing data, ie all NA ...
## element_wts_cp_km2_x_protection: element_wts_cp_km2_x_protection.csv
## element_wts_cs_km2_x_storage: element_wts_cs_km2_x_storage.csv
## element_wts_hab_pres_abs: element_wts_hab_pres_abs.csv
## Layer element_wts_cp_km2_x_protection has no rows of data.
## Layer element_wts_cs_km2_x_storage has no rows of data.
## Layer element_wts_hab_pres_abs has no rows of data.
## Calculating Status and Trend for each region for FIS...
## Calculating Status and Trend for each region for MAR...
## Calculating Status and Trend for each region for AO...
## Calculating Status and Trend for each region for NP...
## Calculating Status and Trend for each region for CS...
## Calculating Status and Trend for each region for CP...
## Calculating Status and Trend for each region for TR...
## Calculating Status and Trend for each region for LIV...
## Calculating Status and Trend for each region for ECO...
##
## data values are NA so removed: 6 of 185 rows
## Calculating Status and Trend for each region for ICO...
## Calculating Status and Trend for each region for LSP...
## Calculating Status and Trend for each region for CW...
## Calculating Status and Trend for each region for HAB...
## Warning in HAB(layers): Some regions/habitats have extent data, but no
## trend data. Consider estimating these values.
## Warning in HAB(layers): Some regions/habitats have extent data, but no
## health data. Consider estimating these values.
## Calculating Status and Trend for each region for SPP...
## Calculating Pressures for each region...
## There are 6 pressures subcategories: pollution, alien_species, habitat_destruction, fishing_pressure, climate_change, social
## These goal-elements are in the weighting data layers, but not included in the pressure_matrix.csv:
## LIV-aqf, ECO-ph, ECO-sb, ECO-tran
## Calculating Resilience for each region...
## There are 7 Resilience subcategories: ecological, alien_species, goal, fishing_pressure, habitat_destruction, pollution, social
## Calculating Goal Score and Likely Future for each region for FIS...
## Calculating Goal Score and Likely Future for each region for MAR...
## Calculating Goal Score and Likely Future for each region for AO...
## Calculating Goal Score and Likely Future for each region for NP...
## Calculating Goal Score and Likely Future for each region for CS...
## Calculating Goal Score and Likely Future for each region for CP...
## Calculating Goal Score and Likely Future for each region for TR...
## Calculating Goal Score and Likely Future for each region for LIV...
## Calculating Goal Score and Likely Future for each region for ECO...
## Calculating Goal Score and Likely Future for each region for ICO...
## Calculating Goal Score and Likely Future for each region for LSP...
## Calculating Goal Score and Likely Future for each region for CW...
## Calculating Goal Score and Likely Future for each region for HAB...
## Calculating Goal Score and Likely Future for each region for SPP...
## Calculating post-Index function for each region for FP...
## Warning in FP(layers, scores): Check: these regions have a MAR weight but
## no score: 1, 2, 3, 4, 11, 12, 26, 28, 30, 33, 34, 35, 36, 38, 39, 44, 46,
## 49, 55, 56, 57, 58, 59, 60, 63, 64, 69, 74, 77, 85, 86, 88, 89, 90, 91, 92,
## 93, 94, 96, 97, 98, 99, 100, 103, 104, 105, 106, 107, 108, 113, 114, 117,
## 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 144, 145, 146, 148, 149,
## 150, 151, 154, 156, 157, 158, 159, 161, 169, 178, 185, 189, 192, 193, 194,
## 195, 197, 198, 199, 200, 215, 220, 221, 227, 228, 237, 248, 249
## Warning in FP(layers, scores): Check: these regions have a MAR score but no
## weight: 41
## Calculating post-Index function for each region for LE...
## Calculating post-Index function for each region for SP...
## Calculating post-Index function for each region for BD...
## Calculating Index Score for each region using goal weights to combine goal scores...
## Calculating Index Likely Future State for each region...
## Calculating Post-process PreGlobalScores() function for each region...
## Calculating scores for ASSESSMENT AREA (region_id=0) by area weighting...
## Calculating FinalizeScores function...
# visualize scores ----
## Flower plots for each region ----
source('plot_flower_local.R')
## plot.
PlotFlower(region_plot = rgn_id_sam,
assessment_name = "Samoa",
dir_fig_save = "reports/figures/flower_new")
## Parsed with column specification:
## cols(
## order_color = col_double(),
## order_hierarchy = col_double(),
## order_calculate = col_integer(),
## goal = col_character(),
## parent = col_character(),
## name = col_character(),
## name_flower = col_character(),
## description = col_character(),
## weight = col_double(),
## preindex_function = col_character(),
## postindex_function = col_character()
## )
## Using layers/fp_wildcaught_weight.csv to plot FIS and MAR with unequal weighting
## Parsed with column specification:
## cols(
## rgn_id = col_integer(),
## year = col_integer(),
## w_fis = col_double()
## )
## Parsed with column specification:
## cols(
## rgn_id = col_integer(),
## rgn_name = col_character(),
## eez_iso3 = col_character(),
## territory = col_character(),
## admin_rgn_id = col_integer(),
## admin_country_name = col_character(),
## Notes = col_character()
## )
## Warning in plot_df$weight[plot_df$goal == "FIS"] <- w$w_fis[w$rgn_id == :
## number of items to replace is not a multiple of replacement length
## Warning in plot_df$weight[plot_df$goal == "MAR"] <- 1 - w$w_fis[w$rgn_id
## == : number of items to replace is not a multiple of replacement length
## Warning: Removed 1 rows containing missing values (geom_bar).
## Warning: Removed 1 rows containing missing values (geom_bar).
Inspect new scores. The best way to do this is by looking at the GitHub differences in scores.csv and in the flower plot.
The overall Index score changed slightly, and the LSP goal did as well.
In this example, the change is too small to be visible in the flower plot, but that is what we would expect from such a small change in data. And LSP is just one goal.
We can also look at the scores in R:
scores <- read_csv("scores.csv")
## Parsed with column specification:
## cols(
## goal = col_character(),
## dimension = col_character(),
## region_id = col_integer(),
## score = col_double()
## )
scores %>%
filter(region_id == rgn_id_sam,
goal == "LSP")
## # A tibble: 6 x 4
## goal dimension region_id score
## <chr> <chr> <int> <dbl>
## 1 LSP future 152 1.31
## 2 LSP pressures 152 33.0
## 3 LSP resilience 152 76.7
## 4 LSP score 152 1.23
## 5 LSP status 152 1.15
## 6 LSP trend 152 0.