library(flexdashboard)
library(tidyverse)
Registered S3 methods overwritten by 'dbplyr':
method from
print.tbl_lazy
print.tbl_sql
── Attaching packages ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.3.2 ──✔ ggplot2 3.4.0 ✔ purrr 1.0.1
✔ tibble 3.2.1 ✔ dplyr 1.1.1
✔ tidyr 1.3.0 ✔ stringr 1.5.0
✔ readr 2.1.3 ✔ forcats 0.5.2── Conflicts ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
✖ tidyr::extract() masks raster::extract()
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
✖ dplyr::select() masks raster::select()
library(sf)
Linking to GEOS 3.10.2, GDAL 3.4.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(lubridate)
Attaching package: ‘lubridate’
The following objects are masked from ‘package:raster’:
intersect, union
The following objects are masked from ‘package:base’:
date, intersect, setdiff, union
library(jsonlite)
Attaching package: ‘jsonlite’
The following object is masked from ‘package:purrr’:
flatten
library(plotly)
Attaching package: ‘plotly’
The following object is masked from ‘package:ggplot2’:
last_plot
The following object is masked from ‘package:raster’:
select
The following object is masked from ‘package:stats’:
filter
The following object is masked from ‘package:graphics’:
layout
library(hablar)
Attaching package: ‘hablar’
The following object is masked from ‘package:forcats’:
fct
The following object is masked from ‘package:dplyr’:
na_if
The following object is masked from ‘package:tibble’:
num
library(RColorBrewer)
# devtools::install_github("hadley/emo")
library(emo)
library(tmap)
library(rsconnect)
Create an interactive map with the tmap package similar to the example above that draws upon the sample code from last class. This part of the Lab Assignment asks you to manipulate the data provided, generate an interactive map with tmap, and send it to RPubs.
read_csv to import the data, then remove all rows
where the latitude column is missingst_as_sf function to convert the tibble to an
sf object (assume the crs is 4326)whoseheritage_data <- read_csv("Whose Heritage.csv", show_col_types = FALSE)
Warning: One or more parsing issues, call `problems()` on your data frame for details, e.g.:
dat <- vroom(...)
problems(dat)
whoseheritage_data1<- whoseheritage_data %>% tidyr::drop_na(Latitude)
st_as_sf(whoseheritage_data1, coords = c("Latitude", "Longitude"), crs = 4326)
Simple feature collection with 2505 features and 16 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 18.08621 ymin: -163.5006 xmax: 62.27001 ymax: -67.14636
Geodetic CRS: WGS 84
read_csv to import the data, then use
filter to extract rows where the year is 1860census_heritage_data <- read_csv("census.csv", show_col_types = FALSE) %>%
filter(year == 1860)
New names:
st_read to import the “NHGIS_POP1860.shp”
datasetst_make_valid function to ensure the geometry
is correct BEFORE plottingleft_join to link the county and state boundaries
to the 1860 Census datast_simplify to reduce the complexity of the
geometry (shapes) to that it does not take days for the map to
rendermap_heritage <- st_read("NHGIS_POP1860.shp")
Reading layer `NHGIS_POP1860' from data source `/Users/theodoracatrina/Downloads/Lab Assignment 5/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
st_make_valid(map_heritage)
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
First 10 features:
OBJECTID DECADE NHGISNAM NHGISST NHGISCTY ICPSRST ICPSRCTY ICPSRNAM STATENAM ICPSRSTI ICPSRCTYI ICPSRFIP STATE COUNTY PID X_CENTROID
1 2 1860 Wright 190 1970 31 1970 WRIGHT Iowa 31 1970 19197 190 1970 735 184343.19
2 8 1860 Auglaize 390 0110 24 110 AUGLAIZE Ohio 24 110 39011 390 0110 1801 986386.42
3 9 1860 Atchison 290 0050 34 50 ATCHISON Missouri 34 50 29005 290 0050 222 48209.91
4 13 1860 Clark 535 0055 73 110 CLARK Washington Territory 73 110 53011 535 0055 1211 -2030078.67
5 19 1860 Floyd 180 0430 22 430 FLOYD Indiana 22 430 18043 180 0430 1308 872473.78
6 23 1860 Somerset 240 0390 52 350 SOMERSET Maryland 52 350 24039 240 0390 969 1743574.81
7 29 1860 Stoddard 290 2070 34 2070 STODDARD Missouri 34 2070 29207 290 2070 437 535465.50
8 30 1860 Toombs 270 1535 33 1670 WILKIN/TOOMBS Minnesota 33 1670 27167 270 1535 57 -28363.25
9 31 1860 Kent 100 0010 11 10 KENT Delaware 11 10 10001 100 0010 171 1737552.64
10 32 1860 Macomb 260 0990 23 990 MACOMB Michigan 23 990 26099 260 0990 1016 1060996.11
Y_CENTROID GISJOIN GISJOIN2 AREA_SQM SHAPE_LENG COMPOSITE_ POP_TOTAL SHAPE_AREA SHAPE_LEN geometry
1 588278.42 G1901970 1901970 1508755622 1.6459953 18601901970 653 1508755144 155582.24 MULTIPOLYGON (((-93.5002 42...
2 404239.35 G3900110 3900110 1040444339 1.8069121 18603900110 17187 1040443924 169140.61 MULTIPOLYGON (((-84.22284 4...
3 328584.31 G2900050 2900050 1417653055 2.0236797 18602900050 4649 1417652576 186843.70 MULTIPOLYGON (((-95.37525 4...
4 1233183.47 G5350055 5350055 3628508857 2.8143681 18605350055 2384 3628507087 277570.57 MULTIPOLYGON (((-122.2413 4...
5 138205.82 G1800430 1800430 384124870 0.9901099 18601800430 20183 384124711 97161.34 MULTIPOLYGON (((-85.80225 3...
6 267627.05 G2400390 2400390 1431079262 8.6565579 18602400390 24992 1431078618 858450.27 MULTIPOLYGON (((-75.71028 3...
7 -55897.11 G2902070 2902070 2146349046 2.5659661 18602902070 7877 2146349693 248079.27 MULTIPOLYGON (((-89.76442 3...
8 970045.71 G2701535 2701535 2326613620 3.0139871 18602701535 40 2326613051 271686.07 MULTIPOLYGON (((-96.67713 4...
9 365232.81 G1000010 1000010 1548770811 2.1582075 18601000010 27804 1548770166 219894.03 MULTIPOLYGON (((-75.50713 3...
10 654944.87 G2600990 2600990 1250599394 1.9159633 18602600990 22843 1250598939 184406.28 MULTIPOLYGON (((-82.7082 42...
map_heritage_1 <- left_join(map_heritage, census_heritage_data, by = 'GISJOIN')
st_make_valid(map_heritage_1)
Simple feature collection with 2126 features and 38 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -124.7626 ymin: 24.52105 xmax: -66.94983 ymax: 49.38435
Geodetic CRS: WGS 84
First 10 features:
OBJECTID DECADE NHGISNAM NHGISST NHGISCTY ICPSRST ICPSRCTY ICPSRNAM STATENAM ICPSRSTI ICPSRCTYI ICPSRFIP STATE COUNTY PID X_CENTROID
1 2 1860 Wright 190 1970 31 1970 WRIGHT Iowa 31 1970 19197 190 1970 735 184343.19
2 8 1860 Auglaize 390 0110 24 110 AUGLAIZE Ohio 24 110 39011 390 0110 1801 986386.42
3 9 1860 Atchison 290 0050 34 50 ATCHISON Missouri 34 50 29005 290 0050 222 48209.91
4 13 1860 Clark 535 0055 73 110 CLARK Washington Territory 73 110 53011 535 0055 1211 -2030078.67
5 19 1860 Floyd 180 0430 22 430 FLOYD Indiana 22 430 18043 180 0430 1308 872473.78
6 23 1860 Somerset 240 0390 52 350 SOMERSET Maryland 52 350 24039 240 0390 969 1743574.81
7 29 1860 Stoddard 290 2070 34 2070 STODDARD Missouri 34 2070 29207 290 2070 437 535465.50
8 30 1860 Toombs 270 1535 33 1670 WILKIN/TOOMBS Minnesota 33 1670 27167 270 1535 57 -28363.25
9 31 1860 Kent 100 0010 11 10 KENT Delaware 11 10 10001 100 0010 171 1737552.64
10 32 1860 Macomb 260 0990 23 990 MACOMB Michigan 23 990 26099 260 0990 1016 1060996.11
Y_CENTROID GISJOIN GISJOIN2 AREA_SQM SHAPE_LENG COMPOSITE_ POP_TOTAL SHAPE_AREA SHAPE_LEN ...1 totalPopulation freeAfAmPopulation slavePopulation year
1 588278.42 G1901970 1901970 1508755622 1.6459953 18601901970 653 1508755144 155582.24 6590 653 0 0 1860
2 404239.35 G3900110 3900110 1040444339 1.8069121 18603900110 17187 1040443924 169140.61 7368 17187 64 0 1860
3 328584.31 G2900050 2900050 1417653055 2.0236797 18602900050 4649 1417652576 186843.70 7030 4649 12 59 1860
4 1233183.47 G5350055 5350055 3628508857 2.8143681 18605350055 2384 3628507087 277570.57 7993 2384 1 0 1860
5 138205.82 G1800430 1800430 384124870 0.9901099 18601800430 20183 384124711 97161.34 6421 20183 757 0 1860
6 267627.05 G2400390 2400390 1431079262 8.6565579 18602400390 24992 1431078618 858450.27 6823 24992 4571 5089 1860
7 -55897.11 G2902070 2902070 2146349046 2.5659661 18602902070 7877 2146349693 248079.27 7130 7877 3 215 1860
8 970045.71 G2701535 2701535 2326613620 3.0139871 18602701535 40 2326613051 271686.07 6962 40 0 0 1860
9 365232.81 G1000010 1000010 1548770811 2.1582075 18601000010 27804 1548770166 219894.03 6125 27804 7271 203 1860
10 654944.87 G2600990 2600990 1250599394 1.9159633 18602600990 22843 1250598939 184406.28 6876 22843 63 0 1860
freeTotalPopulation slavePercentage freeAfAmPercentage freeTotalPercentage slaveDensity freeAfAmDensity totalDensity freeTotalDensity
1 653 0.00 0.00 100.00 0.00 0.00 1.12 1.12
2 17187 0.00 0.37 100.00 0.00 0.16 42.78 42.78
3 4590 1.27 0.26 98.73 0.11 0.02 8.49 8.39
4 2384 0.00 0.04 100.00 0.00 0.00 1.70 1.70
5 20183 0.00 3.75 100.00 0.00 5.10 136.09 136.09
6 19903 20.36 18.29 79.64 9.21 8.27 45.23 36.02
7 7662 2.73 0.04 97.27 0.26 0.00 9.51 9.25
8 40 0.00 0.00 100.00 0.00 0.00 0.04 0.04
9 27601 0.73 26.15 99.27 0.34 12.16 46.50 46.16
10 22843 0.00 0.28 100.00 0.00 0.13 47.31 47.31
geometry
1 MULTIPOLYGON (((-93.5002 42...
2 MULTIPOLYGON (((-84.22284 4...
3 MULTIPOLYGON (((-95.37525 4...
4 MULTIPOLYGON (((-122.2413 4...
5 MULTIPOLYGON (((-85.80225 3...
6 MULTIPOLYGON (((-75.71028 3...
7 MULTIPOLYGON (((-89.76442 3...
8 MULTIPOLYGON (((-96.67713 4...
9 MULTIPOLYGON (((-75.50713 3...
10 MULTIPOLYGON (((-82.7082 42...
sf_use_s2(FALSE)
Spherical geometry (s2) switched off
st_simplify(map_heritage_1)
Warning: st_simplify does not correctly simplify longitude/latitude data, dTolerance needs to be in decimal degrees
Simple feature collection with 2126 features and 38 fields
Geometry type: GEOMETRY
Dimension: XY
Bounding box: xmin: -124.7626 ymin: 24.52105 xmax: -66.94983 ymax: 49.38435
Geodetic CRS: WGS 84
First 10 features:
OBJECTID DECADE NHGISNAM NHGISST NHGISCTY ICPSRST ICPSRCTY ICPSRNAM STATENAM ICPSRSTI ICPSRCTYI ICPSRFIP STATE COUNTY PID X_CENTROID
1 2 1860 Wright 190 1970 31 1970 WRIGHT Iowa 31 1970 19197 190 1970 735 184343.19
2 8 1860 Auglaize 390 0110 24 110 AUGLAIZE Ohio 24 110 39011 390 0110 1801 986386.42
3 9 1860 Atchison 290 0050 34 50 ATCHISON Missouri 34 50 29005 290 0050 222 48209.91
4 13 1860 Clark 535 0055 73 110 CLARK Washington Territory 73 110 53011 535 0055 1211 -2030078.67
5 19 1860 Floyd 180 0430 22 430 FLOYD Indiana 22 430 18043 180 0430 1308 872473.78
6 23 1860 Somerset 240 0390 52 350 SOMERSET Maryland 52 350 24039 240 0390 969 1743574.81
7 29 1860 Stoddard 290 2070 34 2070 STODDARD Missouri 34 2070 29207 290 2070 437 535465.50
8 30 1860 Toombs 270 1535 33 1670 WILKIN/TOOMBS Minnesota 33 1670 27167 270 1535 57 -28363.25
9 31 1860 Kent 100 0010 11 10 KENT Delaware 11 10 10001 100 0010 171 1737552.64
10 32 1860 Macomb 260 0990 23 990 MACOMB Michigan 23 990 26099 260 0990 1016 1060996.11
Y_CENTROID GISJOIN GISJOIN2 AREA_SQM SHAPE_LENG COMPOSITE_ POP_TOTAL SHAPE_AREA SHAPE_LEN ...1 totalPopulation freeAfAmPopulation slavePopulation year
1 588278.42 G1901970 1901970 1508755622 1.6459953 18601901970 653 1508755144 155582.24 6590 653 0 0 1860
2 404239.35 G3900110 3900110 1040444339 1.8069121 18603900110 17187 1040443924 169140.61 7368 17187 64 0 1860
3 328584.31 G2900050 2900050 1417653055 2.0236797 18602900050 4649 1417652576 186843.70 7030 4649 12 59 1860
4 1233183.47 G5350055 5350055 3628508857 2.8143681 18605350055 2384 3628507087 277570.57 7993 2384 1 0 1860
5 138205.82 G1800430 1800430 384124870 0.9901099 18601800430 20183 384124711 97161.34 6421 20183 757 0 1860
6 267627.05 G2400390 2400390 1431079262 8.6565579 18602400390 24992 1431078618 858450.27 6823 24992 4571 5089 1860
7 -55897.11 G2902070 2902070 2146349046 2.5659661 18602902070 7877 2146349693 248079.27 7130 7877 3 215 1860
8 970045.71 G2701535 2701535 2326613620 3.0139871 18602701535 40 2326613051 271686.07 6962 40 0 0 1860
9 365232.81 G1000010 1000010 1548770811 2.1582075 18601000010 27804 1548770166 219894.03 6125 27804 7271 203 1860
10 654944.87 G2600990 2600990 1250599394 1.9159633 18602600990 22843 1250598939 184406.28 6876 22843 63 0 1860
freeTotalPopulation slavePercentage freeAfAmPercentage freeTotalPercentage slaveDensity freeAfAmDensity totalDensity freeTotalDensity
1 653 0.00 0.00 100.00 0.00 0.00 1.12 1.12
2 17187 0.00 0.37 100.00 0.00 0.16 42.78 42.78
3 4590 1.27 0.26 98.73 0.11 0.02 8.49 8.39
4 2384 0.00 0.04 100.00 0.00 0.00 1.70 1.70
5 20183 0.00 3.75 100.00 0.00 5.10 136.09 136.09
6 19903 20.36 18.29 79.64 9.21 8.27 45.23 36.02
7 7662 2.73 0.04 97.27 0.26 0.00 9.51 9.25
8 40 0.00 0.00 100.00 0.00 0.00 0.04 0.04
9 27601 0.73 26.15 99.27 0.34 12.16 46.50 46.16
10 22843 0.00 0.28 100.00 0.00 0.13 47.31 47.31
geometry
1 POLYGON ((-93.49862 42.9084...
2 POLYGON ((-84.22265 40.6641...
3 POLYGON ((-95.37389 40.5804...
4 POLYGON ((-122.2414 46.2175...
5 POLYGON ((-85.7991 38.39019...
6 MULTIPOLYGON (((-75.70964 3...
7 POLYGON ((-89.76445 37.1220...
8 POLYGON ((-96.65624 46.4567...
9 MULTIPOLYGON (((-75.50527 3...
10 POLYGON ((-82.70533 42.6866...
tm_shape line paired with a
tm_polygons line to visualize the “slavePercentage”
attributetm_shape line paired with a
tm_dots line to visualize the “Tracking Status” attribute
from the points sf objecttmap_mode("view")
tmap mode set to interactive viewing
map1_heritage <- tm_shape(map_heritage_1) + tm_polygons(map_heritage_1)
map1_heritage <- tm_shape(map_heritage_1, name = "slaveDensity") +
tm_polygons(col = "slavePercentage", palette = c("lightyellow", "darkorchid4"),
colorNA = "white",
textNA = "No Data Available",
title = "Percentage",
id = "NAME")
map1_heritage
#combining the data
st_heritage <- tibble(state = state.name) %>%
bind_cols(tibble(abb = state.abb))
map_heritage_2 <- left_join(st_heritage, whoseheritage_data1, by = c("abb" = "State"))
map_heritage_3 <- left_join(map_heritage_2, map_heritage_1, by = c("state" = "STATENAM"))
Warning: Detected an unexpected many-to-many relationship between `x` and `y`.
class(map_heritage_3)
[1] "tbl_df" "tbl" "data.frame"
map_heritage_3 <- map_heritage_3 %>% st_as_sf(.)
class(map_heritage_3)
[1] "sf" "tbl_df" "tbl" "data.frame"
map_heritage_4 <- map_heritage_3[!duplicated(map_heritage_3$Latitude), ]
map_heritage_4<- map_heritage_4 %>% tidyr::drop_na(slaveDensity)
tmap_mode("view")
tmap mode set to interactive viewing
map2_heritage <- tm_shape(map_heritage_4, name = "slaveDensity") + tm_dots(col = "Tracking Status", size = 0.2, shape=19)
map2_heritage
# combining the maps
map_final <- map1_heritage + tm_shape(map_heritage_4, name = "slaveDensity") + tm_dots(col = "Tracking Status", size = 0.2, shape=19, clustering = TRUE)
map_final
In areas that had higher percentage of enslaved populations in the year 1860, is both where the most confederate monuments were established, in the clustering of the map, it shows over 2,000 monuments in the South East United States, and far less in the North East and West. Most of these monuments are live, with few being either renamed or just the pedestal remaining. The most recent monument was dedicated in 2021, but states in the Midwest (i.e., Indiana, Kentucky, etc.) seem to be leading the effort to remove the monuments.