R Programming Language 📍

R is a programming language and free software environment for statistical computing and graphics supported by the R Core Team and the R Foundation for Statistical Computing. It is widely used among statisticians and data miners for developing statistical software and data analysis.

📽️ Videos

How to create a shapefile with median age of buildings by block

How to create a shapefile with median age of buildings by block. Data is from NYSGIS which has been loaded into Postgres database.

library(RPostgreSQL)
library(tidyverse)
library(sf)
library(tigris)

rm(list=ls())
con <- DBI::dbConnect(RPostgres::Postgres(), dbname=’gis’, host=’localhost’,
                      port=5432, user=’postgres’, password=’XXXXXXXX’)

bb <- blocks(‘ny’) %>% st_transform(26918)

str_c(“select yr_blt, st_transform(shape, 26918) as geom FROM nytax_ctr_pt WHERE yr_blt>0”) -> sql
yrblt <- st_read(con, query=sql)
 
bb %>%
    st_intersection(yrblt) %>%
    st_drop_geometry() %>%
    group_by(GEOID20) %>%
    summarize(blockyr = median(yr_blt)) %>%
    inner_join(mb, bb) %>%
    write_sf(‘/tmp/median-block.gpkg’)

Why I’m interested in spatial interpolation in R 🗺

Why I’m interested in spatial interpolation in R 🗺

Lately I’ve been very interested in this topic. Spatial interpolation is the art and science of blending together points to make a smooth gradient between them. I can see many uses for this, from making colorful maps of elections or census data to making oredi with the data as rarely do demographics neatly follow census tract or voter tabulation lines.

And the nice thing about R scripts is they are easily repeatable and can run in the background without blocking other things like can happen with QGIS although that’s less of an issue now as it’s much more threaded these days. 

Map: Empire State Topography

I do not recommend tmap 🗺

I do not recommend tmap 🗺

The authors of tmap wrote a wrapper around ggplot2 to make mapping in R easier for beginners. But the thing is you use a lot of flexibility by using the default settings in tmap, you would gain by learning the ins and outs of ggplot2. The next result is very generic looking maps that are functional but not pretty.

Now I’m not condemning the work that tmap represents but I encourage you to learn more about R and ggplot2 for making maps. You can always cut and paste your favorite settings from script to script or create a custom function with the details you want on your maps. No need to limit yourself to the basic maps produced by tmap in R.