library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ purrr   0.3.4
## ✓ tibble  3.1.6     ✓ dplyr   1.0.8
## ✓ tidyr   1.2.0     ✓ stringr 1.4.0
## ✓ readr   2.1.2     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(flexdashboard)
library(hablar)
## 
## Attaching package: 'hablar'
## The following object is masked from 'package:dplyr':
## 
##     na_if
## The following object is masked from 'package:tibble':
## 
##     num
library(rsconnect)
library(janitor)
## 
## Attaching package: 'janitor'
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
library(ggpubr)
library(DataExplorer)
library(fmsb)
library(corrr)
library(devtools)
## Loading required package: usethis
## 
## Attaching package: 'devtools'
## The following object is masked from 'package:rsconnect':
## 
##     lint
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(sf)
## Linking to GEOS 3.9.1, GDAL 3.4.0, PROJ 8.1.1; sf_use_s2() is TRUE
library(viridis)
## Loading required package: viridisLite
library(raster)
## Loading required package: sp
## 
## Attaching package: 'raster'
## The following object is masked from 'package:dplyr':
## 
##     select
library(rgeos)
## rgeos version: 0.5-9, (SVN revision 684)
##  GEOS runtime version: 3.8.1-CAPI-1.13.3 
##  Please note that rgeos will be retired by the end of 2023,
## plan transition to sf functions using GEOS at your earliest convenience.
##  Linking to sp version: 1.4-6 
##  Polygon checking: TRUE
library(RColorBrewer)
library(jsonlite)
## 
## Attaching package: 'jsonlite'
## The following object is masked from 'package:purrr':
## 
##     flatten
library(scales)
## 
## Attaching package: 'scales'
## The following object is masked from 'package:viridis':
## 
##     viridis_pal
## The following object is masked from 'package:purrr':
## 
##     discard
## The following object is masked from 'package:readr':
## 
##     col_factor
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:raster':
## 
##     select
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(tmap)

#my.token <- read_lines("./token_2.txt")
#Sys.setenv("MAPBOX_TOKEN" = my.token)

#saveRDS(my.token, file = "mattyp_mapbox_token.rds")


#my.token <- readRDS("mattyp_mapbox_token.rds")
#Sys.setenv("MAPBOX_TOKEN" = my.token)
# data manip

setwd("/Users/matthewpalagyi/Documents/R Folder/'22 Folder/Lab_Exercises/Lab Assignment 5")

#steps1 - heritage
heritage_tibble <- as_tibble(read_csv("./data/Whose Heritage.csv"))
## Warning: One or more parsing issues, see `problems()` for details
## Rows: 2523 Columns: 18
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (12): feature_name, Honorees, City, County, State, Side, Symbol Type, Sy...
## dbl  (6): Unique ID, Latitude, Longitude, Year Dedicated, Year Removed, Priv...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
heritage.tb <- heritage_tibble %>%
  drop_na(c(Latitude, Longitude))

heritage.sf = st_as_sf(heritage.tb, coords = c("Longitude", "Latitude"), crs = 4326)

#steps 2 - census
census.tb <- as_tibble(read_csv("./data/census.csv"))
## New names:
## * `` -> ...1
## Rows: 8067 Columns: 14── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (1): GISJOIN
## dbl (13): ...1, totalPopulation, freeAfAmPopulation, slavePopulation, year, ...
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#steps 3 - other spatial layer
pop1860 <- st_read("./data/NHGIS_POP1860.shp")
## Reading layer `NHGIS_POP1860' from data source 
##   `/Users/matthewpalagyi/Documents/R Folder/'22 Folder/Lab_Exercises/Lab Assignment 5/data/NHGIS_POP1860.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 2126 features and 25 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -124.7626 ymin: 24.52105 xmax: -66.94983 ymax: 49.38435
## Geodetic CRS:  WGS 84
pop1860_ <- st_make_valid(pop1860)

# Use an inner join from dplyr to link
# call tallys to the polygons...
pop1860_ <- pop1860_ %>%
  mutate(year = as.numeric(DECADE)) %>%
  select(-DECADE)

census.sf <- full_join(census.tb, pop1860_, by = c("GISJOIN" = "GISJOIN"))

#step 3 continued to my liking
census1860.sf_1 <- census.sf %>%
  select(3:14,
         21:22,
         29:39)

census1860.sf_2 <- census1860.sf_1 %>%
  select(-GISJOIN2,
         -COMPOSITE_,
         -year.y)

census1860.sf_3 <- census1860.sf_2 %>%
rename(year = year.x,
       pop1860_total = POP_TOTAL)

census1860.sf_4 <- st_as_sf(census1860.sf_3, crs = 4326)
census1860.sf_5 <- st_make_valid(census1860.sf_4)
census_sf_simple <- st_simplify(census1860.sf_5, preserveTopology = FALSE, dTolerance = 1000)
## Warning in st_simplify.sfc(st_geometry(x), preserveTopology, dTolerance):
## argument preserveTopology cannot be set to FALSE when working with ellipsoidal
## coordinates since the algorithm behind st_simplify always preserves topological
## relationships
tmap_mode("view") 
## tmap mode set to interactive viewing
tm_shape(census_sf_simple, names = "1860 Census") +
  tm_polygons(col = "slavePercentage", palette = "-RdYlBu",
              colorNA = "grey80",
              textNA = "No data available",
              title = "Percent Enslaved") +
  tm_shape(heritage.sf, name = "Confederate Monuments") +
    tm_dots(shape = 21, col = "Tracking Status", 
            colorNA = "grey30",
            popup.vars = c("Name: " = "feature_name", "year dedicated: " = "Year Dedicated")) +
  tm_layout(title = "Confederate Monuments and Enslaved Population 1860", frame = FALSE) +
  tm_view(set.view = c (-77.4360, 37.5407, 6))
## Warning: The shape census_sf_simple contains empty units.

Map Comments

There does not seem to be a correlation between ‘Percent Enslaved’ and the number of monuments. Throughout the greater south, there are numerous monuments among counties that are mixed at best with their percent enslaved. Looking to the northwest, there are greater concentrations of counties with high percentages of enslaved. Yet, they carry little if any monuments as compared to the greater south.

The majority of monuments I browsed through seemed to be erected after the turn of the 20th century (1900-1920).

For the states with the most visible efforts of removing, renaming, or relocating statues, I see Virginia, North Carolina, and the Gulf of Mexico Pan Handle region leading the way in this effort.

Reflection

This was a good experience with R overall. I can’t say I enjoy tmap’s function syntax as much as I do ggplot, but they are interchangeable enough to where I felt it wasn’t that different. Publishing to both Rpubs and Shiny was a little tricky at first (making sure everything is in the same folder, loading all libraries, etc.), but it became a simple enough task in the end.

I am feeling more and more comfortable with R, and I think I am getting close to what I hoped to achieve in this course. We are getting further into statistical analysis and more EDA, which should be a great wrap on these skills and a further boost of my proficiency.