Week7 - trees

Dietmar Offenhuber

March 07, 2021

Today’s Agenda

  1. A more in-depth view of mapping GIS data
  2. Review “my city in time and space” concept sketches & mapping exercise
  3. Quick intro to mapping hierarchies

Mapping GIS data, continued

In our last example, we saw 3 different options how to represent geographic information in R/ggplot:

  1. Using simple features (See https://r-spatial.github.io/sf/index.html) - this is the preferred format

  2. Using spatial objects (See https://edzer.github.io/sp/)

  3. Using spatial information fortified into data frames

Ad a. Simple Feature (sf)

Read the output of st_read and examine the resulting data frame.

# library(sf)
bos1 <- st_read("data/BOSnbhds/Bos_neighborhoods_new.shp")
## Reading layer `Bos_neighborhoods_new' from data source `/Users/dietmar/Dropbox/northeastern/visual analytics/material - new/Rcode/data/BOSnbhds/Bos_neighborhoods_new.shp' using driver `ESRI Shapefile'
## Simple feature collection with 26 features and 5 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 739715.3 ymin: 2908294 xmax: 812255.1 ymax: 2970086
## projected CRS:  NAD83 / Massachusetts Mainland (ftUS)
plot(bos1)

Ad b. Spatial Object (sp)

Read the output of st_read and examine the resulting data frame. What differences do you notice to 1)?

# library(rgdal)
bos2 <- readOGR(dsn="data/BOSnbhds", layer="Bos_neighborhoods_new")
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/dietmar/Dropbox/northeastern/visual analytics/material - new/Rcode/data/BOSnbhds", layer: "Bos_neighborhoods_new"
## with 26 features
## It has 5 fields
## Integer64 fields read as strings:  OBJECTID
plot(bos2)

AD c. Data Frame

Old approach, not recommended: fortify spatial object, convert it to a data frame. Again, examine the resulting data frame.

# library(tidyverse)
bos3 <- fortify(bos2,region='OBJECTID')
## Warning in RGEOSUnaryPredFunc(spgeom, byid, "rgeos_isvalid"): Ring Self-
## intersection at or near point 757457.09557977004 2924489.7073805202
## SpP is invalid
## Warning in rgeos::gUnaryUnion(spgeom = SpP, id = IDs): Invalid objects found;
## consider using set_RGEOS_CheckValidity(2L)

Convert sp to sf object

Again, this is the preferred option if you use libraries such as rgdal

bos.sf2 <- st_as_sf(bos2)
ggplot() + geom_sf(data=bos.sf2)

Load point data from csv

If we load a csv file with spatial coordinates, we can plot it directly in ggplot.

cc <- read_csv(file="data/citizensconnect_latlon.csv")
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   address = col_character(),
##   description = col_character(),
##   lat = col_double(),
##   lon = col_double(),
##   media_url = col_character(),
##   requested_datetime = col_character(),
##   service_code = col_character(),
##   service_name = col_character(),
##   service_request_id = col_double(),
##   status = col_character(),
##   status_notes = col_character(),
##   token = col_character(),
##   updated_datetime = col_character()
## )
ggplot() + geom_point(data=cc, aes(x=lon,y=lat, color=service_name))

What is wrong here?

Check how coordinates are defined in the respective data sources.

ggplot() + 
  geom_sf(data = bos1) +
  geom_point(data = cc, aes(x=lon,y=lat))

Handling diverging coordinate systems

Solution: convert all point layers to a simple feature object To convert it to a simple feature, we have to know which coordinate system it uses As a rule of thumb, if you have lat/lon coordinates, use the crs number 4326

Using sf automatically adjusts diverging coordinate systems

cc2 <- st_as_sf(cc, coords = c("lon", "lat"), crs = 4326)

ggplot() +
  geom_sf(data = bos1) +
  geom_sf(data = cc2, aes(color = service_name))

Variant - plot map with tmap library

#library(tmap)

tm_shape(bos1) + 
  tm_polygons() + 
tm_shape(shp = cc2) +
  tm_dots(col="service_name", size = 0.2, title = "Request") 

Switch to view mode (leaflet)

tmap_mode("view") #Use "plot" to go back the opposite
## tmap mode set to interactive viewing
tm_shape(bos1) + 
  tm_polygons() + 
tm_shape(shp = cc2) +
  tm_dots(col="service_name", size = 0.2, title = "Request") 

2. My City in Time and Space

  1. Rpubs https://rpubs.com - publish directly from Rstudio

  2. Shinyapps https://www.shinyapps.io publish a shiny app directly from Rstudio

  3. Github pages https://pages.github.com static websites hosted directly from github repo

3. Quick intro to tree structures

Tree-like data structures are ubiquitous in R, but unfortunately there is no universal way to work with them (as convenient as tabular data structures).

For the following examples, we will use the JHU corona data set and extract a small subset of the US.

For a selection of tree-visualizations, see https://treevis.net

JHU <- read.csv(url("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/03-03-2021.csv"))

us <- JHU %>% 
  filter(Country_Region == "US") %>% 
  filter(Province_State %in% c("Massachusetts", "New Jersey", "Connecticut")) %>% 
  select(FIPS,Admin2,Province_State,Country_Region,Confirmed,Incident_Rate)

head(us)
##   FIPS     Admin2 Province_State Country_Region Confirmed Incident_Rate
## 1 9001  Fairfield    Connecticut             US     81312      8619.659
## 2 9003   Hartford    Connecticut             US     70703      7928.834
## 3 9005 Litchfield    Connecticut             US     11242      6234.023
## 4 9007  Middlesex    Connecticut             US     10513      6472.087
## 5 9009  New Haven    Connecticut             US     72368      8466.500
## 6 9011 New London    Connecticut             US     19350      7296.215

Creating treemaps

using the treemap package

treemap::itreemap() offers an interactive interface for making treemaps.

#library(treemap)

treemap(us, 
        index=c("Country_Region", "Province_State", "Admin2"), 
        vSize="Confirmed", 
        vColor="Incident_Rate", 
        type="value") 

Extract hierarchies with data.tree (1)

The data.tree package offers useful methods to tables with embedded hierarchies to a tree data structure. For this purpose, we need to specify the hierarchical relationship between the columns (here as column pathString).

Note: we use FIPS codes as node identifiers, because the County names (Admin2) are not unique.

#library(data.tree)
us$pathString <- paste(us$Country_Region,us$Province_State,us$FIPS,sep = "/")

head(us)
##   FIPS     Admin2 Province_State Country_Region Confirmed Incident_Rate
## 1 9001  Fairfield    Connecticut             US     81312      8619.659
## 2 9003   Hartford    Connecticut             US     70703      7928.834
## 3 9005 Litchfield    Connecticut             US     11242      6234.023
## 4 9007  Middlesex    Connecticut             US     10513      6472.087
## 5 9009  New Haven    Connecticut             US     72368      8466.500
## 6 9011 New London    Connecticut             US     19350      7296.215
##            pathString
## 1 US/Connecticut/9001
## 2 US/Connecticut/9003
## 3 US/Connecticut/9005
## 4 US/Connecticut/9007
## 5 US/Connecticut/9009
## 6 US/Connecticut/9011

Extract hierarchies with data.tree (2)

as.Node(us) parses the data frame into the data.tree structure.
See https://cran.r-project.org/web/packages/data.tree/vignettes/data.tree.html for details.

us.tree <- as.Node(us)
plot(us.tree)

Plotting with ggplot/ggraph (1)

To use the data.tree object in the grammar of graphics requires another step. We will use two libraries: tidygraph, a tidy wrapper around the igraph library (discussed more in the networks module), and ggraph to plot graphs with ggplot.

# tidygraph uses igraph, and data.tree offers a function to convert the tree into an igraph structure.

us.t <- as.igraph.Node(us.tree, 
                       vertexAttributes = c("Confirmed","Incident_Rate", "Admin2"), 
                       directed = T)

Plotting with ggplot/ggraph (2)

Now we can finally plot the tree structure in ggraph:

ggraph(us.t, layout = "tree", circular=T) + 
  geom_edge_fan(alpha = 0.3) +
  geom_node_point(aes(size=Confirmed,color=Incident_Rate)) +
  geom_node_text(aes(label=Admin2), size=3, repel = T) +       # text labels with repel to avoid overlap 
  theme_graph() +                                              # hide all axes etc.
  xlim(-1.2,1.2) + ylim(-1.2,1.2)                              # add some space around the plot
## Warning: Removed 4 rows containing missing values (geom_point).
## Warning: Removed 4 rows containing missing values (geom_text_repel).

# Continued ..

ggraph(us.t, layout = 'dendrogram') +
  geom_edge_diagonal(alpha = 0.3) +
  geom_node_point(aes(size=Confirmed,color=Incident_Rate)) +
  geom_node_text(aes(label=Admin2), size = 2, hjust = 1, angle=90) +
  ylim(-0.5,2) +
  theme_graph()
## Warning: Removed 4 rows containing missing values (geom_point).
## Warning: Removed 4 rows containing missing values (geom_text).

Continued ..

ggraph(us.t, layout = 'partition') + 
    geom_node_tile(aes(fill = Incident_Rate)) +
    geom_node_text(aes(label=name), size = 2.5, angle=90)