# loading master data
<- read.csv("PR_CSV/PR_TREE.csv") master
Analysis of FIA data
Objective
Calculate the relative frequency of each species in each inventory cycle, to answer the questions:
- What are the most common species in each inventory cycle?
- Do their frequency changed after hurricane María?
Data
Create data frame of species count by year
library(tidyr)
library(dplyr)
# pivot table
<- master %>%
spcountyr group_by(SPCD, INVYR) %>%
summarise(count = n()) %>%
pivot_wider(names_from = INVYR, values_from = count, values_fill = 0)
Modify data frame
Sum the counts per year for each inventory cycle.
# with the new data frame sum the counts per year for each inventory cycle: 2001 to 2004, 2006 to 2009, 2011 to 2014, 2016 to 2019 using the mutate function
<- spcountyr %>%
spcountyr mutate(Y2001_2004 = `2001` + `2002` + `2003` + `2004`,
Y2006_2009 = `2006` + `2007` + `2008` + `2009`,
Y2011_2014 = `2011` + `2012` + `2013` + `2014`,
Y2016_2019 = `2016` + `2017` + `2018` + `2019`) %>%
select(SPCD, Y2001_2004, Y2006_2009, Y2011_2014, Y2016_2019)
# transform data from int to numeric
<- lapply(spcountyr, as.numeric) spcountyr[]
Calculate relative frequency of each species in each inventory cycle
# calculate frequency relative of each species in each inventory cycle
<- spcountyr %>%
spcountyrfreq mutate(freq2001_2004 = Y2001_2004 / sum(spcountyr$Y2001_2004),
freq2006_2009 = Y2006_2009 / sum(spcountyr$Y2006_2009),
freq2011_2014 = Y2011_2014 / sum(spcountyr$Y2011_2014),
freq2016_2019 = Y2016_2019 / sum(spcountyr$Y2016_2019))
Species names
# load species names
<- read.csv("data/REF_SPECIES.csv")
spnames # merge spnames[GENUS, SPECIES] to spcountyrfreq using SPCD key
<- merge(spcountyrfreq, spnames, by = "SPCD")
spcountyrfreqsp # only GENUS and SPECIES columns
<- spcountyrfreqsp[, c(1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12)]
spcountyrfreqsp # order by frequency higher to lower using freq2001_2004
<- spcountyrfreqsp[order(spcountyrfreqsp$freq2016_2019, decreasing = TRUE), ] spcountyrfreqsp
Results
Table of frequencies for the 10 most abundant tree species (2014-2019 inventory)
# gt with first 6 rows and the following columns: GENUS, SPECIES, freq2001_2004, freq2006_2009, freq2011_2014, freq2016_2019 and frequency with 2 decimals
library(gt)
1:10, c(10, 11, 6:9)] %>%
spcountyrfreqsp[gt() %>%
tab_header(
title = "Relative frequency of each species in each inventory cycle",
subtitle = "First 10 species sorted according cycle 2016-2019"
%>%
) fmt_number(
columns = c(freq2001_2004, freq2006_2009, freq2011_2014, freq2016_2019),
decimals = 2
)
Relative frequency of each species in each inventory cycle | |||||
First 10 species sorted according cycle 2016-2019 | |||||
GENUS | SPECIES | freq2001_2004 | freq2006_2009 | freq2011_2014 | freq2016_2019 |
---|---|---|---|---|---|
Spathodea | campanulata | 0.12 | 0.11 | 0.10 | 0.10 |
Guarea | guidonia | 0.06 | 0.06 | 0.06 | 0.07 |
Leucaena | leucocephala | 0.04 | 0.04 | 0.04 | 0.04 |
Tabebuia | heterophylla | 0.03 | 0.04 | 0.04 | 0.03 |
Cecropia | schreberiana | 0.03 | 0.03 | 0.03 | 0.03 |
Prestoea | acuminata | 0.03 | 0.02 | 0.03 | 0.03 |
Andira | inermis | 0.03 | 0.03 | 0.02 | 0.02 |
Prosopis | pallida | 0.01 | 0.02 | 0.02 | 0.02 |
Bursera | simaruba | 0.02 | 0.04 | 0.03 | 0.02 |
Syzygium | jambos | 0.03 | 0.02 | 0.02 | 0.02 |
Graph of species frequencies versus rank.
# bar graph of species frequencies versus rank order use GENUS and SPECIES as labels
library(ggplot2)
1:10, c(10, 11, 6:9)] %>%
spcountyrfreqsp[ggplot(aes(x = reorder(paste(GENUS, SPECIES), freq2016_2019), y = freq2016_2019)) +
geom_bar(stat = "identity") +
coord_flip() +
labs(
title = "Relative frequency of each species",
subtitle = "First 10 species sorted according cycle 2016-2019",
x = "Species",
y = "Frequency"
)