Lab 7: Interactive Graphics


The purpose of this lab is to practise generating interactive data visualisations and to demonstrate the value of interaction.

For this lab, you will interact with plots to change the view of the data. For some questions, you will need to take snapshots of the interactive plot and embed the resulting PNG file in your R Markdown document to show that you have completed the question.

NOTE that you can take a snapshot of your ‘plotly’ plot by clicking the little camera icon at the top of the plot.

This lab is broken into two sections, requiring TWO submissions: a non-shiny part and a shiny part. You must submit .Rmd and .html for the non-shiny part and just a .Rmd for the shiny part.

The Data Set

The data set is a CSV file, nzpolice-proceedings.csv, which was derived from “Dataset 5” of Proceedings (offender demographics) on the policedata.nz web site.

We can read the data into an R data frame with read.csv().

crime <- read.csv("nzpolice-proceedings.csv")
crime$Month <- as.Date(crime$Date)
crime$Year <- as.POSIXlt(crime$Date)$year + 1900
typeCount <- table(crime$ANZSOC.Division)
crime$Type <- factor(crime$ANZSOC.Division,
                     levels=names(typeCount)[order(typeCount)])

For this lab we will drop the year 2014 (for which we only have partial data).

crime <- subset(crime, Year >= 2015)

Some questions will use the “raw” crime data above, with one row per incident, and some questions will use the table-of-counts version of the data below, with one row per combination of crime type and month.

counts <- as.data.frame(table(crime$Type, crime$Month))
names(counts) <- c("Type", "Month", "Freq")
counts$Month <- as.Date(counts$Month)
counts$Abbrev <- counts$Type
levels(counts$Abbrev) <- sub("(.+?)(,|and|With|Offences|Endangering)(.+)",
                             "\\1", levels(counts$Abbrev))

Questions of Interest

For each data visualisation in this Lab, we will be interested in answering the following question:

  • How do the trends over time differ between types of crime? Are all crime types decreasing in frequency? Are there any unusual patterns for any crime types?

For specific data visualisations there may be additional specific questions of interest.

Data Visualisations

Direct interaction with ‘plotly’

For this question, you must use the ‘plotly’ package.

library(plotly)
  1. Write R code to produce an interactive version of the plot below.

    NOTE that the legend labels are the Abbreviated labels.

    NOTE that you need to add the full-length crime type labels to the tooltips in this plot.

    NOTE that this figure is 8 inches wide and 6 inches high.

    Use zoom and pan to inspect the trends in Homicide and Miscellaneous Acts. Embed a snapshot of a zoomed view, which should look something like the one below.

    Use interactions with the legend to select ONLY Public Order Offences and Dangerous or Negligent Acts so that we can see them clearly without other overlapping lines. Embed a snapshot of your final view of the data, which should look something like the one below.

    Use tooltips to identify the exact month when the sudden dip in Dangerous or Negligent Acts occurred.

    Describe at least 4 examples of interesting differences in trends over time for different types of crime, based on the above interactions and your own further explorations.

Direct interaction with ‘plotscaper’

For this question you must use the ‘plotscaper’ package.

library(plotscaper)

For this question we will limit the exploration to just Jan 2021 onwards (to limit the data size).

crimeRecent <- subset(crime, Year >= 2021)
  1. Write R code to produce (linked) interactive bar plots of crime types, age groups, and dates, as shown below.

    Use tooltips to identify the month with the highest crime count.

    Use selection to view the crime counts over time for youth (Age.Lower 15 and below). Can you see any increase in crime counts in the second half of 2022? Embed a snapshot to support your conclusion.

    Use selection to view the distribution of crimes across types for different age groups. Describe at least 1 interesting difference between age groups. Embed snapshots of your plots to support your conclusions.

Indirect interaction with ‘shiny’

Your submission for this part of the lab should be a separate R Markdown document. You will be able to “knit” or rmarkdown::render() the first part of the lab, but you can only “run” or rmarkdown::run() this part of the lab.

  1. Write R code to produce an interactive ‘shiny’ document as shown below.

    NOTE that the two selection menus should control the two highlighted lines in the plot and the two sliders should control the scale ranges on the axes.

    Can you answer the questions of interest from Question 1 using this plot? Is it easier or harder using ‘shiny’ or using ‘plotly’?

The Report

Your submission should consist of a knitted R Markdown document, in HTML format, submitted via Canvas.

Your report should include:

  • A brief description of the data and the question we are trying to answer.
  • For each data visualisation, R code AND a brief text commentary.
  • A brief overall summary.

Don’t forget to also complete the Canvas Quiz!

Marking

Marks will be lost for:

  • Plagiarism.
  • Section of the report is missing.
  • The summary is too short or does not make sense.
  • Significantly poor R (or other) code.
  • Overly verbose code, output, or commentary.

Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.