Hands-on Exercise 3

Author

Dabbie Neo

Published

April 29, 2023

Modified

June 4, 2023

1. Getting Started

Install and launching R packages

The code chunk below uses p_load() of pacman package to check if packages are installed in the computer. If they are, then they will be launched into R. The R packages installed are:

  • ggiraph, for making ‘ggplot’ graphics interactive.

  • plotly, R library for plotting interactive statistical graphs.

  • gganimate, an ggplot extension for creating animated statistical graphs.

  • DT, provides an R interface to the JavaScript library DataTablesthat create interactive table on html page.

  • tidyverse, a family of modern R packages specially designed to support data science, analysis and communication task including creating static statistical graphs.

  • patchwork, for combining multiple plots into one figure.

  • gifski , converts video frames to GIF animations using pngquant’s fancy features for efficient cross-frame palettes and temporal dithering. It produces animated GIFs that use thousands of colors per frame.

  • gapminder, an excerpt of the data available at Gapminder.org. We just want to use its country_colors scheme.

pacman::p_load(ggiraph, plotly, 
               patchwork, DT, tidyverse)

Importing the data

exam_data <- read_csv("data/Exam_data.csv")
Rows: 322 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (4): ID, CLASS, GENDER, RACE
dbl (3): ENGLISH, MATHS, SCIENCE

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

2. Exercises

2.1 Using ggiraph for interactive data visualisation

ggiraph is an htmlwidget and a ggplot2 extension. It allows ggplot graphics to be interactive. The interactivity is made with ggplot geometries that can understand three arguments:

  • Tooltip: a column of data-sets that contain tooltips to be displayed when the mouse is over elements.

  • Data_id: a column of data-sets that contain an id to be associated with elements.

  • Onclick: a column of data-sets that contain a JavaScript function to be executed when elements are clicked.

If it is used within a shiny application, elements associated with an id (data_id) can be selected and manipulated on client and server sides.

2.1.1 Using tooltip (tooltip effect)

There are two parts of the codes: 1. creating ggplot object, 2. girafe() of ggiraph will be used to create an interactive svg object.

p <- ggplot(data=exam_data, 
       aes(x = MATHS)) +
  #geom_dotplot_interactive still takes argument of original geom_dotplot but with tooltip enabled in aes()
  geom_dotplot_interactive(
    aes(tooltip = ID),
    stackgroups = TRUE, 
    binwidth = 1, 
    method = "histodot") +
  scale_y_continuous(NULL, 
                     breaks = NULL)
girafe(
  ggobj = p,
  width_svg = 6,
  height_svg = 6*0.618
)
Interactivity

By hovering the mouse pointer on an data point of interest, the student’s ID will be displayed.

2.1.2 Displaying multiple information on tooltip

# Create a new field called tooltip
exam_data$tooltip <- c(paste0(     
  "Name = ", exam_data$ID,         
  "\n Class = ", exam_data$CLASS)) 

p <- ggplot(data=exam_data, 
       aes(x = MATHS)) +
  geom_dotplot_interactive(
    aes(tooltip = exam_data$tooltip), 
    stackgroups = TRUE,
    binwidth = 1,
    method = "histodot") +
  scale_y_continuous(NULL,               
                     breaks = NULL)
girafe(
  ggobj = p,
  width_svg = 8,
  height_svg = 8*0.618
)
Interactivity

By hovering the mouse pointer on an data point of interest, the student’s ID and Class will be displayed.

2.1.3 Customising Tooltip style

Using opts_tooltip() of ggiraph to customize tooltip rendering by adding css declarations.

tooltip_css <- "background-color:white; #<<
font-style:bold; color:blue;" #<<

p <- ggplot(data=exam_data, 
       aes(x = MATHS)) +
  geom_dotplot_interactive(              
    aes(tooltip = ID),                   
    stackgroups = TRUE,                  
    binwidth = 1,                        
    method = "histodot") +               
  scale_y_continuous(NULL,               
                     breaks = NULL)
girafe(                                  
  ggobj = p,                             
  width_svg = 6,                         
  height_svg = 6*0.618,
  options = list(    #<<
    opts_tooltip(    #<<
      css = tooltip_css)) #<<
)
Note

Background colour of the tooltip is now white and the font colour is blue and bold

2.1.4 Displaying statistics on tooltip