Remember to download and put into data subdirectory:

Load the following into browser window:

Set-up R Console:

library(raster)
library(ggplot2)

raster import and structure

dsm_harv <- raster("data/NEON-airborne/HARV_dsmCrop.tif")

Plotting spatial data with ggplot

Three steps (write on board):

  1. Do Spatial Work (just importing so far)
  2. Convert to Data Frame (this is what ggplot works with)
  3. Make Plots
dsm_harv_df = as.data.frame(dsm_harv, xy = TRUE)
head(dsm_harv_df)
ggplot() +
  geom_raster(data = dsm_harv_df, 
              aes(x = x, y = y, fill = HARV_dsmCrop))
ggplot() +
  geom_histogram(data = dsm_harv_df, 
                 aes(x = HARV_dsmCrop))

Do Task 1 of Canopy Height from Space.

raster math

Show > * Canopy Height Model picture

dtm_harv <- raster("data/NEON-airborne/HARV_dtmCrop.tif")
chm_harv <- dsm_harv - dtm_harv
chm_harv_df = as.data.frame(chm_harv, xy = TRUE)
ggplot() +
  geom_raster(data = _harv_df, 
              aes(x = x, y = y, fill = layer))

Do Task 2 of Canopy Height from Space.

Import and reproject shapefiles

library(rgdal)

plots_harv <- readOGR("data/NEON-airborne/plot_locations", 
                      "HARV_plots")
chm_harv_df = as.data.frame(chm_harv, xy = TRUE)
plots_harv_df = as.data.frame(plots_harv)
ggplot() +
  geom_raster(data = chm_harv_df, 
              aes(x = x, y = y, fill = layer)) +
  geom_point(data = plots_harv_df, 
             aes(x = coords.x1, y = coords.x2), color = "yellow")
crs(chm_harv)
crs(plots_harv)
plots_harv_utm <- spTransform(plots_harv, crs(chm_harv))
plots_harv_utm_df = as.data.frame(plots_harv_utm)
ggplot() +
  geom_raster(data = chm_harv_df, 
              aes(x = x, y = y, fill = layer)) +
  geom_point(data = plots_harv_utm_df, 
             aes(x = coords.x1, y = coords.x2), color = "yellow")

Do Task 3 of Canopy Height from Space.

Extract raster data

plots_chm <- extract(chm_harv, plots_harv_utm)
plots_harv_utm$plot_id
plots_chm <- data.frame(plot_num = plots_harv_utm$plot_id, plot_value = plots_chm)
extract(chm_harv, plots_harv_utm, buffer = 10)
extract(chm_harv, plots_harv_utm, buffer = 10, fun = mean)

Do Tasks 4-5 of Canopy Height from Space.

Map of point data

library(maps)
us_map = map_data("usa")
ggplot() +
  geom_polygon(data = us_map, 
               aes(x = long, y = lat, group = group), 
               fill = "grey")
ggplot() +
  geom_polygon(data = us_map, 
               aes(x = long, y = lat, group = group), 
               fill = "grey") +
  coord_quickmap()
library(spocc)

do_gbif = occ(query = "Dipodomys ordii", 
              from = "gbif", 
              limit = 1000, 
              has_coords = TRUE)
do_data = data.frame(dipo_df$gbif$data)
ggplot() +
  geom_polygon(data = us_map, 
               aes(x = long, y = lat, group = group), 
               fill = "grey") +
  geom_point(data = do_data, 
             aes(x = Dipodomys_ordii.longitude,
                 y = Dipodomys_ordii.latitude)) +
  coord_quickmap()

Do Species Occurrences Map.

Namespacing

library(dplyr)
library(raster)
select(do_data, Dipodomys_ordii.latitude)
dply::select(do_data, Dipodomys_ordii.latitude)

Making your own vector data

points_crs <- crs("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
do_data_spat <- SpatialPointsDataFrame(
	do_data[c('Dipodomys_ordii.longitude', 'Dipodomys_ordii.latitude')], 
	do_data, 
	proj4string = points_crs)
str(do_data_spat)

Do Species Occurrences Elevation Histogram