Module12_Gordon

Author

Mason Gordon

Module 12

Exercise 1

Make plots of the following variables - Median GDP: (gdp_md_est)

ggplot() + 
  geom_sf(data = countries, aes(fill = gdp_md_est)) +
  scale_fill_viridis_c(option = "viridis", trans = "log") +
  theme_bw()
Warning in transformation$transform(x): NaNs produced
Warning in scale_fill_viridis_c(option = "viridis", trans = "log"):
log-2.718282 transformation introduced infinite values.

  • Population: (pop_est)
ggplot() + 
  geom_sf(data = countries, aes(fill = pop_est)) +
  scale_fill_viridis_c(option = "magma", trans = "log", direction = -1) +
  theme_bw()
Warning in transformation$transform(x): NaNs produced
Warning in scale_fill_viridis_c(option = "magma", trans = "log", direction =
-1): log-2.718282 transformation introduced infinite values.

  • Income group: (income_grp)
ggplot() + 
  geom_sf(data = countries, aes(fill = income_grp)) +
  scale_fill_viridis_d(option = "mako") + 
  theme_bw()

Exercise 2

  • calculate the median NDVI for Bethel Island and Oakley
### Blue
b2 <- rast('../data/rs/rs/LC08_044034_20170614_B2.tif')
### Green
b3 <- rast('../data/rs/rs/LC08_044034_20170614_B3.tif')
### Red
b4 <- rast('../data/rs/rs/LC08_044034_20170614_B4.tif')
### Near Infrared (NIR)
b5 <- rast('../data/rs/rs/LC08_044034_20170614_B5.tif')

s <- c(b5, b4, b3, b2)

ndvi <- (b5 - b4) / (b5 + b4)

ca_places <- st_read("../data/ca_places/ca_places/ca_places.shp", quiet = TRUE)

ca_places <- st_transform(ca_places, 32610)

bethel <- ca_places %>% 
  dplyr::filter(NAME == "Bethel Island")

Bethel_NDVI <- extract(b5, bethel, fun = 'median')[, 2]

oakley <- ca_places %>% 
  dplyr::filter(NAME == "Oakley")

oakley_NDVI <- extract(b5, oakley, fun = 'median')[, 2]

cat("Bethel median NDVI:", Bethel_NDVI, "\n")
Bethel median NDVI: 0.3779947 
cat("Oakley median NDVI:", oakley_NDVI, "\n")
Oakley median NDVI: 0.2823575 

-extract all NDVI values for these two places and compare using a t-test

Bethel_NDVI <- extract(ndvi, bethel)
Oakley_NDVI <- extract(ndvi, oakley)

bethel_all_NDVI <- na.omit(Bethel_NDVI[, 2])
oakley_all_NDVI <- na.omit(Oakley_NDVI[, 2])

ndvi_ttest <- t.test(bethel_all_NDVI, oakley_all_NDVI)

print(ndvi_ttest)

    Welch Two Sample t-test

data:  bethel_all_NDVI and oakley_all_NDVI
t = 45.979, df = 26353, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 0.07298078 0.07948010
sample estimates:
mean of x mean of y 
0.4124472 0.3362167