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.

Show Only ...
Maps - Photos - Videos

A Year with R

A Year with R

A year ago I stumbled upon the R programming language, mostly by accident on YouTube. I wanted a better platform for making graphics and maps and was running up against a lot of limitations in Python with matplotlib. Matplotlib is powerful but it often requires a lot of explicit code to make elegant well thought out graphics.

R has proven to be a very worthwhile skill to learn. While I consider myself to be a fairly experienced Python programmer, R has proven a lot more valuable especially when it comes to making good basic, attractive maps in SVG files. Simply said, R defaults with ggplot just make sense and are attractive. The pipe mechanism in R based around maggitir is fantastic for complicated data wrangling in a single line of code. Pipes are a wonderful thing in Unix and they make a lot of sense for processing data.

R is a werid language to get the hang of at first. It’s not necessarily bad – it’s actually pretty awesome for manipulating data with pipes. But it is different with strange operators and syntax, based around 1 indexing rather than 0 indexing of most C derived languages like Python. But I’ve really gotten the hang of it by doing a lot of reading and watching videos on R and just digging through the commands, reading help files and even the raw R code on objects.

R really excels with automating GIS processes and being a one stop shop from extract, transform, load to render. Interestingly, outside of academia it seems like R doesn’t get the credit it deserves – especially with Census data and tidycensus its a one stop shop from obtaining data to manipulating it to rendering it on a map, often with just a single pipeline of code. It’s pretty neat.

I’m glad I taught myself R and it’s a technology I will probably continue to use daily for exploring my world.

You’ll like arcpullr

If you ever need to do GIS work with R using an remote ArcGIS server … you’ll like arcpullr

With arcpullr you can for example get a shapefile of Schenectady EDs with all of four lines of code, only one if you don’t count loading the libraries. It also can do seamless spatial queries using get_layer_by_polygon(), so you can pull only part of layer your interested in, which is good for large layers like tax maps.

library(arcpullr)
library(tidyverse)
library(sf)

get_spatial_layer('https://spatialags.vhb.com/arcgis/rest/services/29816_SIMS/SIMS/MapServer/54') %>% write_sf('/tmp/schenectady_eds.shp')

MapColoring

MapColoring

The MapColoring package provides functions for assigning colors to polygon maps such that no two adjacent features have the same color, while using a minimal number of colors. If provided more than the minimal required number of colors for a given map, it selects colors in a manner that maximizes color contrasts between adjacent polygons. The package also implements the DSATUR graph coloring algorithm in R.

Spend a good portion of yesterday exploring code for finding peaks in a digital elevation model

Spend a good portion of yesterday exploring code for finding peaks in a digital elevation model. I tried a few different methods, one searching for highest points within the x and y axis, looking for inclines and declines, and picking the points in the middle, but that found significantly more points then desired as it seems there is a lot of small up and downs in the elevation, even if they aren’t the actual summit.

What I ended up settling on was this code, that first finds the highest point in the DEM, then deletes all points within 100 meters in all directions from a copy of the DEM then recurvisely calls the function. This works fairly well, although it is memory intensive after about 10 calls, as you are making a lot of copies of DEM in memory. But it seems to work well, assuming you set a reasonable size for the buffer based on the size of the mountain.

library(tidyverse)
library(terra)
library(units)
library(sf)
rm(list=ls())

dem <- rast('/tmp/merge_5925072950_2_meter.tif')
demdf <- as.data.frame(dem, xy=T) %>%
mutate(Layer_1 = Layer_1 %>% set_units('m') %>% set_units('ft')) %>% drop_units()

dems <- aggregate(dem, fact=4)
demsdf <- as.data.frame(dems, xy=T) %>%
mutate(Layer_1 = Layer_1 %>% set_units('m') %>% set_units('ft')) %>% drop_units()


findPeaks <- function(df, size=100, n=5, peakdf=NA) {
dfp <- df
newPeak <- dfp %>% slice_max(Layer_1)

ifelse(is.na(peakdf), peakdf <- newPeak, peakdf <- rbind(peakdf, newPeak))

dfp <-
dfp %>%
filter( !(between(x, newPeak[1,1]-size, newPeak[1,1]+size) &
between(y, newPeak[1,2]-size, newPeak[1,2]+size)))

if (nrow(peakdf) >= n) {
return(peakdf)
}
else {
findPeaks(dfp, size, n, peakdf)
}
}


findPeaks(demdf, 300, 10) %>%
st_as_sf(coords=c('x', 'y'), crs=26918, agr = "constant") %>%
ggplot() +
geom_raster(data=demsdf, aes(x=x, y=y, fill=Layer_1)) +
geom_sf(color='white') +
ggsflabel::geom_sf_label_repel(aes(label=floor(Layer_1)))