R/plt-ecorgns.R

# Prerequisite Scripts:
# write-lvl2-geojson.R
# import.R
library(sf)
library(maps)


# Would like to visualize the area we're looking at in context.
# Reads state map from map package and converts to sf object.
states <- st_as_sf(maps::map("state", plot = FALSE, fill = TRUE))

if (st_crs(cec_ecoregions) != st_crs(states)) {
  stop("Ecoregion objects have different CRS' than states")
}

# Plots ecoregion and cnc_area over USA map. 
states %>%
  ggplot() +
  geom_sf() + 
  geom_sf(data = lvl2_ecoregions, aes(color = factor(LEVEL2), 
                                      fill  = factor(LEVEL2)), alpha = 0.4) +
  geom_sf(data = cnc_area, fill = 'blue', alpha = 0.6) +
  labs(title = "Boston CNC Area within CEC defined Level II Ecoregions",
       caption = "Boston CNC area is in blue.",
       fill = "Level 2 Ecoregions") +
  scale_color_manual(values = c("red", "green"), guide = FALSE) + # Removes color legend.
  scale_fill_manual(values = c("red", "green")) +
  theme_minimal()

# Saves plot.
ggsave("outputs/graphics/cec-lvl2-ecorgn-cnc-map.png")

# Lets take a closer look at the Boston CNC area.
states %>%
  filter(ID %in% c('massachusetts', 'connecticut', 'new hampshire',
                   'rhode island', 'new york', 'new jersey')) %>% 
  ggplot() +
  geom_sf() + 
  geom_sf(data = filter(lvl3_ecoregions),
          aes(color = factor(LEVEL3), 
              fill  = factor(LEVEL3)), alpha = 0.4) +
  geom_sf(data = cnc_area, fill = 'blue', alpha = 0.6) +
  labs(title = "Boston CNC Area within CEC defined Level III Ecoregions",
       caption = "Boston CNC area is in blue.",
       fill = "Level 3 Ecoregions") +
  scale_color_manual(values = c("red", "green"), guide = FALSE) + # Removes color legend.
  scale_fill_manual(values = c("red", "green")) +
  theme_minimal()

ggsave("outputs/graphics/cec-lvl3-ecorgn-map.png")


# Converts bbox1 WKT string to a tibble, then converts to simple geometry object, for use with ggplot.
bbox <- st_bbox(lvl2_ecoregions)

bbox_sf <- st_as_sfc(bbox, # Converts WKT string to a tibble, then converts to simple geometry object.
                     crs = st_crs(states))

# Plots the bounding box used to query GBIF for our Level II Ecoregions.
states %>%
  ggplot() +
  geom_sf() + 
  geom_sf(data = lvl2_ecoregions, aes(color = factor(LEVEL2), 
                                      fill  = factor(LEVEL2)), alpha = 0.4) +
  geom_sf(data = cnc_area, fill = 'blue', alpha = 0.6) +
  labs(title = "Boston CNC Area within CEC defined Level II Ecoregions with GBIF Bounding Box",
       caption = "Boston CNC area is in blue.",
       fill = "Level 2 Ecoregions") +
  geom_sf(data = bbox_sf, alpha = 0.2, color = "black") +
  scale_color_manual(values = c("red", "green"), guide = FALSE) + # Removes color legend.
  scale_fill_manual(values = c("red", "green")) +
  theme_minimal()

ggsave("outputs/graphics/lvl2-ecorgn-bbox.png")

# Removing extraneous objects.
rm(lvl3_ecoregions, cec_ecoregions, states, lvl2_ecoregions)  
iozeroff/cncpointR documentation built on Feb. 4, 2020, 6:18 p.m.