pacman::p_load(ggiraph, plotly, gganimate, DT, tidyverse, patchwork)Hands-on Exercise 3
hands-on exercise
Interactive Data Visualisation with R.
Getting Started
Import libraries & read sample data
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
)