Dietmar Offenhuber
March 07, 2021
In our last example, we saw 3 different options how to represent geographic information in R/ggplot:
Using simple features (See https://r-spatial.github.io/sf/index.html) - this is the preferred format
Using spatial objects (See https://edzer.github.io/sp/)
Using spatial information fortified into data frames
Read the output of st_read and examine the resulting data frame.
## 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)
Read the output of st_read and examine the resulting data frame. What differences do you notice to 1)?
## 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
Old approach, not recommended: fortify spatial object, convert it to a data frame. Again, examine the resulting data frame.
## 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)
Again, this is the preferred option if you use libraries such as rgdal
If we load a csv file with spatial coordinates, we can plot it directly in ggplot.
##
## ── 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()
## )
Check how coordinates are defined in the respective data sources.
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))#library(tmap)
tm_shape(bos1) +
tm_polygons() +
tm_shape(shp = cc2) +
tm_dots(col="service_name", size = 0.2, title = "Request") ## tmap mode set to interactive viewing
Rpubs https://rpubs.com - publish directly from Rstudio
Shinyapps https://www.shinyapps.io publish a shiny app directly from Rstudio
Github pages https://pages.github.com static websites hosted directly from github repo
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
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") 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
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.
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.
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).