This is intended to be a constantly evolving list of R resources that I have found helpful, informative, and maybe even fun - or that I intend to check out or use in the future. If they’re of benefit to you in any way that is great. But truth be told, this is supposed to be of utility for myself, or at the very least a bit of documentation to see where my interest has been caught.
I came across most of these resources on Mastodon via the creator or a reblogger. Others I picked up from the excellent R Weekly podcast. Yet more are recommendations from my colleagues, or treasures discovered during the bouts of frantic googling that is my job as a technical analyst.
Website of self-described “classic generative art weirdo” Thomas Lin Pedersen. Author of:
{ggfx} - “a (currently experimantal) package that allows the use of various filters and shaders on ggplot2 layers”
{patchwork} - combine separate ggplots and raster images into the same graphic. I plan to use this with a plot of Twitter employee reviews, placing an image of Elon Musk above the point at which the review scores absolutely tank in late October 2022
Amazing-looking workshop by the incredible artist, educator, and general boon to the R community, Danielle Navarro. Originally from rstudio::conf2022, this is an R generative art workshop. Beautifully presented materials. Working my way through as a total genart newb as of late Jan ’23. I hope to blog about my experience of going through the materials!
Geospatial
I’ve made one interactive map and it was mainly done with boilerplate code, so please forgive any imprecision in the wording below!
I found the GB ward boundaries shapefile very useful for work recently. It contains not just the spatial data for drawing polygons on maps, but also fields like LAD codes/names, which can be used as join keys with other public datasets such as those on the DWP’s Stat-Xplore.
This seems to be one of the main geospatial data packages in R. Apparently it’s all about “spatial data simplified”, which I guess is a play on the whole simple features thing. To be honest I could only get it working in the most absolutely simple scenario. I found the docs far, far too daunting for the speed I had to operate for work, and in the end it was (unsurprisingly) google and Stack Overflow that got me sorted. I’m a rank novice with geospatial stuff so my struggle is more a comment on my knowledge than the sf docs themselves.
Edit 2022-02-07 I found this Medium post by Trafford Council’s Trafford Data Lab (GitHub profile) - who do some awesome work - about using the Open Geography Portal API. You can supply an API URL directly to sf::read_sf, which is slick, so I updated my example map below.
Leaflet is an open-source JavaScript library for making interactive maps. And of course, there is an R binding, because people are wonderful 😭. It was surprisingly straightforward once I had my spatial data in. Thankfully there are some excellent examples on the package site, and as I needed to make a choropleth (not that I knew it was called that) for a Shiny app, I just stole the example code and tweaked it for my own purposes!
Total Amount of Benefit per ward, City of Edinburgh (fictional data)
Code
my_ward<-"City of Edinburgh"my_ward_api_formatted<-paste0("'", my_ward, "'")api_url<-URLencode(paste0("https://services1.arcgis.com/ESMARspQHYMw9BZ9/arcgis/rest/services/Wards_December_2022_Boundaries_GB_BGC/FeatureServer/0/query?", "where=LAD22NM=",my_ward_api_formatted,"&‘geometryPrecision=6’&outFields=*&outSR=4326&f=geojson"))ward_shp<-sf::read_sf(api_url)# Random numbers to represent a fictional benefitbenefit<-tibble::tibble( WD22CD =dplyr::pull(dplyr::filter(ward_shp, LAD22NM==my_ward), WD22CD), benefit_value =rnorm(length(WD22CD))*1000)# Ward-specific data for the mapward<-dplyr::filter(ward_shp, LAD22NM==my_ward)|>dplyr::left_join(benefit, by ="WD22CD")# Format helperspal<-leaflet::colorNumeric("Greens", domain =ward$benefit_value)thousands<-scales::label_comma()labels<-sprintf("<strong>%s</strong><br/>%s",ward$WD22NM, thousands(ward$benefit_value))|>lapply(htmltools::HTML)# Map time!leaflet::leaflet()|>leaflet::addTiles()|>leaflet::addPolygons( data =ward, fillColor =~pal(benefit_value), weight =2, opacity =0.9, color ="white", dashArray ="3", fillOpacity =0.9, highlightOptions =leaflet::highlightOptions( weight =3, color ="#666", dashArray ="", fillOpacity =0.9, bringToFront =TRUE), label =labels, labelOptions =leaflet::labelOptions( style =list("font-weight"="normal", padding ="3px 8px"), textsize ="15px", direction ="auto"))|>leaflet::addLegend( pal =pal, values =ward$benefit_value, opacity =0.9, title =NULL, position ="topleft")
A blog post by Yanina Bellini Saibene. I can’t help but wonder if the internal R trainings I delivered for analysts at Policy in Practice would have benefited from a fully-fledged participatory approach. A lot to learn from this compendium of well-presented tips based on collected and personal experience, plus guidelines from several organisations.