Minghao’s Data Visualization Archive
  • Hands-on Exercise
    • Hands-on Exercise 1
    • Hands-on Exercise 3
    • Hands-on Exercise 4
    • Hands-on Exercise 8
    • Hands-on Exercise 10
  • In-Class Exercise
    • In-Class 1: Tableau Visualisation
    • In-Class 2: Interactivity in Tableau
    • In-Class 3: Exploring Interactivity in R
    • In-Class 4: Plotly
    • In-Class 5: Visual Multivariate Analysis
    • In-Class 6: Time Series Data Visualization
    • In-Class 7: Geospatial Data Visualization
    • In-Class 8: Network Data Visualization
    • In-Class 9: Information Dashboard Design
    • In-Class 10: Financial Data Visualization
  • Take-home Exercise
    • Take-Home Exercise 1
    • Take-Home Exercise 2
    • Take-Home Exercise 3
    • Take-Home Exercise 4
  • Project: Hotel Data Visualization
  • Home
  • Gallery

On this page

  • Getting Started
    • Import libraries & read sample data
    • Load Data
    • Interactive dotplot
    • Display more info on tooltip, and customize its style
    • Displaying statistics on tooltip

Hands-on Exercise 3

hands-on exercise

Interactive Data Visualisation with R.

Author
Affiliation
Liang Minghao

SMU, MITB

Published

June 20, 2023

Getting Started

Import libraries & read sample data

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

Load Data

exam_data <- read.csv('/Users/minghaooo/Documents/Term 4/ISSS608-Visual Analytics/inclass-1/Exam_data.csv')

Interactive dotplot

The default tootip shows student ID when hovering.

#interactive graph object
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) +
  ggtitle('Basic dotplot of students')

# create container
girafe(
  ggobj = p,
  width_svg = 6,
  height_svg = 6*0.618
)

Display more info on tooltip, and customize its style

Extra information of class name is also added in.

# create new field, concatenate ID and Class
exam_data$tooltip <- c(paste0(     #<<
  "Name = ", exam_data$ID,         #<<
  "\n Class = ", exam_data$CLASS)) #<<

# Customized CSS style
tooltip_css <- "background-color:white; #<<
font-style:bold; color:black;" #<<

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) +
  ggtitle('Shows student number and class name')
girafe(
  ggobj = p,
  width_svg = 8,
  height_svg = 8*0.618,
  options = list(    #<<
    opts_tooltip(    #<<
      css = tooltip_css)
    ) #<<
)
Warning: Use of `exam_data$tooltip` is discouraged.
ℹ Use `tooltip` instead.

Displaying statistics on tooltip

Shows mean with standard error of mean (SEM).

tooltip <- function(y, ymax, accuracy = .01) {   #<<
  mean <- scales::number(y, accuracy = accuracy) #<<
  sem <- scales::number(ymax - y, accuracy = accuracy) #<<
  paste("Mean maths scores:", mean, "+/-", sem) #<<
} #<<

gg_point <- ggplot(data=exam_data, 
                   aes(x = RACE),
) +
  ggtitle('Boxplots with mean and SEM') +
  stat_summary(aes(y = MATHS, 
                   tooltip = after_stat(  #<<
                     tooltip(y, ymax))),  #<<
    fun.data = "mean_se", 
    geom = GeomInteractiveCol,  #<<
    fill = "light blue"
  ) +
  stat_summary(aes(y = MATHS),
    fun.data = mean_se,
    geom = "errorbar", width = 0.2, size = 0.2
  )
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
girafe(ggobj = gg_point,
       width_svg = 8,
       height_svg = 8*0.618)
p <- ggplot(data=exam_data, 
       aes(x = MATHS)) +
  geom_dotplot_interactive(           
    aes(tooltip = tooltip, data_id = CLASS),             #<<
    stackgroups = TRUE,               
    binwidth = 1,                        
    method = "histodot") +               
  scale_y_continuous(NULL,               
                     breaks = NULL) +
  ggtitle('Highlighting students from the same class')
girafe(                                  
  ggobj = p,                             
  width_svg = 6,                         
  height_svg = 6*0.618                      
)