Module12

Author

Hyunjeong Sin

Read the data

library(sf)
Linking to GEOS 3.13.1, GDAL 3.11.0, PROJ 9.6.0; sf_use_s2() is TRUE
library(ggplot2)
library(viridis)
Loading required package: viridisLite
library(RColorBrewer)
library(terra)
terra 1.8.54
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:terra':

    intersect, union
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
countries <- st_read("./countries/countries/countries.shp", quiet = TRUE)
names(countries)
 [1] "SP_ID"      "scalerank"  "featurecla" "labelrank"  "sovereignt"
 [6] "sov_a3"     "adm0_dif"   "level"      "type"       "admin"     
[11] "adm0_a3"    "geou_dif"   "geounit"    "gu_a3"      "su_dif"    
[16] "subunit"    "su_a3"      "brk_diff"   "name"       "name_long" 
[21] "brk_a3"     "brk_name"   "brk_group"  "abbrev"     "postal"    
[26] "formal_en"  "formal_fr"  "note_adm0"  "note_brk"   "name_sort" 
[31] "name_alt"   "mapcolor7"  "mapcolor8"  "mapcolor9"  "mapcolor13"
[36] "pop_est"    "gdp_md_est" "pop_year"   "lastcensus" "gdp_year"  
[41] "economy"    "income_grp" "wikipedia"  "fips_10"    "iso_a2"    
[46] "iso_a3"     "iso_n3"     "un_a3"      "wb_a2"      "wb_a3"     
[51] "woe_id"     "adm0_a3_is" "adm0_a3_us" "adm0_a3_un" "adm0_a3_wb"
[56] "continent"  "region_un"  "subregion"  "region_wb"  "name_len"  
[61] "long_len"   "abbrev_len" "tiny"       "homepart"   "geometry"  
countries
Simple feature collection with 177 features and 64 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -180 ymin: -90 xmax: 180 ymax: 83.64513
CRS:           NA
First 10 features:
   SP_ID scalerank      featurecla labelrank           sovereignt sov_a3
1      0         1 Admin-0 country         3          Afghanistan    AFG
2      1         1 Admin-0 country         3               Angola    AGO
3      2         1 Admin-0 country         6              Albania    ALB
4      3         1 Admin-0 country         4 United Arab Emirates    ARE
5      4         1 Admin-0 country         2            Argentina    ARG
6      5         1 Admin-0 country         6              Armenia    ARM
7      6         1 Admin-0 country         4           Antarctica    ATA
8      7         3 Admin-0 country         6               France    FR1
9      8         1 Admin-0 country         2            Australia    AU1
10     9         1 Admin-0 country         4              Austria    AUT
   adm0_dif level              type                               admin adm0_a3
1         0     2 Sovereign country                         Afghanistan     AFG
2         0     2 Sovereign country                              Angola     AGO
3         0     2 Sovereign country                             Albania     ALB
4         0     2 Sovereign country                United Arab Emirates     ARE
5         0     2 Sovereign country                           Argentina     ARG
6         0     2 Sovereign country                             Armenia     ARM
7         0     2     Indeterminate                          Antarctica     ATA
8         1     2        Dependency French Southern and Antarctic Lands     ATF
9         1     2           Country                           Australia     AUS
10        0     2 Sovereign country                             Austria     AUT
   geou_dif                             geounit gu_a3 su_dif
1         0                         Afghanistan   AFG      0
2         0                              Angola   AGO      0
3         0                             Albania   ALB      0
4         0                United Arab Emirates   ARE      0
5         0                           Argentina   ARG      0
6         0                             Armenia   ARM      0
7         0                          Antarctica   ATA      0
8         0 French Southern and Antarctic Lands   ATF      0
9         0                           Australia   AUS      0
10        0                             Austria   AUT      0
                               subunit su_a3 brk_diff                   name
1                          Afghanistan   AFG        0            Afghanistan
2                               Angola   AGO        0                 Angola
3                              Albania   ALB        0                Albania
4                 United Arab Emirates   ARE        0   United Arab Emirates
5                            Argentina   ARG        0              Argentina
6                              Armenia   ARM        0                Armenia
7                           Antarctica   ATA        0             Antarctica
8  French Southern and Antarctic Lands   ATF        0 Fr. S. Antarctic Lands
9                            Australia   AUS        0              Australia
10                             Austria   AUT        0                Austria
                             name_long brk_a3                   brk_name
1                          Afghanistan    AFG                Afghanistan
2                               Angola    AGO                     Angola
3                              Albania    ALB                    Albania
4                 United Arab Emirates    ARE       United Arab Emirates
5                            Argentina    ARG                  Argentina
6                              Armenia    ARM                    Armenia
7                           Antarctica    ATA                 Antarctica
8  French Southern and Antarctic Lands    ATF Fr. S. and Antarctic Lands
9                            Australia    AUS                  Australia
10                             Austria    AUT                    Austria
   brk_group     abbrev postal
1       <NA>       Afg.     AF
2       <NA>       Ang.     AO
3       <NA>       Alb.     AL
4       <NA>     U.A.E.     AE
5       <NA>       Arg.     AR
6       <NA>       Arm.    ARM
7       <NA>       Ant.     AQ
8       <NA> Fr. S.A.L.     TF
9       <NA>       Auz.     AU
10      <NA>      Aust.      A
                                              formal_en formal_fr note_adm0
1                          Islamic State of Afghanistan      <NA>      <NA>
2                           People's Republic of Angola      <NA>      <NA>
3                                   Republic of Albania      <NA>      <NA>
4                                  United Arab Emirates      <NA>      <NA>
5                                    Argentine Republic      <NA>      <NA>
6                                   Republic of Armenia      <NA>      <NA>
7                                                  <NA>      <NA>      <NA>
8  Territory of the French Southern and Antarctic Lands      <NA>       Fr.
9                             Commonwealth of Australia      <NA>      <NA>
10                                  Republic of Austria      <NA>      <NA>
                           note_brk                           name_sort
1                              <NA>                         Afghanistan
2                              <NA>                              Angola
3                              <NA>                             Albania
4                              <NA>                United Arab Emirates
5                              <NA>                           Argentina
6                              <NA>                             Armenia
7  Multiple claims held in abeyance                          Antarctica
8                              <NA> French Southern and Antarctic Lands
9                              <NA>                           Australia
10                             <NA>                             Austria
   name_alt mapcolor7 mapcolor8 mapcolor9 mapcolor13  pop_est gdp_md_est
1      <NA>         5         6         8          7 28400000    22270.0
2      <NA>         3         2         6          1 12799293   110300.0
3      <NA>         1         4         1          6  3639453    21810.0
4      <NA>         2         1         3          3  4798491   184300.0
5      <NA>         3         1         3         13 40913584   573900.0
6      <NA>         3         1         2         10  2967004    18770.0
7      <NA>         4         5         1        -99     3802      760.4
8      <NA>         7         5         9         11      140       16.0
9      <NA>         1         2         2          7 21262641   800200.0
10     <NA>         3         1         3          4  8210281   329500.0
   pop_year lastcensus gdp_year                    economy
1       -99       1979      -99  7. Least developed region
2       -99       1970      -99  7. Least developed region
3       -99       2001      -99       6. Developing region
4       -99       2010      -99       6. Developing region
5       -99       2010      -99    5. Emerging region: G20
6       -99       2001      -99       6. Developing region
7       -99        -99      -99       6. Developing region
8       -99        -99      -99       6. Developing region
9       -99       2006      -99 2. Developed region: nonG7
10      -99       2011      -99 2. Developed region: nonG7
                income_grp wikipedia fips_10 iso_a2 iso_a3 iso_n3 un_a3 wb_a2
1            5. Low income       -99    <NA>     AF    AFG    004   004    AF
2   3. Upper middle income       -99    <NA>     AO    AGO    024   024    AO
3   4. Lower middle income       -99    <NA>     AL    ALB    008   008    AL
4  2. High income: nonOECD       -99    <NA>     AE    ARE    784   784    AE
5   3. Upper middle income       -99    <NA>     AR    ARG    032   032    AR
6   4. Lower middle income       -99    <NA>     AM    ARM    051   051    AM
7  2. High income: nonOECD       -99    <NA>     AQ    ATA    010  -099   -99
8  2. High income: nonOECD       -99    <NA>     TF    ATF    260  -099   -99
9     1. High income: OECD       -99    <NA>     AU    AUS    036   036    AU
10    1. High income: OECD       -99    <NA>     AT    AUT    040   040    AT
   wb_a3 woe_id adm0_a3_is adm0_a3_us adm0_a3_un adm0_a3_wb
1    AFG    -99        AFG        AFG        -99        -99
2    AGO    -99        AGO        AGO        -99        -99
3    ALB    -99        ALB        ALB        -99        -99
4    ARE    -99        ARE        ARE        -99        -99
5    ARG    -99        ARG        ARG        -99        -99
6    ARM    -99        ARM        ARM        -99        -99
7    -99    -99        ATA        ATA        -99        -99
8    -99    -99        ATF        ATF        -99        -99
9    AUS    -99        AUS        AUS        -99        -99
10   AUT    -99        AUT        AUT        -99        -99
                 continent               region_un                 subregion
1                     Asia                    Asia             Southern Asia
2                   Africa                  Africa             Middle Africa
3                   Europe                  Europe           Southern Europe
4                     Asia                    Asia              Western Asia
5            South America                Americas             South America
6                     Asia                    Asia              Western Asia
7               Antarctica              Antarctica                Antarctica
8  Seven seas (open ocean) Seven seas (open ocean)   Seven seas (open ocean)
9                  Oceania                 Oceania Australia and New Zealand
10                  Europe                  Europe            Western Europe
                    region_wb name_len long_len abbrev_len tiny homepart
1                  South Asia       11       11          4  -99        1
2          Sub-Saharan Africa        6        6          4  -99        1
3       Europe & Central Asia        7        7          4  -99        1
4  Middle East & North Africa       20       20          6  -99        1
5   Latin America & Caribbean        9        9          4  -99        1
6       Europe & Central Asia        7        7          4  -99        1
7                  Antarctica       10       10          4  -99        1
8          Sub-Saharan Africa       22       35         10    2      -99
9         East Asia & Pacific        9        9          4  -99        1
10      Europe & Central Asia        7        7          5  -99        1
                         geometry
1  MULTIPOLYGON (((61.21082 35...
2  MULTIPOLYGON (((16.32653 -5...
3  MULTIPOLYGON (((20.59025 41...
4  MULTIPOLYGON (((51.57952 24...
5  MULTIPOLYGON (((-65.5 -55.2...
6  MULTIPOLYGON (((43.58275 41...
7  MULTIPOLYGON (((-59.57209 -...
8  MULTIPOLYGON (((68.935 -48....
9  MULTIPOLYGON (((145.398 -40...
10 MULTIPOLYGON (((16.97967 48...

Make plots of variables

a) Median GDP

my_breaks = c(0, 10000, 100000, 1000000)
countries_clean <- subset(countries, gdp_md_est > 0)
ggplot() +
  geom_sf(data = countries_clean, aes(fill = gdp_md_est)) +
  scale_fill_viridis(option = "magma", trans = "log", breaks = my_breaks, labels = my_breaks) +
  theme_bw()

b) Income group

ggplot() +
  geom_sf(data = countries, aes(fill = income_grp)) +
  scale_fill_brewer(palette = "Set3") +
  theme_bw()

Calculate the median NDVI for Bethel Island and Oakley

Polygon Extraction: Bethel Island and Oakley

b5 <- rast("./rs/rs/LC08_044034_20170614_B5.tif")
b4 <- rast("./rs/rs/LC08_044034_20170614_B4.tif")
ndvi <- (b5 - b4) / (b5 + b4)
ca_places <- st_read("./ca_places/ca_places/ca_places.shp", quiet = TRUE)
ca_places <- st_transform(ca_places, crs(ndvi))
bethel <- ca_places %>% filter(NAME == "Bethel Island")
bethel
Simple feature collection with 1 feature and 16 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 616158.3 ymin: 4207683 xmax: 622256.6 ymax: 4212060
Projected CRS: WGS 84 / UTM zone 10N
  STATEFP PLACEFP  PLACENS   GEOID          GEOIDFQ          NAME
1      06   06210 02407834 0606210 1600000US0606210 Bethel Island
           NAMELSAD LSAD CLASSFP PCICBSA MTFCC FUNCSTAT    ALAND  AWATER
1 Bethel Island CDP   57      U1       N G4210        S 13599384 1468307
     INTPTLAT     INTPTLON                       geometry
1 +38.0281297 -121.6298509 MULTIPOLYGON (((616158.3 42...
oakley <- ca_places %>% filter(NAME == "Oakley")
oakley
Simple feature collection with 1 feature and 16 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 609198.2 ymin: 4203114 xmax: 620975.2 ymax: 4208891
Projected CRS: WGS 84 / UTM zone 10N
  STATEFP PLACEFP  PLACENS   GEOID          GEOIDFQ   NAME    NAMELSAD LSAD
1      06   53070 02411294 0653070 1600000US0653070 Oakley Oakley city   25
  CLASSFP PCICBSA MTFCC FUNCSTAT    ALAND AWATER    INTPTLAT     INTPTLON
1      C1       N G4110        A 41091677 789638 +37.9961485 -121.6936235
                        geometry
1 MULTIPOLYGON (((609214.1 42...

NDVI Extraction and Median Calculation

a) Bethel Island

ndvi_bethel <- extract(ndvi, bethel)
median_bethel <- median(ndvi_bethel[,2], na.rm = TRUE)
median_bethel
[1] 0.4180438

b) Oakley

ndvi_oakley <- extract(ndvi, oakley)
median_oakley <- median(ndvi_oakley[,2], na.rm = TRUE)
median_oakley
[1] 0.3005387

Bonus: Compare all NDVI values for these two places using a t-test

t.test(ndvi_bethel[,2], ndvi_oakley[,2])

    Welch Two Sample t-test

data:  ndvi_bethel[, 2] and ndvi_oakley[, 2]
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 

A statistically significant difference exists in the mean NDVI values between Bethel Island and Oakley (p < 0.001). Bethel Island’s NDVI value is slightly higher, suggesting that its vegetation is more abundant or healthier.