knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

onsr

This package collects common R functions used in the Ottawa Neighbourhood Study's (ONS) data-science work. We use it to speed up our work, but anyone is welcome to use it or learn from it.

Installation

You can install the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("Ottawa-Neighbourhood-Study/onsr")

Example

This is a basic example which shows you how to solve a common problem:

# Load the ONS Gen 2 shapefile and plot it.
library(onsr)
library(tidyverse)

ons_shp <- get_ons_shp()

ons_shp %>%
  ggplot() +
    geom_sf()

And we can also easily locate point data within the ONS neighbourhoods:

library(sf)
library(leaflet)

bike_thefts <- sf::read_sf("https://opendata.arcgis.com/datasets/3620cc7a3b874557bb288d889a4d56c2_0.geojson")

bike_thefts_pts <- get_pts_neighbourhood(pts = bike_thefts,
                                          pgon = ons_shp)


bike_thefts_nbhd <- bike_thefts_pts %>%
  group_by(ONS_ID, Name, Name_FR) %>%
  summarise(total_thefts= n()) %>%
  sf::st_set_geometry(NULL) %>%
  right_join(ons_shp) %>%
  mutate(total_thefts = if_else(is.na(total_thefts), 0, total_thefts)) %>%
  sf::st_as_sf()

bike_thefts_nbhd %>%
  ggplot() +
  geom_sf(aes(fill = total_thefts)) +
  scale_fill_gradient(trans="log",
                      breaks = c(1,10,100, 1000)) +
  labs(fill = "Thefts",
       title = "Total Reported Bicycle Thefts in ONS Neighbourhoods, 2014-2020")


Ottawa-Neighbourhood-Study/onsr documentation built on June 19, 2022, 11:46 a.m.