Curtin logo

Introduction to R - Data Literacy

Version 1.8 - July 2024

COPYRIGHT © Curtin University 2024

Workshop 1 - Part 6 - Interactive visualisations

Note: This Leaflet and Map Shapefile data was already loaded in the previous part of this workflow.

load(file = "pc_sf_raw.RData")

Interactive Visualisations

The previous visualisations have no interactive elements so are static.

The most useful visualisations don’t limit the user to a static analysis, they incorporate interactivity, where the visualisation can be changed by the user varying relevant parameters.

The user is not dictated to, instead they are guided and can choose their own analysis journey. Incorporating intuitive elements can allow even more insights.

There is always one key issue which needs to be addressed with data analysis and interactive visualisations - how to share them with those who will use them?

Essentially the humble web browser is the one application we know will most likely be on any device capable of using the visualisations.

RShiny is a very powerful interactive visualisation tool for R used in a web browser, however it requires a separate RShiny server to be running, which not everyone may have the access needed to implement.

Putting aside the issue of the web server, let’s explore some visualisations which function completely within a web browser in a static website environment.

Crosstalk Filters

Crosstalk is an extension package designed to work with htmlwidgets package, both of which allow interactive visualisations in a Notebook environment in a web browser.

Crosstalk is not infinitely configurable, but will provide slider, checkbox and dropdown filters which will work with several of the htmlwidgets visualisation tools, including Plotly/ggplotly, DataTables and Leaflet.

Plotly/ggplotly Visualisation with Crosstalk filters

Plotly for R is an advanced and diverse open source visualisation library, which also includes a function ggplotly() which wraps around a ggplot generated plot and immediately adds useful interactivity and features.

To enable the Crosstalk filters to work with the visualisation, we created a shared data resource, which is the used by the visualisation tools.

Let’s create an intuitive visualisation which plots mean Taxable Income and Private Health participation per Postcode for those Postcodes with a SEIFA percentile/rank less than the chosen percentile value. The x axis has the ‘Postcode’ used as a numerical value.

This is achieved by - Creating a shared resource tax2020_seifa_shared, which is a narrow/long format of the combined tax and seifa data created previously. - use bscols() to format a visualisation with two rows, one for the filter_slider() and one for the plot.

Can you draw any interesting conclusions from the visualisations?

tax2020_seifa <- tax2020_raw %>% 
  filter( State !="Unknown" & State!="Overseas" ) %>% 
  mutate(TaxableIncome_dollarspr = TaxableIncome_dollars/Returns) %>% 
  mutate(PrivateHealth_percentpp = round(PrivateHealth_returns/Returns*100,0)) %>% 
  inner_join( x= ., y = seifa2016_raw, by = "Postcode") %>%
  select(State, Postcode, ieo_percentile, TaxableIncome_dollarspr, PrivateHealth_percentpp ) %>%   
  pivot_longer( cols = -c(Postcode, State, ieo_percentile), 
                names_to = "item", 
                values_to = "value", 
                values_drop_na = TRUE)

tax2020_seifa_shared <- SharedData$new(tax2020_seifa)

plot_tax2020_seifa_shared <- tax2020_seifa_shared  %>% 
  ggplot(aes(x=Postcode, y=value, fill=State)) +
  geom_bar(width = 1.5, stat="identity") + facet_wrap( vars(item), scales="free_y") 

bscols(
  widths=c(12,12),
  filter_slider("ieo_percentile", "SEIFA Index of Education and Occupation", tax2020_seifa_shared, column=~ieo_percentile, step=1, width = "100%"),
  ggplotly(plot_tax2020_seifa_shared))

Previous

Part 5 - Map visualisations

Next

Part 7 - Interactive map visualisations

Curtin logo

Version 1.8 - July 2024

COPYRIGHT © Curtin University 2024