###Final Project

library (raster)
## Loading required package: sp
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:raster':
## 
##     intersect, select, union
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(sp)
library(spData)
library(tmap)
library(tmaptools)

###Data Input

if (!file.exists("1950-2018-torn-initpoint")) { 
  download.file("http://www.spc.noaa.gov/gis/svrgis/zipped/1950-2018-torn-initpoint.zip",
                destfile = "temporary.zip")
  unzip("temporary.zip")
}
tornado <- st_read(dsn = "1950-2018-torn-initpoint")%>%
  filter(yr == 2006:2010)
## Reading layer `1950-2018-torn-initpoint' from data source `C:\RData\1950-2018-torn-initpoint' using driver `ESRI Shapefile'
## Simple feature collection with 63645 features and 22 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: -163.53 ymin: 18.13 xmax: -64.9 ymax: 61.02
## geographic CRS: WGS 84
fire <- raster("GrossEmissions_v101_USA_Fire.tif")

insect <- raster("GrossEmissions_v101_USA_Insect.tif")

The goal of this project is to compare the prevalanece of insect, fire, and tornado damage to gain insight into which of these occurences is more impactful upon the Continental US.

plot(fire,
     main = "Gross Carbon Emissions from Fire Damage\n across CONUS Forests (2006-2010)",
     xlab = "horizontal extent (m)",
     ylab = "vertical extent (m)",
     legend.args = list(text = "Mg C/yr\n", side = 3),
     colNA = "black",
     box = FALSE)

plot(insect,
     main = "Gross Carbon Emissions from Insect Damage\n across CONUS Forests (2006-2010)",
     xlab = "horizontal extent (m)",
     ylab = "vertical extent (m)",
     legend.args = list(text = "Mg C/yr\n", side = 3),
     colNA = "black",
     box = FALSE)

##The spatial extent of the RasterLayer object is displayed on the x- and y-axes. All N/A cells (i.e., cells that have no values) are colored black for better visualization of fire & insect damage. The legend offers the range of megagrams of Carbon emmisions per year and represents them using a default color theme.

After examining both sets of data, which measure mg of Carbon per year due to fire, and also Carbon emissions from insects, there seems to be a prevalence of damage throughout various sections of the country. At first glance, very little may seem to intersect with one another. Though it does warrant further exploration.

tm_shape(world) +
  tm_polygons() +
tm_shape(world[world$name_long == "United States", ]) +
  tm_polygons(col = "white") +
tm_shape(tornado) +
  tm_dots() + 
  tm_facets(by = "yr", as.layers = TRUE) +
tm_shape(us_states, projection = 28, is.master = TRUE) + 
  tm_borders() +
   tm_style("natural") +
  tm_layout(main.title = "Contiguous U.S. Tornadoes [2006-2010]",
            main.title.position = "center", main.title.size = .85,
            panel.labels = tornado$yr[2006:2010]) +
  tm_credits(c(rep("", 11), "Data Source: U.S. SPC"), 
             position = c("right", "bottom"))
## Warning in CPL_crs_from_input(x): GDAL Error 1: PROJ: proj_create_from_database:
## crs not found