From 98de03abfd7fdcadeeeb329940e6e451e0c8da2e Mon Sep 17 00:00:00 2001 From: annaramji Date: Mon, 19 Aug 2024 21:03:40 +0000 Subject: [PATCH 1/2] docs: added plotting methods for comparing data in NP step 2, comments on differences create: added 2024 np_product_weights.csv to output folder --- .../np/v2024/STEP2_np_weighting_prep.Rmd | 287 +++++++- .../np/v2024/output/np_product_weights.csv | 661 ++++++++++++++++++ 2 files changed, 936 insertions(+), 12 deletions(-) create mode 100644 globalprep/np/v2024/output/np_product_weights.csv diff --git a/globalprep/np/v2024/STEP2_np_weighting_prep.Rmd b/globalprep/np/v2024/STEP2_np_weighting_prep.Rmd index 3a41edd..8a40ac4 100644 --- a/globalprep/np/v2024/STEP2_np_weighting_prep.Rmd +++ b/globalprep/np/v2024/STEP2_np_weighting_prep.Rmd @@ -30,6 +30,13 @@ weighting scheme will be applied to the scores to determine how much of each natural products layer affects the overall NP score when updating in ohi global. + +## Updates from previous year + +- updated syntax to follow tidyverse style when reasonable; updated functions `read.csv()` and `write.csv()` to `read_csv()` and `write_csv()` + +- updated file paths to be more reproducible, use of `here()` package and updated syntax to reduce system-dependent file paths + ------------------------------------------------------------------------ # Data Source @@ -279,7 +286,9 @@ orn_tonnes <- np_tonnes %>% sw_fill_df <- region_product %>% filter(product == "seaweeds") -sw_tonnes <- read_csv(here(current_np_dir, "int", "np_seaweeds_tonnes_weighting.csv")) %>% +sw_tonnes_raw <- read_csv(here(current_np_dir, "int", "np_seaweeds_tonnes_weighting.csv")) + +sw_tonnes <- sw_tonnes_raw %>% mutate(product = "seaweeds") %>% group_by(rgn_id, year, product) %>% summarise(tonnes = sum(tonnes, na.rm = TRUE)) %>% @@ -298,7 +307,7 @@ sw_tonnes <- read_csv(here(current_np_dir, "int", "np_seaweeds_tonnes_weighting. Now we will calculate the weights per each product. To do this we need to multiply our average \$ value for each product \* tonnes of each product, and then divide by the total per each region. We will also -assign year = 2022 so that this can be read into OHI-global (2022 +assign year = (YYYY, e.g., 2022) so that this can be read into OHI-global (YYYY corresponds to the year that these weights were calculated). ```{r} @@ -321,12 +330,14 @@ prod_weights <- orn_tonnes %>% filter(rgn_id != 213) #write.csv(prod_weights, file.path(prep, "output/np_product_weights.csv"), row.names = FALSE) -write_csv(prod_weights, here(current_np_dir, "output", "np_product_weights.csv")) +readr::write_csv(prod_weights, here(current_np_dir, "output", "np_product_weights.csv")) ``` -```{r} -##datacheck + +### Datacheck +```{r fig.height=5, fig.width=8} +# Read in previous and current years' np_product_weights to compare #old_prod_weights <- read_csv(file.path(prep, paste0("../v", prep_year-3, "/output/np_product_weights.csv"))) #old_prod_weights <- read_csv(file.path(prep, paste0("../v", prep_year-2, "/output/np_product_weights.csv"))) old_prod_weights <- read_csv(here(previous_np_dir, "output", "np_product_weights.csv")) @@ -338,22 +349,47 @@ check <- prod_weights %>% mutate(diff = new_weight - weight) %>% left_join(rgns_eez) -plot(check$new_weight, check$weight) +# note -- these are across all years... + +plot(check$new_weight, check$weight, main = "All Products") abline(0,1, col="red") check_sw <- check %>% filter(product == "seaweeds") -plot(check_sw$new_weight, check_sw$weight) +plot(check_sw$new_weight, check_sw$weight, main = "Seaweeds") abline(0,1, col="red") check_orn <- check %>% filter(product == "ornamentals") %>% mutate(difference = new_weight - weight) -plot(check_orn$new_weight, check_orn$weight) +plot(check_orn$new_weight, check_orn$weight, main = "Ornamentals") abline(0,1, col="red") + +check_fofm <- check %>% + filter(product == "fish_oil") + +plot(check_fofm$new_weight, check_fofm$weight, main = "Fish Oil / Fish Meal (FOFM)") +abline(0, 1, col = "red") ## these change because of new SAUP fisheries catch data... + + +max(check$diff, na.rm = TRUE) +min(check$diff, na.rm = TRUE) + +top_10_diffs <- check %>% arrange(desc(abs(diff))) %>% head(n = 10) +top_10_diffs %>% relocate(diff, .after = "rgn_id") +# rgn 164 = Belize + # ~ had a "seaweeds" weight of 0.00 in 2023, now has a weight of 0.98 ≈ 0.9845 diff (v2024) + # ~ had a "fish_oil" weight of 1.00 in 2023, now has a weight of 0.015 ≈ -0.9845 diff (v2024) +# 155 = Tonga + # ~ had an "ornamentals" weight of 0.97 in 2023, now has a weight of 0.00 ≈ -9.77 (v2024) +# 153 = Cook Islands +# 11 = Marshall Islands + +# for Cook Islands and Marshall Islands, almost feels like values for `ornamentals` and `fish_oil` got flipped? + ## check saint martin, Niue, Bonaire, Sint Eustasius - should decrease in production ## check djibouti - should increase in production @@ -367,13 +403,240 @@ harvest_tonnes_usd_old <- #read_csv(file.path(prep, paste0("../v", prep_year-1, test2 <- harvest_tonnes_usd_old %>% filter(rgn_id == 46) +# test +# test2 +# v2024: Djibouti (rgn 46) does increase in production gf_orn <- read_csv(here(current_np_dir, "output", "np_ornamentals_harvest_tonnes_gf.csv")) -check_fofm <- check %>% - filter(product == "fish_oil") -plot(check_fofm$new_weight, check_fofm$weight) -abline(0, 1, col = "red") ## these change because of new SAUP fisheries catch data... +``` + + + + + +Old old check: + +```{r} +# Read in previous and current years' np_product_weights to compare +#old_prod_weights <- read_csv(file.path(prep, paste0("../v", prep_year-3, "/output/np_product_weights.csv"))) +#old_prod_weights <- read_csv(file.path(prep, paste0("../v", prep_year-2, "/output/np_product_weights.csv"))) +old_prod_weights <- read_csv(here(previous_np_dir, "output", "np_product_weights.csv")) +older_prod_weights <- read_csv(here("globalprep", "np", "v2022", "output", "np_product_weights.csv")) + +check_older <- older_prod_weights %>% + rename("older_weight" = "weight") %>% + left_join(old_prod_weights, by = c("rgn_id", "product")) %>% + mutate(diff = weight - older_weight) %>% + left_join(rgns_eez) + +plot(check_older$weight, check_older$older_weight, main = "v2023 vs v2022"); abline(0,1, col="red") + +plot(check$new_weight, check$weight, main = "v2024 vs v2023"); abline(0,1, col="red") + + +# interactive plot: + +weight_comp_plot <- ggplot(check_older, aes(x = older_weight, y = weight, + color = product, + text = paste("Region ID:", rgn_id) #, "
Product:", product) + #fill = rgn_id + )) + + geom_abline(intercept = 0, slope = 1, color = "#3498DB", linewidth = 1.3) + # Change color and size of line + geom_point(size = 2) + # Increase point size + scale_color_manual(values = c("fish_oil" = "#edae49", "seaweeds" = "#386641", "ornamentals" = "#83c5be")) + + theme_minimal() + + labs(x = "v2022 Weights", y = "v2023 Weights", + title = "Comparing NP Product Weights", + subtitle = "") + + theme(legend.position = "none") + +plotly::ggplotly(weight_comp_plot) %>% plotly::layout(yaxis = list(hoverformat = '.4f'), xaxis = list(hoverformat = '.4f')) + + + + + +weight_comp_plot_new <- ggplot(check, aes(x = weight, y = new_weight, + fill = product, + text = paste("Region ID:", rgn_id, "
Product:", product) + #fill = rgn_id + )) + + geom_abline(intercept = 0, slope = 1, color = "#3498DB", linewidth = 1.3) + # Change color and size of line + geom_point(size = 2) + # Increase point size + scale_fill_manual(values = c("fish_oil" = "#edae49", "seaweeds" = "#386641", "ornamentals" = "#83c5be")) + + theme_minimal() + + labs(x = "v2022 Weights", y = "v2023 Weights", + title = "Comparing NP Product Weights", + subtitle = "") + + theme(legend.position = "none") + +p <- plotly::ggplotly(weight_comp_plot, tooltip = "text") %>% + plotly::layout( + hovermode = "closest", + yaxis = list(hoverformat = '.4f'), xaxis = list(hoverformat = '.4f')) + +# Custom hover template +p$x$data <- lapply(p$x$data, function(trace) { + if (trace$mode == "markers") { + trace$hovertemplate <- paste( + "%{text}
", + "Product: %{fill}
", + "Old Weight: %{x:.4f}
", + "New Weight: %{y:.4f}
" + ) + } + return(trace) +}) + +p ``` + + + +```{r} +library(plotly) +library(RColorBrewer) + +color_scale <- c("fish_oils" = "#edae49", "seaweeds" = "#00798c", "ornamentals" = "#d1495b") + + +test_comp <- check_older %>% plotly::plot_ly(x = ~older_weight, y = ~weight, + type = "scatter", mode = "markers") %>% + add_trace( + # text = ~paste("Region ID:", rgn_id, "
Product:", product), + # hoverinfo = "text+x+y", + text = (~rgn_id, ~product), + marker = list( + color = ~product, + colors = color_scale + ), + hovertemplate = paste( + 'Region ID: %{text}', + '
Old Weight: %{x:.2f}', + '
New Weight: %{y:.2f}
' + ), + # showlegend = F + ) %>% + add_trace( + x = c(0,1), + y = c(0,1), + mode = "lines", + line = list(color = "red"), + showlegend = FALSE, + hoverinfo = "none" + ) %>% + + layout(xaxis = list(hoverformat = '.3f'), + yaxis = list(hoverformat = '.3f')) + +test_comp +``` + + +- RAM lost some stocks, so some stocks get removed in v4.65 (2024 data download); also got some new stocks + + + + +```{r} +library(ggplot2) +library(plotly) +library(dplyr) + +color_scale <- c("fish_oils" = "#edae49", "seaweeds" = "#00798c", "ornamentals" = "#d1495b") + +# Assuming check_older is your dataframe +# If not, replace check_older with your actual dataframe name + +# Create the ggplot object +g <- ggplot(check_older, aes(x = older_weight, y = weight, color = product, + text = paste("Region ID:", rgn_id, "
Product:", product #, + #"
Old weight:", older_weight, "
New weight:", weight + ))) + + geom_point() + + geom_abline(intercept = 0, slope = 1, color = "red", linetype = "dashed") + + scale_color_manual(values = c("fish_oil" = "#edae49", "seaweeds" = "#386641", "ornamentals" = "#83c5be")) + + labs(title = "v2023 vs v2022 Weights by Product", + x = "v2022 Weight", + y = "v2023 Weight", + color = "Product") + + theme_minimal() + +# Convert to plotly +p <- ggplotly(g, tooltip = "text") %>% + layout( + hovermode = "closest", + xaxis = list(hoverformat = ".4f"), + yaxis = list(hoverformat = ".4f") + ) + +# Custom hover template +p$x$data <- lapply(p$x$data, function(trace) { + if (trace$mode == "markers") { + trace$hovertemplate <- paste( + "%{text}
", + "Old Weight: %{x:.4f}
", + "New Weight: %{y:.4f}
" + ) + } + return(trace) +}) + +# Display the plot +p +``` + + + + +```{r} +library(ggplot2) +library(plotly) +library(dplyr) + +color_scale <- c("fish_oils" = "#edae49", "seaweeds" = "#00798c", "ornamentals" = "#d1495b") + +# Assuming check_older is your dataframe +# If not, replace check_older with your actual dataframe name + +# Create the ggplot object +g <- ggplot(check, aes(x = weight, y = new_weight, color = product, + text = paste("Region ID:", rgn_id, "
Product:", product #, + #"
Old weight:", older_weight, "
New weight:", weight + ))) + + geom_point() + + geom_abline(intercept = 0, slope = 1, color = "red", linetype = "dashed") + + scale_color_manual(values = c("fish_oil" = "#edae49", "seaweeds" = "#386641", "ornamentals" = "#83c5be")) + + labs(title = "v2024 vs v2023 Natural Product Weights by Product for 2019", + x = "v2023 2019 Weight", + y = "v2024 2019 Weight", + color = "Product") + + theme_minimal() + +# Convert to plotly +p <- ggplotly(g, tooltip = "text") %>% + layout( + hovermode = "closest", + xaxis = list(hoverformat = ".4f"), + yaxis = list(hoverformat = ".4f") + ) + +# Custom hover template +p$x$data <- lapply(p$x$data, function(trace) { + if (trace$mode == "markers") { + trace$hovertemplate <- paste( + "%{text}
", + "Old Weight: %{x:.4f}
", + "New Weight: %{y:.4f}
" + ) + } + return(trace) +}) + +# Display the plot +p +``` + diff --git a/globalprep/np/v2024/output/np_product_weights.csv b/globalprep/np/v2024/output/np_product_weights.csv new file mode 100644 index 0000000..1c75599 --- /dev/null +++ b/globalprep/np/v2024/output/np_product_weights.csv @@ -0,0 +1,661 @@ +rgn_id,year,product,weight +1,2024,ornamentals,0 +1,2024,seaweeds,0 +1,2024,fish_oil,1 +2,2024,ornamentals,0 +2,2024,seaweeds,0 +2,2024,fish_oil,1 +3,2024,ornamentals,0 +3,2024,seaweeds,0 +3,2024,fish_oil,1 +4,2024,ornamentals,NA +4,2024,seaweeds,NA +4,2024,fish_oil,NA +5,2024,ornamentals,0.7874776299730073 +5,2024,seaweeds,0 +5,2024,fish_oil,0.21252237002699273 +6,2024,ornamentals,0.17535674245101066 +6,2024,seaweeds,0 +6,2024,fish_oil,0.8246432575489894 +7,2024,ornamentals,0 +7,2024,seaweeds,0.9999365815311565 +7,2024,fish_oil,6.341846884350129e-5 +8,2024,ornamentals,0.07533760921784932 +8,2024,seaweeds,0 +8,2024,fish_oil,0.9246623907821507 +9,2024,ornamentals,0.3491981790448914 +9,2024,seaweeds,0 +9,2024,fish_oil,0.6508018209551086 +10,2024,ornamentals,NA +10,2024,seaweeds,NA +10,2024,fish_oil,NA +11,2024,ornamentals,0.20334543142857986 +11,2024,seaweeds,0 +11,2024,fish_oil,0.7966545685714201 +12,2024,ornamentals,0 +12,2024,seaweeds,0 +12,2024,fish_oil,1 +13,2024,ornamentals,0 +13,2024,seaweeds,0 +13,2024,fish_oil,1 +14,2024,ornamentals,0.0018028955836556458 +14,2024,seaweeds,0.002938154593447601 +14,2024,fish_oil,0.9952589498228968 +15,2024,ornamentals,0.0018651627004764475 +15,2024,seaweeds,0.7659395064926009 +15,2024,fish_oil,0.2321953308069226 +16,2024,ornamentals,0.0476389273154409 +16,2024,seaweeds,0 +16,2024,fish_oil,0.952361072684559 +17,2024,ornamentals,0.003726584019854075 +17,2024,seaweeds,0.9525271499372625 +17,2024,fish_oil,0.043746266042883425 +18,2024,ornamentals,0.0708259544314723 +18,2024,seaweeds,0.8287506117634107 +18,2024,fish_oil,0.100423433805117 +19,2024,ornamentals,0 +19,2024,seaweeds,0 +19,2024,fish_oil,1 +20,2024,ornamentals,1.5260600804731354e-5 +20,2024,seaweeds,0.8007671310761275 +20,2024,fish_oil,0.19921760832306767 +21,2024,ornamentals,0 +21,2024,seaweeds,0.8032213201105051 +21,2024,fish_oil,0.196778679889495 +24,2024,ornamentals,0 +24,2024,seaweeds,0.007799026126249965 +24,2024,fish_oil,0.9922009738737501 +25,2024,ornamentals,0.013076922542915514 +25,2024,seaweeds,0 +25,2024,fish_oil,0.9869230774570845 +26,2024,ornamentals,0 +26,2024,seaweeds,0 +26,2024,fish_oil,1 +28,2024,ornamentals,0 +28,2024,seaweeds,0 +28,2024,fish_oil,1 +29,2024,ornamentals,0 +29,2024,seaweeds,0 +29,2024,fish_oil,1 +30,2024,ornamentals,NA +30,2024,seaweeds,NA +30,2024,fish_oil,NA +31,2024,ornamentals,0 +31,2024,seaweeds,0 +31,2024,fish_oil,1 +32,2024,ornamentals,0 +32,2024,seaweeds,0 +32,2024,fish_oil,1 +33,2024,ornamentals,NA +33,2024,seaweeds,NA +33,2024,fish_oil,NA +34,2024,ornamentals,NA +34,2024,seaweeds,NA +34,2024,fish_oil,NA +35,2024,ornamentals,NA +35,2024,seaweeds,NA +35,2024,fish_oil,NA +36,2024,ornamentals,NA +36,2024,seaweeds,NA +36,2024,fish_oil,NA +37,2024,ornamentals,0.2310328288694083 +37,2024,seaweeds,0 +37,2024,fish_oil,0.7689671711305917 +38,2024,ornamentals,NA +38,2024,seaweeds,NA +38,2024,fish_oil,NA +39,2024,ornamentals,0.888603743463162 +39,2024,seaweeds,0 +39,2024,fish_oil,0.1113962565368379 +40,2024,ornamentals,0.028798845246346017 +40,2024,seaweeds,0.0011183715023389284 +40,2024,fish_oil,0.9700827832513151 +41,2024,ornamentals,3.536298550631269e-6 +41,2024,seaweeds,0 +41,2024,fish_oil,0.9999964637014493 +42,2024,ornamentals,0.001088613168312248 +42,2024,seaweeds,0.7525131736965341 +42,2024,fish_oil,0.24639821313515367 +43,2024,ornamentals,0.04594683016441037 +43,2024,seaweeds,0.10163161072531442 +43,2024,fish_oil,0.8524215591102752 +44,2024,ornamentals,0 +44,2024,seaweeds,0 +44,2024,fish_oil,1 +45,2024,ornamentals,0 +45,2024,seaweeds,0 +45,2024,fish_oil,1 +46,2024,ornamentals,NA +46,2024,seaweeds,NA +46,2024,fish_oil,NA +47,2024,ornamentals,8.298679173798664e-5 +47,2024,seaweeds,0 +47,2024,fish_oil,0.999917013208262 +48,2024,ornamentals,0 +48,2024,seaweeds,0 +48,2024,fish_oil,1 +49,2024,ornamentals,NA +49,2024,seaweeds,NA +49,2024,fish_oil,NA +50,2024,ornamentals,0 +50,2024,seaweeds,0 +50,2024,fish_oil,1 +51,2024,ornamentals,NA +51,2024,seaweeds,NA +51,2024,fish_oil,NA +52,2024,ornamentals,0.002123177250659463 +52,2024,seaweeds,0 +52,2024,fish_oil,0.9978768227493405 +53,2024,ornamentals,0.0012448512054724905 +53,2024,seaweeds,0 +53,2024,fish_oil,0.9987551487945275 +54,2024,ornamentals,0.007110714980605236 +54,2024,seaweeds,0 +54,2024,fish_oil,0.9928892850193948 +55,2024,ornamentals,0 +55,2024,seaweeds,0 +55,2024,fish_oil,1 +56,2024,ornamentals,0 +56,2024,seaweeds,0 +56,2024,fish_oil,1 +57,2024,ornamentals,0 +57,2024,seaweeds,0 +57,2024,fish_oil,1 +58,2024,ornamentals,0 +58,2024,seaweeds,0 +58,2024,fish_oil,1 +59,2024,ornamentals,0.2754058275457616 +59,2024,seaweeds,0 +59,2024,fish_oil,0.7245941724542384 +60,2024,ornamentals,NA +60,2024,seaweeds,NA +60,2024,fish_oil,NA +61,2024,ornamentals,0 +61,2024,seaweeds,0.01866298965802558 +61,2024,fish_oil,0.9813370103419744 +62,2024,ornamentals,0 +62,2024,seaweeds,0.005302382016869483 +62,2024,fish_oil,0.9946976179831306 +63,2024,ornamentals,0 +63,2024,seaweeds,0 +63,2024,fish_oil,1 +64,2024,ornamentals,0 +64,2024,seaweeds,0 +64,2024,fish_oil,1 +65,2024,ornamentals,0 +65,2024,seaweeds,0 +65,2024,fish_oil,1 +66,2024,ornamentals,0 +66,2024,seaweeds,0 +66,2024,fish_oil,1 +67,2024,ornamentals,0.44422209215715586 +67,2024,seaweeds,0 +67,2024,fish_oil,0.5557779078428441 +68,2024,ornamentals,0 +68,2024,seaweeds,0 +68,2024,fish_oil,1 +69,2024,ornamentals,0 +69,2024,seaweeds,0 +69,2024,fish_oil,1 +70,2024,ornamentals,0 +70,2024,seaweeds,0 +70,2024,fish_oil,1 +71,2024,ornamentals,0.026010052396885697 +71,2024,seaweeds,0 +71,2024,fish_oil,0.9739899476031143 +72,2024,ornamentals,0 +72,2024,seaweeds,0 +72,2024,fish_oil,1 +73,2024,ornamentals,0 +73,2024,seaweeds,0.11094253120475427 +73,2024,fish_oil,0.8890574687952457 +74,2024,ornamentals,0 +74,2024,seaweeds,0 +74,2024,fish_oil,1 +75,2024,ornamentals,0 +75,2024,seaweeds,0 +75,2024,fish_oil,1 +76,2024,ornamentals,3.227743889034704e-4 +76,2024,seaweeds,0 +76,2024,fish_oil,0.9996772256110966 +77,2024,ornamentals,0.008111165826205427 +77,2024,seaweeds,0 +77,2024,fish_oil,0.9918888341737946 +78,2024,ornamentals,8.723225109041678e-4 +78,2024,seaweeds,0 +78,2024,fish_oil,0.9991276774890958 +79,2024,ornamentals,0.007500023031962037 +79,2024,seaweeds,0 +79,2024,fish_oil,0.9924999769680379 +80,2024,ornamentals,0 +80,2024,seaweeds,0 +80,2024,fish_oil,1 +81,2024,ornamentals,0 +81,2024,seaweeds,0 +81,2024,fish_oil,1 +82,2024,ornamentals,0 +82,2024,seaweeds,0 +82,2024,fish_oil,1 +84,2024,ornamentals,0 +84,2024,seaweeds,0 +84,2024,fish_oil,1 +85,2024,ornamentals,0 +85,2024,seaweeds,0 +85,2024,fish_oil,1 +86,2024,ornamentals,NA +86,2024,seaweeds,NA +86,2024,fish_oil,NA +88,2024,ornamentals,NA +88,2024,seaweeds,NA +88,2024,fish_oil,NA +89,2024,ornamentals,0 +89,2024,seaweeds,0 +89,2024,fish_oil,1 +90,2024,ornamentals,NA +90,2024,seaweeds,NA +90,2024,fish_oil,NA +91,2024,ornamentals,NA +91,2024,seaweeds,NA +91,2024,fish_oil,NA +92,2024,ornamentals,NA +92,2024,seaweeds,NA +92,2024,fish_oil,NA +93,2024,ornamentals,NA +93,2024,seaweeds,NA +93,2024,fish_oil,NA +94,2024,ornamentals,0 +94,2024,seaweeds,0 +94,2024,fish_oil,1 +95,2024,ornamentals,0 +95,2024,seaweeds,0 +95,2024,fish_oil,1 +96,2024,ornamentals,0 +96,2024,seaweeds,0 +96,2024,fish_oil,1 +97,2024,ornamentals,0 +97,2024,seaweeds,0 +97,2024,fish_oil,1 +98,2024,ornamentals,0 +98,2024,seaweeds,0 +98,2024,fish_oil,1 +99,2024,ornamentals,0 +99,2024,seaweeds,0 +99,2024,fish_oil,1 +100,2024,ornamentals,0 +100,2024,seaweeds,0 +100,2024,fish_oil,1 +101,2024,ornamentals,6.822747801745234e-5 +101,2024,seaweeds,0 +101,2024,fish_oil,0.9999317725219825 +102,2024,ornamentals,0.0021929732424260996 +102,2024,seaweeds,0.06407541994967879 +102,2024,fish_oil,0.9337316068078952 +103,2024,ornamentals,0 +103,2024,seaweeds,0 +103,2024,fish_oil,1 +104,2024,ornamentals,0 +104,2024,seaweeds,0 +104,2024,fish_oil,1 +105,2024,ornamentals,NA +105,2024,seaweeds,NA +105,2024,fish_oil,NA +106,2024,ornamentals,8.35931598229732e-5 +106,2024,seaweeds,0 +106,2024,fish_oil,0.999916406840177 +107,2024,ornamentals,NA +107,2024,seaweeds,NA +107,2024,fish_oil,NA +108,2024,ornamentals,0 +108,2024,seaweeds,0 +108,2024,fish_oil,1 +110,2024,ornamentals,0 +110,2024,seaweeds,0 +110,2024,fish_oil,1 +111,2024,ornamentals,0 +111,2024,seaweeds,0 +111,2024,fish_oil,1 +112,2024,ornamentals,0.004983109536402074 +112,2024,seaweeds,0 +112,2024,fish_oil,0.9950168904635979 +113,2024,ornamentals,NA +113,2024,seaweeds,NA +113,2024,fish_oil,NA +114,2024,ornamentals,0.02047691346248584 +114,2024,seaweeds,0 +114,2024,fish_oil,0.9795230865375141 +115,2024,ornamentals,0.013799709454706812 +115,2024,seaweeds,0 +115,2024,fish_oil,0.9862002905452932 +116,2024,ornamentals,0 +116,2024,seaweeds,0 +116,2024,fish_oil,1 +117,2024,ornamentals,0 +117,2024,seaweeds,0 +117,2024,fish_oil,1 +118,2024,ornamentals,0 +118,2024,seaweeds,0 +118,2024,fish_oil,1 +119,2024,ornamentals,0 +119,2024,seaweeds,0.01948289379473863 +119,2024,fish_oil,0.9805171062052613 +120,2024,ornamentals,0 +120,2024,seaweeds,0.15241091458914185 +120,2024,fish_oil,0.8475890854108582 +121,2024,ornamentals,0 +121,2024,seaweeds,0 +121,2024,fish_oil,1 +122,2024,ornamentals,0 +122,2024,seaweeds,0.9507233623440297 +122,2024,fish_oil,0.049276637655970325 +123,2024,ornamentals,0 +123,2024,seaweeds,0 +123,2024,fish_oil,1 +124,2024,ornamentals,NA +124,2024,seaweeds,NA +124,2024,fish_oil,NA +125,2024,ornamentals,0 +125,2024,seaweeds,0.33373493296951556 +125,2024,fish_oil,0.6662650670304845 +126,2024,ornamentals,0.01851711513123504 +126,2024,seaweeds,0 +126,2024,fish_oil,0.981482884868765 +127,2024,ornamentals,0 +127,2024,seaweeds,0.0690503709409271 +127,2024,fish_oil,0.9309496290590729 +129,2024,ornamentals,0 +129,2024,seaweeds,0 +129,2024,fish_oil,1 +130,2024,ornamentals,0.9981545968270721 +130,2024,seaweeds,0 +130,2024,fish_oil,0.001845403172927838 +131,2024,ornamentals,1 +131,2024,seaweeds,0 +131,2024,fish_oil,0 +132,2024,ornamentals,0.01874861310753486 +132,2024,seaweeds,0 +132,2024,fish_oil,0.9812513868924652 +133,2024,ornamentals,0 +133,2024,seaweeds,0 +133,2024,fish_oil,1 +134,2024,ornamentals,0 +134,2024,seaweeds,0 +134,2024,fish_oil,1 +135,2024,ornamentals,3.316287373246964e-4 +135,2024,seaweeds,0 +135,2024,fish_oil,0.9996683712626754 +136,2024,ornamentals,0 +136,2024,seaweeds,0 +136,2024,fish_oil,1 +137,2024,ornamentals,0.002003662992060166 +137,2024,seaweeds,0.06610795044192853 +137,2024,fish_oil,0.9318883865660113 +138,2024,ornamentals,7.68875810407538e-6 +138,2024,seaweeds,4.3688140588665504e-8 +138,2024,fish_oil,0.9999922675537554 +139,2024,ornamentals,0 +139,2024,seaweeds,0.10482496624125871 +139,2024,fish_oil,0.8951750337587412 +140,2024,ornamentals,0 +140,2024,seaweeds,0 +140,2024,fish_oil,1 +141,2024,ornamentals,0 +141,2024,seaweeds,0.004304670975749007 +141,2024,fish_oil,0.995695329024251 +143,2024,ornamentals,0 +143,2024,seaweeds,0 +143,2024,fish_oil,1 +144,2024,ornamentals,0 +144,2024,seaweeds,0 +144,2024,fish_oil,1 +145,2024,ornamentals,0 +145,2024,seaweeds,0 +145,2024,fish_oil,1 +146,2024,ornamentals,NA +146,2024,seaweeds,NA +146,2024,fish_oil,NA +147,2024,ornamentals,0.1481133337867934 +147,2024,seaweeds,0 +147,2024,fish_oil,0.8518866662132066 +148,2024,ornamentals,0 +148,2024,seaweeds,0 +148,2024,fish_oil,1 +149,2024,ornamentals,NA +149,2024,seaweeds,NA +149,2024,fish_oil,NA +150,2024,ornamentals,NA +150,2024,seaweeds,NA +150,2024,fish_oil,NA +151,2024,ornamentals,0 +151,2024,seaweeds,0 +151,2024,fish_oil,1 +152,2024,ornamentals,0 +152,2024,seaweeds,0.0016896524344866388 +152,2024,fish_oil,0.9983103475655133 +153,2024,ornamentals,0.1138779677701584 +153,2024,seaweeds,0 +153,2024,fish_oil,0.8861220322298416 +154,2024,ornamentals,0 +154,2024,seaweeds,0 +154,2024,fish_oil,1 +155,2024,ornamentals,0 +155,2024,seaweeds,0.28798100609492183 +155,2024,fish_oil,0.7120189939050782 +156,2024,ornamentals,0 +156,2024,seaweeds,0 +156,2024,fish_oil,1 +157,2024,ornamentals,0 +157,2024,seaweeds,0 +157,2024,fish_oil,1 +158,2024,ornamentals,NA +158,2024,seaweeds,NA +158,2024,fish_oil,NA +159,2024,ornamentals,NA +159,2024,seaweeds,NA +159,2024,fish_oil,NA +161,2024,ornamentals,0 +161,2024,seaweeds,0 +161,2024,fish_oil,1 +162,2024,ornamentals,1.5100706213140653e-4 +162,2024,seaweeds,0 +162,2024,fish_oil,0.9998489929378686 +163,2024,ornamentals,5.758584740319285e-4 +163,2024,seaweeds,0.0072038407657504374 +163,2024,fish_oil,0.9922203007602177 +164,2024,ornamentals,0 +164,2024,seaweeds,0.9845444169874336 +164,2024,fish_oil,0.015455583012566432 +166,2024,ornamentals,0 +166,2024,seaweeds,0 +166,2024,fish_oil,1 +167,2024,ornamentals,9.37703364124855e-4 +167,2024,seaweeds,0 +167,2024,fish_oil,0.9990622966358751 +168,2024,ornamentals,0.0017675571926103108 +168,2024,seaweeds,0 +168,2024,fish_oil,0.9982324428073897 +169,2024,ornamentals,0 +169,2024,seaweeds,0 +169,2024,fish_oil,1 +171,2024,ornamentals,0.006877559154136395 +171,2024,seaweeds,0.01741874011562971 +171,2024,fish_oil,0.9757037007302338 +172,2024,ornamentals,0 +172,2024,seaweeds,0 +172,2024,fish_oil,1 +173,2024,ornamentals,0 +173,2024,seaweeds,0 +173,2024,fish_oil,1 +174,2024,ornamentals,0 +174,2024,seaweeds,0 +174,2024,fish_oil,1 +175,2024,ornamentals,0.001531585962982455 +175,2024,seaweeds,0.0010372714464923927 +175,2024,fish_oil,0.9974311425905252 +176,2024,ornamentals,0.009565170731928495 +176,2024,seaweeds,0 +176,2024,fish_oil,0.9904348292680716 +177,2024,ornamentals,0.270641769857585 +177,2024,seaweeds,0 +177,2024,fish_oil,0.729358230142415 +178,2024,ornamentals,1.2545082545419201e-5 +178,2024,seaweeds,0 +178,2024,fish_oil,0.9999874549174547 +179,2024,ornamentals,0.028592434213746656 +179,2024,seaweeds,2.1593127605189626e-4 +179,2024,fish_oil,0.9711916345102015 +180,2024,ornamentals,0.002395455395690891 +180,2024,seaweeds,0 +180,2024,fish_oil,0.997604544604309 +181,2024,ornamentals,7.723437650871406e-5 +181,2024,seaweeds,9.65470642462437e-5 +181,2024,fish_oil,0.999826218559245 +182,2024,ornamentals,0.2658047414190335 +182,2024,seaweeds,1.460773342257315e-4 +182,2024,fish_oil,0.7340491812467408 +183,2024,ornamentals,0.02383610997456537 +183,2024,seaweeds,0.0017543978577823278 +183,2024,fish_oil,0.9744094921676524 +184,2024,ornamentals,0.0070823253519876955 +184,2024,seaweeds,0 +184,2024,fish_oil,0.9929176746480122 +185,2024,ornamentals,NA +185,2024,seaweeds,NA +185,2024,fish_oil,NA +186,2024,ornamentals,0 +186,2024,seaweeds,0 +186,2024,fish_oil,1 +187,2024,ornamentals,0 +187,2024,seaweeds,0 +187,2024,fish_oil,1 +188,2024,ornamentals,0 +188,2024,seaweeds,0 +188,2024,fish_oil,1 +189,2024,ornamentals,0.0042866838431241385 +189,2024,seaweeds,0 +189,2024,fish_oil,0.9957133161568759 +190,2024,ornamentals,0 +190,2024,seaweeds,0 +190,2024,fish_oil,1 +191,2024,ornamentals,0.003678124509311274 +191,2024,seaweeds,0 +191,2024,fish_oil,0.9963218754906887 +192,2024,ornamentals,0 +192,2024,seaweeds,0 +192,2024,fish_oil,1 +193,2024,ornamentals,0 +193,2024,seaweeds,0 +193,2024,fish_oil,1 +194,2024,ornamentals,0 +194,2024,seaweeds,0 +194,2024,fish_oil,1 +195,2024,ornamentals,0 +195,2024,seaweeds,0 +195,2024,fish_oil,1 +196,2024,ornamentals,4.1586110377811e-5 +196,2024,seaweeds,0 +196,2024,fish_oil,0.9999584138896223 +197,2024,ornamentals,0 +197,2024,seaweeds,0 +197,2024,fish_oil,1 +198,2024,ornamentals,0 +198,2024,seaweeds,0 +198,2024,fish_oil,1 +199,2024,ornamentals,0.014349594230357084 +199,2024,seaweeds,0 +199,2024,fish_oil,0.9856504057696429 +200,2024,ornamentals,0 +200,2024,seaweeds,0 +200,2024,fish_oil,1 +202,2024,ornamentals,0.0011433427393194922 +202,2024,seaweeds,0.5488587013946071 +202,2024,fish_oil,0.4499979558660734 +203,2024,ornamentals,0.0010614950901586312 +203,2024,seaweeds,0.10053238530294385 +203,2024,fish_oil,0.8984061196068974 +204,2024,ornamentals,0 +204,2024,seaweeds,0 +204,2024,fish_oil,1 +205,2024,ornamentals,0.001027359304557805 +205,2024,seaweeds,0.001363695108297602 +205,2024,fish_oil,0.9976089455871445 +206,2024,ornamentals,0.0016817483847285306 +206,2024,seaweeds,0.7354915101225374 +206,2024,fish_oil,0.2628267414927339 +207,2024,ornamentals,0.0030945177145356904 +207,2024,seaweeds,0.09244284488023755 +207,2024,fish_oil,0.9044626374052268 +208,2024,ornamentals,0.8464145905363012 +208,2024,seaweeds,0 +208,2024,fish_oil,0.15358540946369886 +209,2024,ornamentals,4.069640729452248e-6 +209,2024,seaweeds,0.9411729070613706 +209,2024,fish_oil,0.058823023297899964 +210,2024,ornamentals,3.7564853317768296e-4 +210,2024,seaweeds,0.12510377867776237 +210,2024,fish_oil,0.8745205727890599 +212,2024,ornamentals,0.03722022663823027 +212,2024,seaweeds,0 +212,2024,fish_oil,0.9627797733617697 +214,2024,ornamentals,0.008789736476918435 +214,2024,seaweeds,0 +214,2024,fish_oil,0.9912102635230816 +215,2024,ornamentals,0 +215,2024,seaweeds,0 +215,2024,fish_oil,1 +216,2024,ornamentals,6.609484887352461e-4 +216,2024,seaweeds,0.8410358742447746 +216,2024,fish_oil,0.15830317726649015 +218,2024,ornamentals,1.113600904710003e-5 +218,2024,seaweeds,0 +218,2024,fish_oil,0.9999888639909529 +219,2024,ornamentals,0 +219,2024,seaweeds,0 +219,2024,fish_oil,1 +220,2024,ornamentals,NA +220,2024,seaweeds,NA +220,2024,fish_oil,NA +221,2024,ornamentals,0 +221,2024,seaweeds,0 +221,2024,fish_oil,1 +222,2024,ornamentals,6.059426092499611e-4 +222,2024,seaweeds,0 +222,2024,fish_oil,0.99939405739075 +223,2024,ornamentals,0 +223,2024,seaweeds,7.641039142665642e-4 +223,2024,fish_oil,0.9992358960857334 +224,2024,ornamentals,0 +224,2024,seaweeds,0.04823637328402634 +224,2024,fish_oil,0.9517636267159736 +227,2024,ornamentals,0 +227,2024,seaweeds,0 +227,2024,fish_oil,1 +228,2024,ornamentals,0 +228,2024,seaweeds,0 +228,2024,fish_oil,1 +231,2024,ornamentals,0 +231,2024,seaweeds,0.31442437629007447 +231,2024,fish_oil,0.6855756237099255 +232,2024,ornamentals,0 +232,2024,seaweeds,0 +232,2024,fish_oil,1 +237,2024,ornamentals,NA +237,2024,seaweeds,NA +237,2024,fish_oil,NA +244,2024,ornamentals,0 +244,2024,seaweeds,0 +244,2024,fish_oil,1 +245,2024,ornamentals,0 +245,2024,seaweeds,0 +245,2024,fish_oil,1 +247,2024,ornamentals,0 +247,2024,seaweeds,0 +247,2024,fish_oil,1 +248,2024,ornamentals,NA +248,2024,seaweeds,NA +248,2024,fish_oil,NA +249,2024,ornamentals,NA +249,2024,seaweeds,NA +249,2024,fish_oil,NA +250,2024,ornamentals,0.8173261324499358 +250,2024,seaweeds,0 +250,2024,fish_oil,0.18267386755006418 From 7a0d66ce4d7105ffba9f076e339e448423a59fb2 Mon Sep 17 00:00:00 2001 From: annaramji Date: Mon, 19 Aug 2024 23:17:31 +0000 Subject: [PATCH 2/2] docs: editing and refining datacheck visualizations for NP step 2 --- .../np/v2024/STEP2_np_weighting_prep.Rmd | 227 ++++++++---------- 1 file changed, 105 insertions(+), 122 deletions(-) diff --git a/globalprep/np/v2024/STEP2_np_weighting_prep.Rmd b/globalprep/np/v2024/STEP2_np_weighting_prep.Rmd index 8a40ac4..977adb1 100644 --- a/globalprep/np/v2024/STEP2_np_weighting_prep.Rmd +++ b/globalprep/np/v2024/STEP2_np_weighting_prep.Rmd @@ -336,7 +336,12 @@ readr::write_csv(prod_weights, here(current_np_dir, "output", "np_product_weight ### Datacheck + +Note: we're not comparing data for a specific year, e.g., 2019, as we do in many data checks. The data processing that creates `np_product_weights.csv` fills the `year` column with the scenario year (e.g., 2024), which means that the previous year's `np_product_weights.csv` and the current year's weights cannot be compared by filtering to a specific year. + + ```{r fig.height=5, fig.width=8} + # Read in previous and current years' np_product_weights to compare #old_prod_weights <- read_csv(file.path(prep, paste0("../v", prep_year-3, "/output/np_product_weights.csv"))) #old_prod_weights <- read_csv(file.path(prep, paste0("../v", prep_year-2, "/output/np_product_weights.csv"))) @@ -352,20 +357,20 @@ check <- prod_weights %>% # note -- these are across all years... plot(check$new_weight, check$weight, main = "All Products") -abline(0,1, col="red") +abline(0,1, col = "red") check_sw <- check %>% filter(product == "seaweeds") plot(check_sw$new_weight, check_sw$weight, main = "Seaweeds") -abline(0,1, col="red") +abline(0,1, col = "red") check_orn <- check %>% filter(product == "ornamentals") %>% mutate(difference = new_weight - weight) plot(check_orn$new_weight, check_orn$weight, main = "Ornamentals") -abline(0,1, col="red") +abline(0,1, col = "red") check_fofm <- check %>% @@ -374,9 +379,8 @@ check_fofm <- check %>% plot(check_fofm$new_weight, check_fofm$weight, main = "Fish Oil / Fish Meal (FOFM)") abline(0, 1, col = "red") ## these change because of new SAUP fisheries catch data... - -max(check$diff, na.rm = TRUE) -min(check$diff, na.rm = TRUE) +# max(check$diff, na.rm = TRUE) +# min(check$diff, na.rm = TRUE) top_10_diffs <- check %>% arrange(desc(abs(diff))) %>% head(n = 10) top_10_diffs %>% relocate(diff, .after = "rgn_id") @@ -407,7 +411,7 @@ test2 <- harvest_tonnes_usd_old %>% # test2 # v2024: Djibouti (rgn 46) does increase in production -gf_orn <- read_csv(here(current_np_dir, "output", "np_ornamentals_harvest_tonnes_gf.csv")) +#gf_orn <- read_csv(here(current_np_dir, "output", "np_ornamentals_harvest_tonnes_gf.csv")) ``` @@ -415,7 +419,6 @@ gf_orn <- read_csv(here(current_np_dir, "output", "np_ornamentals_harvest_tonnes - Old old check: ```{r} @@ -437,61 +440,6 @@ plot(check_older$weight, check_older$older_weight, main = "v2023 vs v2022"); abl plot(check$new_weight, check$weight, main = "v2024 vs v2023"); abline(0,1, col="red") -# interactive plot: - -weight_comp_plot <- ggplot(check_older, aes(x = older_weight, y = weight, - color = product, - text = paste("Region ID:", rgn_id) #, "
Product:", product) - #fill = rgn_id - )) + - geom_abline(intercept = 0, slope = 1, color = "#3498DB", linewidth = 1.3) + # Change color and size of line - geom_point(size = 2) + # Increase point size - scale_color_manual(values = c("fish_oil" = "#edae49", "seaweeds" = "#386641", "ornamentals" = "#83c5be")) + - theme_minimal() + - labs(x = "v2022 Weights", y = "v2023 Weights", - title = "Comparing NP Product Weights", - subtitle = "") + - theme(legend.position = "none") - -plotly::ggplotly(weight_comp_plot) %>% plotly::layout(yaxis = list(hoverformat = '.4f'), xaxis = list(hoverformat = '.4f')) - - - - - -weight_comp_plot_new <- ggplot(check, aes(x = weight, y = new_weight, - fill = product, - text = paste("Region ID:", rgn_id, "
Product:", product) - #fill = rgn_id - )) + - geom_abline(intercept = 0, slope = 1, color = "#3498DB", linewidth = 1.3) + # Change color and size of line - geom_point(size = 2) + # Increase point size - scale_fill_manual(values = c("fish_oil" = "#edae49", "seaweeds" = "#386641", "ornamentals" = "#83c5be")) + - theme_minimal() + - labs(x = "v2022 Weights", y = "v2023 Weights", - title = "Comparing NP Product Weights", - subtitle = "") + - theme(legend.position = "none") - -p <- plotly::ggplotly(weight_comp_plot, tooltip = "text") %>% - plotly::layout( - hovermode = "closest", - yaxis = list(hoverformat = '.4f'), xaxis = list(hoverformat = '.4f')) - -# Custom hover template -p$x$data <- lapply(p$x$data, function(trace) { - if (trace$mode == "markers") { - trace$hovertemplate <- paste( - "%{text}
", - "Product: %{fill}
", - "Old Weight: %{x:.4f}
", - "New Weight: %{y:.4f}
" - ) - } - return(trace) -}) - -p ``` @@ -500,26 +448,25 @@ p library(plotly) library(RColorBrewer) -color_scale <- c("fish_oils" = "#edae49", "seaweeds" = "#00798c", "ornamentals" = "#d1495b") - test_comp <- check_older %>% plotly::plot_ly(x = ~older_weight, y = ~weight, type = "scatter", mode = "markers") %>% add_trace( - # text = ~paste("Region ID:", rgn_id, "
Product:", product), - # hoverinfo = "text+x+y", - text = (~rgn_id, ~product), - marker = list( - color = ~product, - colors = color_scale - ), - hovertemplate = paste( - 'Region ID: %{text}', - '
Old Weight: %{x:.2f}', - '
New Weight: %{y:.2f}
' + # text = ~paste("Region ID:", rgn_id #, "
Product:", product + # ), + color = ~rgn_id, + # marker = list( + # color = ~product, + # ), + hoverinfo = "text+x+y", + hovertemplate = paste( + 'Region ID: %{color}', + '
Old Weight: %{x:.2f}', + '
New Weight: %{y:.2f}
' ), # showlegend = F ) %>% + # add AB line add_trace( x = c(0,1), y = c(0,1), @@ -539,42 +486,76 @@ test_comp - RAM lost some stocks, so some stocks get removed in v4.65 (2024 data download); also got some new stocks - +### New datacheck: interactive plot ```{r} +# ======== Setup ========================================================== +# (mostly copied from earlier setup with addition of library(plotly)) +# Read in packages necessary for visualization (in case you cleared your environment) library(ggplot2) library(plotly) library(dplyr) +library(tidyr) +library(here) -color_scale <- c("fish_oils" = "#edae49", "seaweeds" = "#00798c", "ornamentals" = "#d1495b") +# Set scenario year, reproducible file paths +scen_year_number <- 2024 # update this!! +scenario_year <- as.character(scen_year_number) +v_scen_year <- paste0("v", scenario_year) + +current_np_dir <- here::here("globalprep", "np", v_scen_year) +previous_np_dir <- here::here("globalprep", "np", paste0("v", scen_year_number - 1)) + +# ----- Read in NP weights data ----- +# Read in current, previous, and 2 years previous data +old_prod_weights <- read_csv(here(previous_np_dir, "output", "np_product_weights.csv")) +prod_weights <- read_csv(here(current_np_dir, "output", "np_product_weights.csv")) + +check <- prod_weights %>% + rename("new_weight" = "weight") %>% + left_join(old_prod_weights, by = c("rgn_id", "product")) %>% + mutate(diff = new_weight - weight) %>% + left_join(rgns_eez) + + +#old_prod_weights <- read_csv(here(previous_np_dir, "output", "np_product_weights.csv")) +older_prod_weights <- read_csv(here("globalprep", "np", paste0("v", scen_year_number - 2), "output", "np_product_weights.csv")) + +check_older <- older_prod_weights %>% + rename("older_weight" = "weight") %>% + left_join(old_prod_weights, by = c("rgn_id", "product")) %>% + mutate(diff = weight - older_weight) %>% + left_join(rgns_eez) + + +# ======== Plot new NP weights data vs. previous year's data ================== -# Assuming check_older is your dataframe -# If not, replace check_older with your actual dataframe name -# Create the ggplot object -g <- ggplot(check_older, aes(x = older_weight, y = weight, color = product, +# ---- Create ggplot object with `text` aes argument to prep for plotly ----- # +g_new <- ggplot(check, aes(x = weight, y = new_weight, color = product, text = paste("Region ID:", rgn_id, "
Product:", product #, #"
Old weight:", older_weight, "
New weight:", weight ))) + - geom_point() + - geom_abline(intercept = 0, slope = 1, color = "red", linetype = "dashed") + + geom_point(alpha = 0.8) + + geom_abline(intercept = 0, slope = 1, alpha = 0.7, + color = "red", linetype = "dashed") + scale_color_manual(values = c("fish_oil" = "#edae49", "seaweeds" = "#386641", "ornamentals" = "#83c5be")) + - labs(title = "v2023 vs v2022 Weights by Product", - x = "v2022 Weight", - y = "v2023 Weight", + labs(title = "v2024 vs v2023 Natural Product Weights by Product", + x = "v2023 Weight", + y = "v2024 Weight", color = "Product") + theme_minimal() -# Convert to plotly -p <- ggplotly(g, tooltip = "text") %>% +# ---- Convert to plotly ------------------------------ # +p_new <- ggplotly(g_new, tooltip = "text") %>% layout( hovermode = "closest", - xaxis = list(hoverformat = ".4f"), - yaxis = list(hoverformat = ".4f") + xaxis = list(hoverformat = ".4f"), # customize number formatting + yaxis = list(hoverformat = ".4f") # (number of places after decimal) ) -# Custom hover template -p$x$data <- lapply(p$x$data, function(trace) { +# ------- Create custom hover template ---------------- # +p_new$x$data <- lapply(p_new$x$data, function(trace) { if (trace$mode == "markers") { trace$hovertemplate <- paste( "%{text}
", @@ -585,47 +566,48 @@ p$x$data <- lapply(p$x$data, function(trace) { return(trace) }) -# Display the plot -p -``` +# Display customized plot +p_new +# ========= Plot old vs. older NP weights data ================================ +# Plot previous year's NP weights data vs. year before previous year's data -```{r} -library(ggplot2) -library(plotly) -library(dplyr) - -color_scale <- c("fish_oils" = "#edae49", "seaweeds" = "#00798c", "ornamentals" = "#d1495b") - -# Assuming check_older is your dataframe -# If not, replace check_older with your actual dataframe name +#color_scale <- c("fish_oils" = "#edae49", "seaweeds" = "#00798c", "ornamentals" = "#d1495b") -# Create the ggplot object -g <- ggplot(check, aes(x = weight, y = new_weight, color = product, - text = paste("Region ID:", rgn_id, "
Product:", product #, - #"
Old weight:", older_weight, "
New weight:", weight - ))) + - geom_point() + - geom_abline(intercept = 0, slope = 1, color = "red", linetype = "dashed") + +# ---- Create ggplot object with added `text` field to prep for plotly ------ # +g_old <- ggplot(check_older, aes( + x = older_weight, y = weight, color = product, + text = paste("Region ID:", rgn_id, "
Product:", product #, + #"
Old weight:", older_weight, "
New weight:", weight + ) + )) + + # add scatterplot points + geom_point(alpha = 0.8) + + # add ab line + geom_abline(intercept = 0, slope = 1, alpha = 0.7, + color = "red", linetype = "solid") + + # customize colors scale_color_manual(values = c("fish_oil" = "#edae49", "seaweeds" = "#386641", "ornamentals" = "#83c5be")) + - labs(title = "v2024 vs v2023 Natural Product Weights by Product for 2019", - x = "v2023 2019 Weight", - y = "v2024 2019 Weight", + # update labels + labs(title = "v2023 vs v2022 Natural Products Weights by Product", + x = "v2022 Weights", + y = "v2023 Weights", color = "Product") + + # set base theme theme_minimal() -# Convert to plotly -p <- ggplotly(g, tooltip = "text") %>% +# ---- Convert to plotly ------------------------------ # +p_old <- ggplotly(g_old, tooltip = "text") %>% layout( hovermode = "closest", - xaxis = list(hoverformat = ".4f"), + xaxis = list(hoverformat = ".4f"), # customize number formatting yaxis = list(hoverformat = ".4f") ) -# Custom hover template -p$x$data <- lapply(p$x$data, function(trace) { +# ------- Custom hover template ----------------------- # +p_old$x$data <- lapply(p_old$x$data, function(trace) { if (trace$mode == "markers") { trace$hovertemplate <- paste( "%{text}
", @@ -636,7 +618,8 @@ p$x$data <- lapply(p$x$data, function(trace) { return(trace) }) -# Display the plot -p +# Display customized plot! +p_old ``` +