This is an R Markdown notebook which illustrates how to make charts representing Forest gain and loss data obtained from Google Earth Engine (GEE) using the Hansen Global Forest Change v.1.10 (2000-2022) dataset.
First of all, it is convenient to clean the R environment:
rm(list=ls())
Then, we need to install several libraries. Make sure you install them from the R console.
# DO NOT RUN THIS CHUNK FROM HERE
# DO IT FROM THE R CONSOLE
# install.packages("devtools")
# devtools::install_github('Mikata-Project/ggthemr')
# install.packages("forcats")
Now, we load the libraries:
library(readr)
library(tidyr)
library(dplyr)
library(ggplot2)
library(ggthemr) # to set a custom theme but non essential!
library(forcats) # to reorder categorical variables
Set theme for the plot:
# Set theme for the plot
ggthemr('dust', type = "outer", layout = "minimal")
# This theme will now be applied to all plots you make, if you wanted to
# get rid of it, use:
# ggthemr_reset()
Next up, go the directory where we saved the csv data we exported from GEE to Google Drive and read in the files.
# Read in the data ----
NP_forest_gain <- read_csv("NP_forest_gain.csv")
## Rows: 4 Columns: 33
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (21): system:index, CONS_OBJ, DESIG, DESIG_ENG, DESIG_TYPE, GOV_TYPE, IN...
## dbl (12): GIS_AREA, GIS_M_AREA, MARINE, METADATAID, NO_TK_AREA, PA_DEF, REP_...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
NP_forest_loss <- read_csv("NP_forest_loss.csv")
## Rows: 4 Columns: 33
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (21): system:index, CONS_OBJ, DESIG, DESIG_ENG, DESIG_TYPE, GOV_TYPE, IN...
## dbl (12): GIS_AREA, GIS_M_AREA, MARINE, METADATAID, NO_TK_AREA, PA_DEF, REP_...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
We will combine the two objects (the one for forest loss and the one for forest gain) so that we can visualise them in the same plot. We can create an “identifier” column so that we know which values refer to gain and which ones to loss in forest cover
# Create identifier column for gain vs loss
NP_forest_gain$type <- "Gain"
NP_forest_loss$type <- "Loss"
# Bind the objects together
forest_change <- rbind(NP_forest_gain, NP_forest_loss)
Let’s check what we got:
names(forest_change)
## [1] "system:index" "CONS_OBJ" "DESIG" "DESIG_ENG" "DESIG_TYPE"
## [6] "GIS_AREA" "GIS_M_AREA" "GOV_TYPE" "INT_CRIT" "ISO3"
## [11] "IUCN_CAT" "MANG_AUTH" "MANG_PLAN" "MARINE" "METADATAID"
## [16] "NAME" "NO_TAKE" "NO_TK_AREA" "ORIG_NAME" "OWN_TYPE"
## [21] "PARENT_ISO" "PA_DEF" "REP_AREA" "REP_M_AREA" "STATUS"
## [26] "STATUS_YR" "SUB_LOC" "SUPP_INFO" "VERIF" "WDPAID"
## [31] "WDPA_PID" "sum" ".geo" "type"
Let’s keep only columns with relevant data:
forest_change %>% select(NAME, GIS_AREA, REP_AREA, STATUS_YR, sum, type) ->
nforest_change
Check the output:
nforest_change
Note that the NAMES colummn contains names which are too large to put on a plot. What is the relevant data type?
nforest_change$NAME
## [1] "Sierra de la Macarena" "Tinigua"
## [3] "Miraflores Picachos" "Serrania de Chiribiquete"
## [5] "Sierra de la Macarena" "Tinigua"
## [7] "Miraflores Picachos" "Serrania de Chiribiquete"
Let’s define a names array:
newnames <-
c("Maca","Tini","Mira","Chiri", "Maca", "Tini", "Mira", "Chiri")
Let’s change such names:
nforest_change$NAME <- newnames
Check the output:
nforest_change$NAME
## [1] "Maca" "Tini" "Mira" "Chiri" "Maca" "Tini" "Mira" "Chiri"
(forest_barplot <- ggplot(nforest_change, aes(x = NAME, y = sum/GIS_AREA,
fill = fct_rev(type))) +
geom_bar(stat = "identity", position = "dodge") +
labs(x = NULL, y = "Forest change (% of park area)\n") +
# Expanding the scale removes the emtpy space below the bars
scale_y_continuous(expand = c(0, 0)) +
theme(text = element_text(size = 16), # makes font size larger
legend.position = c(0.1, 0.85), # changes the placement of the legend
legend.title = element_blank(), # gets rid of the legend title
legend.background = element_rect(color = "black",
fill = "transparent", # removes the white background behind the legend
linetype = "blank")))
If you reuse this code please cite it as follows: Lizarazo, I. 2024. Plotting forest change in R. Available at: https://rpubs.com/ials2un/forest_change
sessionInfo()
## R version 4.3.2 (2023-10-31)
## Platform: x86_64-apple-darwin20 (64-bit)
## Running under: macOS Sonoma 14.2.1
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## time zone: America/Bogota
## tzcode source: internal
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] forcats_1.0.0 ggthemr_1.1.0 ggplot2_3.4.4 dplyr_1.1.3 tidyr_1.3.0
## [6] readr_2.1.4
##
## loaded via a namespace (and not attached):
## [1] bit_4.0.5 gtable_0.3.4 jsonlite_1.8.8 highr_0.10
## [5] crayon_1.5.2 compiler_4.3.2 tidyselect_1.2.0 parallel_4.3.2
## [9] jquerylib_0.1.4 scales_1.3.0 yaml_2.3.8 fastmap_1.1.1
## [13] R6_2.5.1 labeling_0.4.3 generics_0.1.3 knitr_1.45
## [17] tibble_3.2.1 munsell_0.5.0 bslib_0.6.1 pillar_1.9.0
## [21] tzdb_0.4.0 rlang_1.1.3 utf8_1.2.4 cachem_1.0.8
## [25] xfun_0.41 sass_0.4.8 bit64_4.0.5 cli_3.6.2
## [29] withr_3.0.0 magrittr_2.0.3 digest_0.6.34 grid_4.3.2
## [33] vroom_1.6.3 rstudioapi_0.15.0 hms_1.1.3 lifecycle_1.0.4
## [37] vctrs_0.6.5 evaluate_0.23 glue_1.7.0 farver_2.1.1
## [41] fansi_1.0.6 colorspace_2.1-0 rmarkdown_2.25 purrr_1.0.2
## [45] tools_4.3.2 pkgconfig_2.0.3 htmltools_0.5.7