knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  echo = TRUE, message=F, warning=F)

Install npstools & Configure Path to Tables

You only need to run this once, or again to update package.

library(devtools) # install.packages("devtools")

devtools::install_github("ecoquants/npstools") # devtools::install() # for developing R package

You'll need access to the databaes tables in comma-seperated value (CSV) format. If you don't have access to the shared drive where they're stored internally, you can use this copy:

You'll then need to unzip the contents and create your own configuration file with this path, similar to:

It is recommended that you setup this file in your home directory "~/nps_config.yaml" or an RStudio Project so you can reference it with here::here("nps_config.yaml").

Setup: Load Libraries & Configuration, Set Park & Year

TODO: Use rstudio/config: config package for R.

# load libraries
#library(npstools)
devtools::load_all()
library(tidyverse)
library(here)
library(glue)

# load your own configuration, which could be based off package
nps_config_yaml <- system.file(package="npstools", "nps_config.yaml") 
cfg <- get_nps_config(nps_config_yaml)

# specify park and year of interest
park <- "CABR" # "CABR" | "CHIS" | "SAMO"
year <- 2015

Get Tables

Table of Species Richness

Table E.6. Species richness (per transect) observed in 20XX monitoring of [PARK] vegetation.

# set optional path to Excel spreadsheet output
#n_spp_xlsx <- here(glue("data/spp_richness_pivot_{park}_{year}.xlsx"))

# get species richness pivot table
n_spp_tbl <- get_n_spp_pivtbl(cfg, park, year) # , xlsx=n_spp_xlsx)

# render pivot table as html widget
n_spp_tbl$renderPivot()

Table of Percent Cover

Table of data for...

Figure E.2. Absolute foliar cover (%) of plant growth forms, as observed during 20XX monitoring at CABR. Colored bars show mean values, while error bars extend ±1 s.d. from the means.

Render Interactive Table

# get percent cover
pct_cover_tbl <- get_pct_cover_tbl(cfg, park, year)

# render interactive table, without Query_type column
pct_cover_tbl %>% 
  select(-Query_type) %>%
  DT::datatable() %>%
  DT::formatRound(columns=c("Average", "StdDev"), digits=3)

Render Static Table

# render static table, for first 10 rows without Query_type column
pct_cover_tbl %>% 
  head(10) %>% 
  select(-Query_type) %>%
  knitr::kable(digits=3)

Plots

Map of locations, clustered

TODO:

library(tidyverse)
library(leaflet)
library(mapview)
library(glue)
library(sf)

to_lonlat <- function(x, y, crs_str){
  st_as_sf(data_frame(x=x, y=y), crs=crs_str, coords = c("x", "y")) %>%
    st_transform(crs=4326) %>%
    st_coordinates()
}

locations <- tbl_Locations %>%
  mutate(
    # fix one location
    Y_Coord   = ifelse(Location_ID == 1502833764, B_Y_Coord, Y_Coord)) %>%
  # TODO: fix other locations
  filter(!is.na(X_Coord), !is.na(Y_Coord), !is.na(UTM_Zone)) %>%
  mutate(
    crs_str = glue("+proj={tolower(Coord_System)} +zone={UTM_Zone} +datum={Datum} +units={c(meters='m')[Coord_Units]}"),
    lon_lat = pmap(list(X_Coord, Y_Coord, crs_str), to_lonlat),
    lon     = map_dbl(lon_lat, ~.[,1]),
    lat     = map_dbl(lon_lat, ~.[,2])) %>%
  st_as_sf(coords = c("lon", "lat"), crs=4326)

leaflet(locations) %>%
  addProviderTiles(providers$Esri.OceanBasemap) %>%
  addMarkers(
    clusterOptions = markerClusterOptions(),
    popup = ~glue(
      "<b>Location_ID</b>: {Location_ID}<br>
      <b>Vegetation_Community</b>: {Vegetation_Community}<br>
      <b>Loc_Notes</b>: {Loc_Notes}"))

Plot of species richness

#devtools::load_all()
#n_spp_tbl <- get_n_spp_pivtbl(cfg, park, year) # , xlsx=n_spp_xlsx)
tbl_nspp <- attr(n_spp_tbl, "data")
#View(tbl_nspp)

tbl_nspp_sum <- tbl_nspp %>% 
  group_by(Nativity, Life_Form, Vegetation_Community) %>% 
  summarize(
    nspp = n())

#table(tbl_nspp_sum$Nativity)
#fill=FxnGroup, group=FxnGroup, color=FxnGroup, 
g <- ggplot(tbl_nspp_sum, aes(x=Vegetation_Community, y=nspp, fill=Life_Form, alpha=Nativity)) + 
  #facet_wrap(~Vegetation_Community) +
  geom_bar(stat="identity", position="dodge") + # , colour="black"
  scale_alpha_manual(values=c(1, 0.6, 0.3)) +
  labs(x="Vegetation Community", y="# Species") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))
  #coord_flip()

# TODO: FxnGroup = "All Sites"
g

Plot of species richness, dynamic

library(plotly)

ggplotly(g)

Plot of Percent Cover



ecoquants/npstools documentation built on May 10, 2019, 9:50 a.m.