Code
library(tidyverse)
library(sf) # tools for working with spatial vector data (GIS functionality, mapping)
library(elevatr) # access to raster elevation maps
library(terra)
library(stars)
library(tidycensus) # spatial data for the US with census information
library(USAboundaries) # access to boundaries for US states, counties, zip codes, and congressional districts 
Code
nc <- st_read(system.file("shape/nc.shp", package = "sf"))
Reading layer `nc' from data source 
  `/Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/library/sf/shape/nc.shp' 
  using driver `ESRI Shapefile'
Simple feature collection with 100 features and 14 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
Geodetic CRS:  NAD27
Code
mn_cities <- st_read("data/shp_loc_pop_centers/city_and_township_population_centers.shp")
Reading layer `city_and_township_population_centers' from data source 
  `/Users/lorenzogurrola/workspace/COMP212/portfolio-LorenzoGurrola/src/ica/04-adv-maps/data/shp_loc_pop_centers/city_and_township_population_centers.shp' 
  using driver `ESRI Shapefile'
Simple feature collection with 1081 features and 8 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 190832.6 ymin: 4816672 xmax: 747463.4 ymax: 5468045
Projected CRS: NAD83 / UTM zone 15N
Code
mn_water <- st_read("data/shp_water_lakes_rivers/LakesAndRivers.shp")
Reading layer `LakesAndRivers' from data source 
  `/Users/lorenzogurrola/workspace/COMP212/portfolio-LorenzoGurrola/src/ica/04-adv-maps/data/shp_water_lakes_rivers/LakesAndRivers.shp' 
  using driver `ESRI Shapefile'
Simple feature collection with 2313 features and 8 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 419538.6 ymin: 4922700 xmax: 522665 ymax: 5029945
Projected CRS: NAD83 / UTM zone 15N
Code
class(nc)
[1] "sf"         "data.frame"
Code
class(mn_cities)
[1] "sf"         "data.frame"
Code
class(mn_water)
[1] "sf"         "data.frame"
Code
st_crs(nc)
Coordinate Reference System:
  User input: NAD27 
  wkt:
GEOGCRS["NAD27",
    DATUM["North American Datum 1927",
        ELLIPSOID["Clarke 1866",6378206.4,294.978698213898,
            LENGTHUNIT["metre",1]]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS["latitude",north,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433]],
        AXIS["longitude",east,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433]],
    ID["EPSG",4267]]
Code
ggplot(nc) +
    geom_sf() +
    theme_classic() +
    labs(title = "NAD27")

Code
transform_and_plot <- function(spatial_obj, new_crs) {
    spatial_obj |> 
        st_transform(crs = new_crs) |> 
        ggplot() +
            geom_sf() +
            theme_classic()
}

# Example usage of this function (using a South Carolina CRS)
transform_and_plot(nc, new_crs = "EPSG:5588")

Code
mn_counties <- USAboundaries::us_counties(resolution = "high", states = "Minnesota")
names_counties <- names(mn_counties)
names(mn_counties)[names_counties == "state_name"] <- c("state_name1", "state_name2")
Code
st_crs(mn_cities)
Coordinate Reference System:
  User input: NAD83 / UTM zone 15N 
  wkt:
PROJCRS["NAD83 / UTM zone 15N",
    BASEGEOGCRS["NAD83",
        DATUM["North American Datum 1983",
            ELLIPSOID["GRS 1980",6378137,298.257222101,
                LENGTHUNIT["metre",1]]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["degree",0.0174532925199433]],
        ID["EPSG",4269]],
    CONVERSION["UTM zone 15N",
        METHOD["Transverse Mercator",
            ID["EPSG",9807]],
        PARAMETER["Latitude of natural origin",0,
            ANGLEUNIT["Degree",0.0174532925199433],
            ID["EPSG",8801]],
        PARAMETER["Longitude of natural origin",-93,
            ANGLEUNIT["Degree",0.0174532925199433],
            ID["EPSG",8802]],
        PARAMETER["Scale factor at natural origin",0.9996,
            SCALEUNIT["unity",1],
            ID["EPSG",8805]],
        PARAMETER["False easting",500000,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",0,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["(E)",east,
            ORDER[1],
            LENGTHUNIT["metre",1]],
        AXIS["(N)",north,
            ORDER[2],
            LENGTHUNIT["metre",1]],
    ID["EPSG",26915]]
Code
st_crs(mn_counties)
Coordinate Reference System:
  User input: EPSG:4326 
  wkt:
GEOGCRS["WGS 84",
    DATUM["World Geodetic System 1984",
        ELLIPSOID["WGS 84",6378137,298.257223563,
            LENGTHUNIT["metre",1]]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS["geodetic latitude (Lat)",north,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433]],
        AXIS["geodetic longitude (Lon)",east,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433]],
    USAGE[
        SCOPE["Horizontal component of 3D system."],
        AREA["World."],
        BBOX[-90,-180,90,180]],
    ID["EPSG",4326]]
Code
st_crs(mn_water)
Coordinate Reference System:
  User input: NAD83 / UTM zone 15N 
  wkt:
PROJCRS["NAD83 / UTM zone 15N",
    BASEGEOGCRS["NAD83",
        DATUM["North American Datum 1983",
            ELLIPSOID["GRS 1980",6378137,298.257222101,
                LENGTHUNIT["metre",1]]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["degree",0.0174532925199433]],
        ID["EPSG",4269]],
    CONVERSION["UTM zone 15N",
        METHOD["Transverse Mercator",
            ID["EPSG",9807]],
        PARAMETER["Latitude of natural origin",0,
            ANGLEUNIT["Degree",0.0174532925199433],
            ID["EPSG",8801]],
        PARAMETER["Longitude of natural origin",-93,
            ANGLEUNIT["Degree",0.0174532925199433],
            ID["EPSG",8802]],
        PARAMETER["Scale factor at natural origin",0.9996,
            SCALEUNIT["unity",1],
            ID["EPSG",8805]],
        PARAMETER["False easting",500000,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",0,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["(E)",east,
            ORDER[1],
            LENGTHUNIT["metre",1]],
        AXIS["(N)",north,
            ORDER[2],
            LENGTHUNIT["metre",1]],
    ID["EPSG",26915]]
Code
st_transform(mn_counties, crs = st_crs(mn_cities))
Simple feature collection with 87 features and 15 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 189778.2 ymin: 4816294 xmax: 761657.1 ymax: 5472414
Projected CRS: NAD83 / UTM zone 15N
First 10 features:
    statefp countyfp countyns       affgeoid geoid              name
35       27      011 00659451 0500000US27011 27011         Big Stone
36       27      027 00659459 0500000US27027 27027              Clay
37       27      119 00659505 0500000US27119 27119              Polk
38       27      077 00659483 0500000US27077 27077 Lake of the Woods
39       27      075 00659484 0500000US27075 27075              Lake
40       27      173 00659531 0500000US27173 27173   Yellow Medicine
256      27      139 00659514 0500000US27139 27139             Scott
257      27      129 00659510 0500000US27129 27129          Renville
531      27      137 00662850 0500000US27137 27137         St. Louis
532      27      031 00659461 0500000US27031 27031              Cook
                    namelsad stusps state_name1 lsad       aland     awater
35          Big Stone County     MN   Minnesota   06  1292831731   74957179
36               Clay County     MN   Minnesota   06  2706993098   19303394
37               Polk County     MN   Minnesota   06  5104864821   69390824
38  Lake of the Woods County     MN   Minnesota   06  3361396303 1248338649
39               Lake County     MN   Minnesota   06  5462555544 2283914208
40    Yellow Medicine County     MN   Minnesota   06  1966062964   10687958
256             Scott County     MN   Minnesota   06   922742777   31378277
257          Renville County     MN   Minnesota   06  2545711095   10851893
531         St. Louis County     MN   Minnesota   06 16181141791 1586529239
532              Cook County     MN   Minnesota   06  3762130687 4887924296
    state_name2 state_abbr jurisdiction_type                       geometry
35    Minnesota         MN             state MULTIPOLYGON (((201186.4 50...
36    Minnesota         MN             state MULTIPOLYGON (((208915.1 52...
37    Minnesota         MN             state MULTIPOLYGON (((191765 5343...
38    Minnesota         MN             state MULTIPOLYGON (((327670.7 53...
39    Minnesota         MN             state MULTIPOLYGON (((609767.3 52...
40    Minnesota         MN             state MULTIPOLYGON (((226986 4966...
256   Minnesota         MN             state MULTIPOLYGON (((427602.2 49...
257   Minnesota         MN             state MULTIPOLYGON (((303758.9 49...
531   Minnesota         MN             state MULTIPOLYGON (((492722.4 53...
532   Minnesota         MN             state MULTIPOLYGON (((754365.4 53...
Code
st_transform(mn_water, crs = st_crs(mn_cities))
Simple feature collection with 2313 features and 8 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 419538.6 ymin: 4922700 xmax: 522665 ymax: 5029945
Projected CRS: NAD83 / UTM zone 15N
First 10 features:
     OWF_ID SYSTEM             NAME_DNR ALT_NAME PRIORITY_L AREA_ACRES
1  00640874  River               Cannon     <NA>          N 346.103850
2  00642425  River                Crane     <NA>          N   9.723367
3  00642502  River                 Crow     <NA>          N 635.346941
4  00647923  River            Minnehaha     <NA>          N  18.167678
5  00648557  River             Ninemile     <NA>          N   7.694640
6  00650382  River                  Rum     <NA>          N 584.841980
7  00652101  River              Sixmile     <NA>          N   4.684512
8  00653634  River           Vermillion     <NA>          N 198.525506
9  00653940  River  West Branch Sunrise     <NA>          N  54.737067
10 00658357  River South Branch Sunrise     <NA>          N   9.335020
   Shape_Leng Shape_Area                       geometry
1   64422.966 1400632.59 MULTIPOLYGON (((498080.6 49...
2    5567.484   39349.07 MULTIPOLYGON (((420260.2 49...
3   86495.868 2571157.85 MULTIPOLYGON (((459094 5010...
4    8358.148   73521.98 MULTIPOLYGON (((468947.3 49...
5    3029.780   31139.10 MULTIPOLYGON (((474298.8 49...
6   96658.937 2366771.52 MULTIPOLYGON (((472923.7 50...
7    2339.238   18957.55 MULTIPOLYGON (((444710 4969...
8   40692.578  803404.22 MULTIPOLYGON (((514159.3 49...
9   14263.740  221513.05 MULTIPOLYGON (((496143.8 50...
10   4554.234   37777.49 MULTIPOLYGON (((498783.5 50...
Code
library(ggthemes)
ggplot() +
  geom_sf(data = mn_counties) +
  geom_sf(data = mn_cities, color = "blue", size = 0.7) +
  theme_classic() + 
  ggthemes::theme_map()

Code
ggplot() +
    geom_sf(data = mn_counties, fill = "white") + 
    geom_sf(data = mn_cities |> filter(Population >= 10000), mapping = aes(color = Population, size = Population)) + # cities layer
    scale_color_viridis_c() + # continuous (gradient) color scale
    labs(title = "Minnesota Cities with Population >= 10,000") + 
    ggthemes::theme_map() +
    theme(legend.position = "bottom") # move legend

Code
elevation <- elevatr::get_elev_raster(mn_counties, z = 5, clip = "bbox")
Code
raster::crs(elevation) <- sf::st_crs(mn_counties)
Code
elev_df <- elevation |> terra::as.data.frame(xy = TRUE)
Code
colnames(elev_df) <- c("x", "y", "elevation")
Code
ggplot() +
  geom_raster(data = elev_df, aes(x=x, y=y, fill=elevation)) +
  scale_fill_gradient(low = "darkgreen", high = "white", guide = FALSE) +
  geom_sf(data = mn_counties, fill = NA, color = "black") +
  geom_sf(data = mn_cities |> filter(Population >= 10000), mapping = aes(color = Population, size = Population))+ # cities layer
    scale_color_viridis_c() + # continuous (gradient) color scale
    labs(title = "Minnesota Cities with Population >= 10,000") + 
    ggthemes::theme_map() +
    theme(legend.position = "bottom") # move legend

Code
seven_countyarea <- mn_counties |>
    filter(name %in% c("Anoka", "Hennepin", "Ramsey", "Dakota", "Carver", "Washington", "Scott")) |> 
    st_bbox()