::p_load(ggiraph, plotly,
pacman patchwork, DT, tidyverse)
Hands-on Exercise 3A - Programming Interactive Data Visualisation with R
3.1. Getting Started
3.2. Importing data
Using the same dataset from Hands-on Exercise 02
<- read_csv("C:/Cindy-2312/ISSS608-VAA/Hands-on_Exercise/Hands-on_Ex02/data/Exam_data_2.csv") exam_data
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.
3.3. Interactive Data Visualisation - ggiraph methods
3.3.1.Tooltip effect with tooltip aesthetic
<- ggplot(data=exam_data,
p 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
)
3.3.2. Displaying multiple information on tooltip
$tooltip <- c(paste0(
exam_data"Name = ", exam_data$ID,
"\n Class = ", exam_data$CLASS))
<- ggplot(data=exam_data,
p 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
)
3.3.3. Customizing tooltip style
<- "background-color:white; #<<
tooltip_css font-style:bold; color:black;" #<<
<- ggplot(data=exam_data,
p 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)) #<<
)
3.3.4. Displaying statistics on tooltip
<- function(y, ymax, accuracy = .01) {
tooltip <- scales::number(y, accuracy = accuracy)
mean <- scales::number(ymax - y, accuracy = accuracy)
sem paste("Mean maths scores:", mean, "+/-", sem)
}
<- ggplot(data=exam_data,
gg_point aes(x = RACE),
+
) 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)
3.3.5. Hover effect with data_id aesthetic
<- ggplot(data=exam_data,
p aes(x = MATHS)) +
geom_dotplot_interactive(
aes(data_id = CLASS),
stackgroups = TRUE,
binwidth = 1,
method = "histodot") +
scale_y_continuous(NULL,
breaks = NULL)
girafe(
ggobj = p,
width_svg = 6,
height_svg = 6*0.618
)
3.3.6. Styling hover effect
<- ggplot(data=exam_data,
p aes(x = MATHS)) +
geom_dotplot_interactive(
aes(data_id = CLASS),
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_hover(css = "fill: #202020;"),
opts_hover_inv(css = "opacity:0.2;")
) )
3.3.7. Combining tooltip and hover effect
<- ggplot(data=exam_data,
p aes(x = MATHS)) +
geom_dotplot_interactive(
aes(tooltip = CLASS,
data_id = CLASS),
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_hover(css = "fill: #202020;"),
opts_hover_inv(css = "opacity:0.2;")
) )
3.3.8. Click effect with onclick
$onclick <- sprintf("window.open(\"%s%s\")",
exam_data"https://www.moe.gov.sg/schoolfinder?journey=Primary%20school",
as.character(exam_data$ID))
<- ggplot(data=exam_data,
p aes(x = MATHS)) +
geom_dotplot_interactive(
aes(onclick = onclick),
stackgroups = TRUE,
binwidth = 1,
method = "histodot") +
scale_y_continuous(NULL,
breaks = NULL)
girafe(
ggobj = p,
width_svg = 6,
height_svg = 6*0.618)
3.3.9. Coordinated Multiple Views with ggiraph
<- ggplot(data=exam_data,
p1 aes(x = MATHS)) +
geom_dotplot_interactive(
aes(data_id = ID),
stackgroups = TRUE,
binwidth = 1,
method = "histodot") +
coord_cartesian(xlim=c(0,100)) +
scale_y_continuous(NULL,
breaks = NULL)
<- ggplot(data=exam_data,
p2 aes(x = ENGLISH)) +
geom_dotplot_interactive(
aes(data_id = ID),
stackgroups = TRUE,
binwidth = 1,
method = "histodot") +
coord_cartesian(xlim=c(0,100)) +
scale_y_continuous(NULL,
breaks = NULL)
girafe(code = print(p1 + p2),
width_svg = 6,
height_svg = 3,
options = list(
opts_hover(css = "fill: #202020;"),
opts_hover_inv(css = "opacity:0.2;")
) )
3.4. Interactive Data Visualisation - plotly methods!
3.4.1. Creating an interactive scatter plot: plot_ly() method
plot_ly(data = exam_data,
x = ~MATHS,
y = ~ENGLISH)
No trace type specified:
Based on info supplied, a 'scatter' trace seems appropriate.
Read more about this trace type -> https://plotly.com/r/reference/#scatter
No scatter mode specifed:
Setting the mode to markers
Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
3.4.2. Working with visual variable: plot_ly() method
plot_ly(data = exam_data,
x = ~ENGLISH,
y = ~MATHS,
color = ~RACE)
No trace type specified:
Based on info supplied, a 'scatter' trace seems appropriate.
Read more about this trace type -> https://plotly.com/r/reference/#scatter
No scatter mode specifed:
Setting the mode to markers
Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
3.4.3. Creating an interactive scatter plot: ggplotly() method
<- ggplot(data=exam_data,
p aes(x = MATHS,
y = ENGLISH)) +
geom_point(size=1) +
coord_cartesian(xlim=c(0,100),
ylim=c(0,100))
ggplotly(p)
3.4.4. Coordinated Multiple Views with plotly
<- highlight_key(exam_data)
d <- ggplot(data=d,
p1 aes(x = MATHS,
y = ENGLISH)) +
geom_point(size=1) +
coord_cartesian(xlim=c(0,100),
ylim=c(0,100))
<- ggplot(data=d,
p2 aes(x = MATHS,
y = SCIENCE)) +
geom_point(size=1) +
coord_cartesian(xlim=c(0,100),
ylim=c(0,100))
subplot(ggplotly(p1),
ggplotly(p2))
3.5. Interactive Data Visualisation - crosstalk methods!
3.5.1. Interactive Data Table: DT package
::datatable(exam_data, class= "compact") DT
3.5.2. Linked brushing: crosstalk method
<- highlight_key(exam_data)
d <- ggplot(d,
p aes(ENGLISH,
+
MATHS)) geom_point(size=1) +
coord_cartesian(xlim=c(0,100),
ylim=c(0,100))
<- highlight(ggplotly(p),
gg "plotly_selected")
::bscols(gg,
crosstalk::datatable(d),
DTwidths = 5)
Setting the `off` event (i.e., 'plotly_deselect') to match the `on` event (i.e., 'plotly_selected'). You can change this default via the `highlight()` function.