This assignment explores different visualization techniques using tmap function.
rm(list = ls()) # Clears the global environment for a fresh start
cat('\f') # Cleans the console
my_libtrary <- c("sf", "dplyr", "raster", "tmap", "ggplot2", "leaflet")
lapply(my_libtrary, library, character.only = TRUE)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
## Loading required package: sp
##
## Attaching package: 'raster'
## The following object is masked from 'package:dplyr':
##
## select
For this assignment we used the Shelby Census tract shapefile along with our own data for population density, median income and other variables. We looked for NA values.
Shelby <- st_read("Data/Assignment_5/Shelby.shp")
## Reading layer `Shelby' from data source `C:\Rizwan\Education\UofM PhD Work\Seminar in Earth Sciences\R-Spatial\Data\Assignment_5\Shelby.shp' using driver `ESRI Shapefile'
## Simple feature collection with 221 features and 9 fields
## geometry type: POLYGON
## dimension: XY
## bbox: xmin: -90.3103 ymin: 34.99419 xmax: -89.63278 ymax: 35.40948
## geographic CRS: NAD83
Data <- as_tibble(read.csv("Data/Assignment_5/Data.csv"))
cat("Number of NA value in the data table is: ", sum(is.na(Data)), sep = '')
## Number of NA value in the data table is: 0
We explore the data using str function and inspected basic statistics of the data using summary function.
str(Shelby)
## Classes 'sf' and 'data.frame': 221 obs. of 10 variables:
## $ STATEFP : chr "47" "47" "47" "47" ...
## $ COUNTYFP: chr "157" "157" "157" "157" ...
## $ TRACTCE : chr "021312" "021530" "021726" "022024" ...
## $ AFFGEOID: chr "1400000US47157021312" "1400000US47157021530" "1400000US47157021726" "1400000US47157022024" ...
## $ GEOID : chr "47157021312" "47157021530" "47157021726" "47157022024" ...
## $ NAME : chr "213.12" "215.30" "217.26" "220.24" ...
## $ LSAD : chr "CT" "CT" "CT" "CT" ...
## $ ALAND : num 1362074 10068590 1433803 2581685 1056567 ...
## $ AWATER : num 0 0 2132 0 45527 ...
## $ geometry:sfc_POLYGON of length 221; first list element: List of 1
## ..$ : num [1:26, 1:2] -89.8 -89.8 -89.8 -89.8 -89.8 ...
## ..- attr(*, "class")= chr [1:3] "XY" "POLYGON" "sfg"
## - attr(*, "sf_column")= chr "geometry"
## - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA
## ..- attr(*, "names")= chr [1:9] "STATEFP" "COUNTYFP" "TRACTCE" "AFFGEOID" ...
str(Data)
## tibble [221 x 17] (S3: tbl_df/tbl/data.frame)
## $ FID : int [1:221] 0 1 2 3 4 5 6 7 8 9 ...
## $ STATEFP : int [1:221] 47 47 47 47 47 47 47 47 47 47 ...
## $ COUNTYFP : int [1:221] 157 157 157 157 157 157 157 157 157 157 ...
## $ GEO_ID : chr [1:221] "1400000US47157021312" "1400000US47157021530" "1400000US47157021726" "1400000US47157022024" ...
## $ OBJECTID : int [1:221] 1161 1176 1186 1201 1001 1006 1021 1029 1044 1059 ...
## $ ALAND : int [1:221] 1362074 10068590 1433803 2581685 1056567 2037629 1356159 1413184 1418277 1726745 ...
## $ AWATER : int [1:221] 0 0 2132 0 45527 154343 0 0 0 0 ...
## $ Area_sqmil: num [1:221] 0.528 3.886 0.554 0.994 0.421 ...
## $ Tot_Pop : int [1:221] 2108 5396 3486 3429 790 2286 2030 3138 2253 1402 ...
## $ Med_Inc : int [1:221] 54769 129375 26731 49667 14345 14299 45729 56111 20724 27344 ...
## $ Tot_Wht : int [1:221] 1902 4530 1012 167 0 42 882 2417 18 21 ...
## $ Tot_AA : int [1:221] 99 308 2423 3250 762 2244 857 595 2229 1379 ...
## $ Tot_Asian : int [1:221] 97 479 0 0 0 0 110 80 0 1 ...
## $ Pop_Den : num [1:221] 3990 1388 6294 3449 1877 ...
## $ PopDen_Wht: num [1:221] 3600 1166 1827 168 0 ...
## $ PopDen_AA : num [1:221] 187.4 79.3 4375.1 3269 1810.7 ...
## $ PopDen_Asi: num [1:221] 184 123 0 0 0 ...
summary(Data)
## FID STATEFP COUNTYFP GEO_ID OBJECTID
## Min. : 0 Min. :47 Min. :157 Length:221 Min. :1000
## 1st Qu.: 55 1st Qu.:47 1st Qu.:157 Class :character 1st Qu.:1055
## Median :110 Median :47 Median :157 Mode :character Median :1110
## Mean :110 Mean :47 Mean :157 Mean :1110
## 3rd Qu.:165 3rd Qu.:47 3rd Qu.:157 3rd Qu.:1165
## Max. :220 Max. :47 Max. :157 Max. :1220
## ALAND AWATER Area_sqmil Tot_Pop
## Min. : 555457 Min. : 0 Min. : 0.2163 Min. : 0
## 1st Qu.: 1940071 1st Qu.: 0 1st Qu.: 0.7730 1st Qu.: 2625
## Median : 3843270 Median : 0 Median : 1.4855 Median : 3958
## Mean : 8948641 Mean : 251458 Mean : 3.5522 Mean : 4240
## 3rd Qu.: 6099276 3rd Qu.: 8845 3rd Qu.: 2.4992 3rd Qu.: 5426
## Max. :138912108 Max. :32514243 Max. :66.1540 Max. :16377
## Med_Inc Tot_Wht Tot_AA Tot_Asian
## Min. : 0 Min. : 0 Min. : 0 Min. : 0.0
## 1st Qu.: 25296 1st Qu.: 167 1st Qu.: 787 1st Qu.: 0.0
## Median : 39375 Median : 845 Median :1800 Median : 23.0
## Mean : 48137 Mean : 1667 Mean :2270 Mean : 108.3
## 3rd Qu.: 65382 3rd Qu.: 2535 3rd Qu.:3425 3rd Qu.: 97.0
## Max. :165321 Max. :10300 Max. :8736 Max. :2219.0
## Pop_Den PopDen_Wht PopDen_AA PopDen_Asi
## Min. : 0 Min. : 0.0 Min. : 0.0 Min. : 0.00
## 1st Qu.: 1722 1st Qu.: 122.9 1st Qu.: 456.9 1st Qu.: 0.00
## Median : 2993 Median : 432.5 Median :1473.2 Median : 16.44
## Mean : 3039 Mean :1008.1 Mean :1822.5 Mean : 52.86
## 3rd Qu.: 4197 3rd Qu.:1754.7 3rd Qu.:2922.1 3rd Qu.: 72.23
## Max. :11705 Max. :5276.3 Max. :9842.2 Max. :622.92
We merged the shapefile and the data file to combine all the data as attribute of the Shelby County shapefile.
Shelby_merged <- merge(Shelby,Data, by.x = "AFFGEOID", by.y = "GEO_ID")
The below shows the basic plotting with tmap. The tm_shape function plots the polygons stored in the Shelby_merged object. The tm_fill function fills the polygons with uniform color, and the tm_borders function draws the borders of the polygons. To arrange different maps side by side, we used tmap_arrange function.
t1 <- tm_shape(Shelby_merged) +
tm_fill()
t2 <- tm_shape(Shelby_merged) +
tm_borders()
t3 <- tm_shape(Shelby_merged) +
tm_fill() +
tm_borders()
tmap_arrange(t1, t2, t3, ncol = 3, nrow = 1)
The aesthetics of the maps can be changed by changing the properties of the tm_fill and tm_borders functions.
ma1 = tm_shape(Shelby_merged) + tm_fill(col = "red")
ma2 = tm_shape(Shelby_merged) + tm_fill(col = "red", alpha = 0.3)
ma3 = tm_shape(Shelby_merged) + tm_borders(col = "blue")
ma4 = tm_shape(Shelby_merged) + tm_borders(lwd = 3)
ma5 = tm_shape(Shelby_merged) + tm_borders(lty = 2)
ma6 = tm_shape(Shelby_merged) + tm_fill(col = "red", alpha = 0.3) +
tm_borders(col = "blue", lwd = 3, lty = 2)
tmap_arrange(ma1, ma2, ma3, ma4, ma5, ma6)
The legend title can be changed by changing the title properties of the tm_fill function.
legend_title = expression("Area (Miles"^2*")")
tm_shape(Shelby_merged)+
tm_fill(col = "Area_sqmil", title = legend_title)+
tm_borders()
The following shows different color scheme to represent a variable in the map where breaks can be manually set as per the user requirements. The color palette can also be selected by the user.
legend_title = "Median Income"
c1 <- tm_shape(Shelby_merged) + tm_polygons(col = "Med_Inc", title=legend_title)
breaks <- c(0, 10, 15, 20) * 10000
c2 <- tm_shape(Shelby_merged) + tm_polygons(col = "Med_Inc", breaks = breaks, title=legend_title)
c3 <- tm_shape(Shelby_merged) + tm_polygons(col = "Med_Inc", n = 10, title=legend_title)
c4 <- tm_shape(Shelby_merged) + tm_polygons(col = "Med_Inc", palette = "BuGn", title=legend_title)
tmap_arrange(c1, c2, c3, c4)
The style properties of the tm_polygons function can be used to select different style of data representations like jenks, quantile, pretty, etc.
legend_title = "Population Density"
c5 <- tm_shape(Shelby_merged) + tm_polygons(col = "Pop_Den", title=legend_title, style="pretty", palette = "OrRd")
c6 <- tm_shape(Shelby_merged) + tm_polygons(col = "Pop_Den", title=legend_title, style="equal", palette = "BuGn")
c7 <- tm_shape(Shelby_merged) + tm_polygons(col = "Pop_Den", title=legend_title, style="quantile", palette = "YlOrBr")
c8 <- tm_shape(Shelby_merged) + tm_polygons(col = "Pop_Den", title=legend_title, style="jenks", palette = "Blues")
tmap_arrange(c5, c6, c7, c8)
The tm_compass function inserts a north arrow to the map. The position of the map can be selected by the postion argument of the function. A scale bar for the map can be inserted using tm_scale_bar function.
l1 <- tm_shape(Shelby_merged) +
tm_polygons(col = "Med_Inc", style="quantile", palette = "YlOrBr")
l1 +
tm_compass(type = "8star", position = c("left", "bottom")) +
tm_scale_bar(breaks = c(0, 5, 10), text.size = 1)
The tm_layout function helps to change the layout of the map. The code below is an example to use the function with user defined parameters.
l1 +
tm_layout(title = "Median Income",
bg.color = "lightblue", frame.lwd = 2,
legend.outside = TRUE)+
tm_compass(type = "8star", position = c("left", "top"))+
tm_scale_bar(breaks = c(0, 5, 10), text.size = 1, position = c("left", "bottom"))
The tm_style function have some predefined layout styles which can be useful too.
s1 <- l1 + tm_style("bw")
s2 <- l1 + tm_style("classic")
s3 <- l1 + tm_style("cobalt")
s4 <- l1 + tm_style("col_blind")
tmap_arrange(s1, s2, s3, s4)
The maps generated maps can be showed interactively by changing the tmap_mode to view. It will allow users to interactively see the map. The map can be zoomed in or zoomed out, and is layed over a real time basemap.
tmap_mode("view")
## tmap mode set to interactive viewing
l1