Bigger & Brighter

[This article was first published on R | Quantum Jitter, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In Criminal Goings-on I initially got a sense of the data using a static facet plot. This is a great visualisation tool building on the principle of small multiples. There may come a point though where the sheer volume of small multiples make it harder to “see the wood for the trees”. So let’s explore an alternative strategy.

It’s a bit gloomy there in “Little Wood”. Ideal conditions for criminal goings-on. So we’ll add a splash of colour using the “Fantastic Mr. Fox” pallete. We’ll need at least 9 colours for the 9 types of offences.

theme_set(theme_bw())

(cols <- wes_palette(9, name = "FantasticFox1", type = "continuous"))

The data need a little tidy-up.

crime_df <-
  read_csv(
    "https://data.london.gov.uk/download/recorded_crime_rates/c051c7ec-c3ad-4534-bbfe-6bdfee2ef6bb/crime%20rates.csv",
    col_types = "cfcfdn"
  ) %>%
  clean_names() %>%
  mutate(year = str_extract(year, number_range(1999, 2017)),
         year = as.numeric(year)) %>%
  group_by(year, borough, offences) %>%
  summarise(number_of_offences = sum(number_of_offences)) %>%
  filter(
    offences != "All recorded offences",
    !borough %in% c("England and Wales", "Met Police Area", "Inner London", "Outer London")
  )

This was my original visualisation using ggplot’s facet_wrap.

crime_df %>%
  mutate(borough = str_wrap(borough, 11)) %>%
  ggplot(aes(year, number_of_offences, colour = offences, group = offences)) +
  geom_line() +
  facet_wrap(~borough, scales = "free_y", ncol = 4) +
  labs(
    x = NULL, y = NULL, title = "London Crime by Borough",
    colour = "Offence", caption = "Source: data.gov.uk"
  ) +
  scale_colour_manual(values = cols) +
  guides(colour = guide_legend(nrow = 3)) +
  theme(
    strip.background = element_rect(fill = cols[4]),
    legend.position = "bottom",
    axis.text.x = element_text(angle = 45, hjust = 1)
  )

There are some nice alternatives which allow one to go deeper into the data whilst making the whole experience more consumable and engaging.

Switching facet_wrap for facet_trelliscope is a simple option. Or, I could use trelliscope in combination with the rbokeh or plotly packages. Irrespective of the option chosen, I can more flexibly display the several hundred “small multiple” panels required to go deeper into the crime data.

I’m choosing here to pair trelliscope with rbokeh. This permits me to add some custom cognostics and include additional interactivity. The slope cognostic, for example, enables filtering on the boroughs and types of offences exhibiting the steepest upward or downward trends.

slope <- function(x, y) {
  coef(lm(y ~ x))[2]
}

plot_data <- crime_df %>%
  group_by(borough, offences) %>%
  nest() %>%
  ungroup() %>%
  mutate(
    additional_cogs = map_cog(
      data,
      ~ tibble(
        slope = cog(slope(.x$year, .x$number_of_offences), desc = "Steepness of the trend") %>%
          round(2),
        mean_count = cog(mean(.x$number_of_offences), desc = "Average count"),
        iqr_count = cog(IQR(.x$number_of_offences), desc = "Interquartile range")
      )
    ),
    panel = map_plot(
      data,
      ~ figure(xlab = "Date", ylab = "Count") %>%
        ly_lines(year, number_of_offences, color = cols[5], width = 2, data = .x) %>%
        ly_points(year, number_of_offences,
          size = 10,
          fill_color = cols[9],
          hover = number_of_offences, data = .x
        ) %>%
        theme_plot(
          background_fill_color = cols[2],
          background_fill_alpha = 0.5
        )
    )
  )

I’ll use iframe to display the interactive app on this page. Or, if you prefer to see this full-screen, the self-contained app is served here on this blogdown site.

R Toolbox

Summarising below the packages and functions used in this post enables me to separately create a toolbox visualisation summarising the usage of packages and functions across all posts.

Package Function
base library[8]; c[2]; function[2]; list[2]; sum[2]; as.numeric[1]; conflicts[1]; cumsum[1]; mean[1]; round[1]; search[1]
dplyr mutate[7]; filter[5]; group_by[3]; if_else[3]; tibble[3]; summarise[2]; arrange[1]; as_tibble[1]; desc[1]; select[1]; ungroup[1]
ggplot2 aes[1]; element_rect[1]; element_text[1]; facet_wrap[1]; geom_line[1]; ggplot[1]; guide_legend[1]; guides[1]; labs[1]; scale_colour_manual[1]; theme[1]; theme_bw[1]; theme_set[1]
janitor clean_names[1]
kableExtra kable[1]
lubridate year[1]
purrr map[1]; map2_dfr[1]; possibly[1]; set_names[1]
rbokeh figure[1]; ly_lines[1]; ly_points[1]; theme_plot[1]
readr cols[1]; read_csv[1]; read_lines[1]
rebus literal[4]; lookahead[3]; whole_word[2]; ALPHA[1]; lookbehind[1]; number_range[1]; one_or_more[1]; or[1]
stats coef[1]; IQR[1]; lm[1]
stringr str_detect[3]; str_c[2]; str_remove[2]; str_count[1]; str_extract[1]; str_remove_all[1]; str_wrap[1]
tibble enframe[1]
tidyr tibble[3]; as_tibble[1]; nest[1]; unnest[1]
trelliscopejs cog[3]; map_cog[1]; map_plot[1]; sort_spec[1]; trelliscope[1]
wesanderson wes_palette[1]

To leave a comment for the author, please follow the link and comment on their blog: R | Quantum Jitter.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)