Adding points from multiple dataframes to R-leaflet maps

I’ve been getting interested in plotting electoral data onto maps — and as a result have been looking at how combinations of information might explain the results of the 2019 election.

To do so, i have been plotting the booth-by-booth results onto a map, as well as data from other information sources.  This was tricky initially, but once you get the knack it makes a lot of sense (in that Hadley Wickham way).

The key is  moving the data = XYZ bit from inside the leaflet() function to the first argument of the addCircleMarkers() function.

The other trick to note below is that I set the  bins for the colorBin function manually.  This is necessary, as the mid point of the range is not zero.

Building on my earlier single-layer maps example, in this case we have the location of mines and the two-party-preferred election 2019 results by polling booth.

# packages
require(data.table)
require(leaflet)
require(readxl)
twoppReps <- as.data.table(read.csv("https://tallyroom.aec.gov.au/Downloads/HouseTppByPollingPlaceDownload-24310.csv", skip = 1L))
pollingPlaces <- as.data.table(read.csv("https://tallyroom.aec.gov.au/Downloads/GeneralPollingPlacesDownload-24310.csv", skip = 1))
operatingMines <- as.data.table(read_excel("C:/Users/mcoog/Dropbox/blogs/operating_mines.xls"))
# inner join to add lat and longtitude data
twoppReps[pollingPlaces[, list(PollingPlaceID, Latitude, Longitude)],
`:=` (lat = i.Latitude, long = i.Longitude),
on = 'PollingPlaceID']
# part 1 bin the data so that 0 = neutral, -100 = red and +100 = blue
swing_bins <- c(100, 25, 10, 5, 2, 1, 0, 1, 2, 5, 10, 25, 100)
swing_pal <- colorBin(c("#f00505", "#fafafa", "#0520f0"),
bins = swing_bins)
ausMines_booths <- leaflet() %>%
addTiles() %>%
addCircleMarkers(data = twoppReps,
lat = ~lat, lng = ~long, popup = ~PollingPlace,
col = ~swing_pal(Swing),
stroke = FALSE, fillOpacity = 0.3, opacity = 0.3) %>%
addCircleMarkers(data = operatingMines,
lat = ~Latitude, lng = ~Longitude, popup = ~Name,
color = 'black', radius = 7, stroke = FALSE,
opacity = 0.50, fillOpacity = 0.50)
print(ausMines_booths)

view raw
mines_2pp.r
hosted with ❤ by GitHub

Maps with geo-spatial coordinates in R

I’ve been thinking a lot about the recent Australian federal election … and to visualise the data i’ve needed to plot the vote outcomes by booth.

As is so often the case, this is pretty easing using R.

The fantastic data.table package looks after joining the polling place IDs to the longitude and latitude coordinates, and the leaflet package creates great maps.

Here’s an example

Australian Federal Election 2019: New South Wales

NSW Polling booths (the intensity of the red simply reflects the number of polling places)
# how to draw a map — and some geospatial points — using R
require(data.table)
require(leaflet)
# get the data
pollingPlaces <- as.data.table(read.csv("https://tallyroom.aec.gov.au/Downloads/GeneralPollingPlacesDownload-24310.csv", skip = 1))
nswReps <- as.data.table(read.csv("https://tallyroom.aec.gov.au/Downloads/HouseStateFirstPrefsByPollingPlaceDownload-24310-NSW.csv", skip = 1))
# inner join to add lat and longtitude data
nswReps[pollingPlaces[, list(PollingPlaceID, Latitude, Longitude)],
`:=` (lat = i.Latitude, long = i.Longitude),
on = 'PollingPlaceID']
# part 1: out first map
# create a simple map — show all pollling places in NSW
nswVotingPlaces <- leaflet(nswReps) %>%
addTiles() %>%
addCircleMarkers(lat = ~lat, lng = ~long, popup = ~DivisionNm,
col = 'red', stroke = FALSE, fillOpacity = 0.01)
print(nswVotingPlaces)