Loading the data
This post explores the tidy Tuesday dataset for 2023-03-07, it is concerned with Numbat sightings in Australia. Let's load the data and take a quick look at it:
Show the code
library(tidyverse)
numbats <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-03-07/numbats.csv')
numbats %>%
head(5)# A tibble: 5 × 16
decimalLatitude decimalLongitude eventDate scientificName
<dbl> <dbl> <dttm> <chr>
1 -37.6 146. NA Myrmecobius fasciatus
2 -35.1 150. 2014-06-05 02:00:00 Myrmecobius fasciatus
3 -35 118. NA Myrmecobius fasciatus
4 -34.7 118. NA Myrmecobius fasciatus
5 -34.6 117. NA Myrmecobius fasciatus
# ℹ 12 more variables: taxonConceptID <chr>, recordID <chr>,
# dataResourceName <chr>, year <dbl>, month <chr>, wday <chr>, hour <dbl>,
# day <date>, dryandra <lgl>, prcp <dbl>, tmax <dbl>, tmin <dbl>
The dataset contains geographical information such as the latitude and longitude coordinates. We also have date stamps for when the sighting occurred. Let's use the coordinate data to map the sightings.
Working with geographical data
There are a few packages that enable us to more easily handle geo data. Let's load the packages:
Show the code
library(jsonlite)
library(httr)
library(geojsonio)Now that we have the necessary packages let's load the geo json data for australia, this will be useful when plotting the data:
Show the code
ausgeojson <- GET("https://raw.githubusercontent.com/johan/world.geo.json/master/countries/AUS.geo.json") |>
content() |>
fromJSON(simplifyVector = FALSE) |>
as.json()We can inspect the class of the ausgeojson object by calling the class() function:
Show the code
class(ausgeojson)[1] "json" "geo_json"
This object contains the geo json boundary information for Australia.
Visualise the data
The highcharter library has functionality to create interactive maps. Let's create a map with the australia boundary object that we have previously created:
Show the code
library(highcharter)
ausmap <- highchart(type = "map") |>
hc_add_series(mapData = ausgeojson, showInLegend = FALSE)
ausmapThe map above has the boundary data for Australia. Before overlaying the Numbat sighting information, we need to geo code it such that it is the same class as the ausgeojson object:
Show the code
numbats_geojson <- geojson_json(numbats, lat = "decimalLatitude", lon = "decimalLongitude") # Specify the coordinate data.
class(numbats_geojson)[1] "geofeaturecollection" "geojson" "geo_json"
[4] "json"
Now that our data is geo coded, we can add it to our base map:
Show the code
ausmap %>%
hc_add_series(
data = numbats_geojson,
type = "mappoint",
color = hex_to_rgba("darkred", alpha = 0.3),
dataLabels = list(enabled = TRUE),
name = "Numbats",
tooltip = list(pointFormat = "Sighting")
) %>%
hc_mapNavigation(enabled = TRUE) %>% # add interactive elements
hc_title(text = "Numbats in Australia") %>%
hc_subtitle(text = "Numbat sightings are concentrated in the South West of the country")We can see that the sightings are concentrated in the South West of Australia, play around with the interactive map, try zooming in to the region with a high concentration of Numbat sightings. How would you improve the widget? Would you change the label, perhaps the color of the points?