::p_load(ggiraph, plotly,
pacman patchwork, DT, tidyverse)
Hands-on Exercise 3 - Programming Interactive Data Visualisation with R
Learning Objectives:
- Create interactive data visualisation by using functions provided by ggiraph and plotlyr packages
Getting Started
Installing and loading the required libraries
The following R packages will be used:
ggiraph for making ‘ggplot’ graphics interactive.
plotly, R library for plotting interactive statistical graphs.
DT provides an R interface to the JavaScript library DataTables that 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 ggplot2 graphs into one figure.
Code chunk below will be used to check if these packages have been installed and also will load them into the working R environment.
Importing the Data
The code chunk below imports exam_data.csv into R environment by using read_csv() function of readr package.
readr is a pacakge within tidyverse.
<- read_csv("data/Exam_data.csv") exam_data
exam_data tibble data frame contains:
Year end examination grades of a cohort of primary 3 students from a local school.
There are a total of seven attributes. Four of them are categorical data type and the other three are in continuous data type.
The categorical attributes are: ID, CLASS, GENDER and RACE.
The continuous attributes are: MATHS, ENGLISH and SCIENCE.
Interactive Data Visualisation - ggiraph methods
ggiraph is a html widget and a ggplot2 extension. It allows ggplot graphics to be interactive.
Interactive elements are made possible with ggplot geometries that understands three arguments:
Tooltip: a column of data-sets that contain tooltips to be displayed when the mouse is over elements.
Onclick: a column of data-sets that contain a JavaScript function to be executed when elements are clicked.
Data_id: a column of data-sets that contain an id to be associated with elements.
Tooltip effect with tooltip aesthetic
Below shows a typical code chunk to plot an interactive statistical graph by using ggiraph package.
First, an interactive version of ggplot2 geom (i.e. geom_dotplot_interactive()
) will be used to create the basic graph. Then, girafe()
will be used to generate an svg object to be displayed on an html page.
<- 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
)
Displaying multiple information on tooltip
The first three lines of codes in the code chunk create a new field called tooltip. At the same time, it populates text in ID and CLASS fields into the newly created field. Next, this newly created field is used as tooltip field as shown in the code of line 7. By hovering the mouse pointer on an data point of interest, the student’s ID will be displayed.
$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
)
Customising Tooltop style
Code chunk below uses opts_tooltip()
of ggiraph to customize tooltip rendering by add css declarations.
Notice that the background colour of the tooltip is black and the font colour is white and bold.
<- "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)) #<<
)
For more customisations, refer to Customizing girafe objects
Displaying statistics on tooltip
Code chunk below shows an advanced way to customise tooltip. In this example, a function is used to compute 90% confident interval of the mean. The derived statistics are then displayed in the 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
)
girafe(ggobj = gg_point,
width_svg = 8,
height_svg = 8*0.618)
Hover effect with data_id aesthetic
Code chunk below shows the second interactive feature of ggiraph, namely data_id
.
Elements associated with a data_id (i.e CLASS) will be highlighted upon mouse over.
<- 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
)
The default value of the hover css is hover_css = “fill:orange;”.
Styling hover effect
In the code chunk below, css codes are used to change the highlighting effect.
Elements associated with a data_id (i.e CLASS) will be highlighted upon mouse over.
<- 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;")
) )
Plot 1: Combining tooltip and hover effect
The tooltip and hover effects are combined in the interactive statistical graph in the code chunk below.
Elements associated with a data_id (i.e CLASS) will be highlighted upon mouse over. At the same time, the tooltip will show the CLASS.
<- 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;")
) )
Click effect with onclick
onclick
argument of ggiraph provides hotlink interactivity on the web. Web document link with a data object will be displayed on the web browser upon mouse click.
The code chunk below shown an example of 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)
Click actions must be a string column in the dataset containing valid javascript instructions.
Coordinated Multiple Views with ggiraph
When a data point of one of the dotplot is selected, the corresponding data point ID on the second data visualisation will be highlighted too.
In order to build a coordinated multiple views, the following programming strategy will be used:
Appropriate interactive functions of ggiraph will be used to create the multiple views.
patchwork function of patchwork package will be used inside girafe function to create the interactive coordinated multiple views.
The data_id aesthetic is critical to link observations between plots and the tooltip aesthetic is optional but nice to have when mouse over a point.
<- 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;")
) )
Interactive Data Visualisation - plotly methods!
Plotly’s R graphing library create interactive web graphics from ggplot2 graphs and/or a custom interface to the (MIT-licensed) JavaScript library plotly.js inspired by the grammar of graphics. Different from other plotly platform, plot.R is free and open source.
There are two ways to create interactive graph by using plotly, they are:
by using plot_ly(), and
by using ggplotly()
Creating an interactive scatter plot: plot_ly() method
The code chunk below shows an example a basic interactive plot created by using plot_ly().
plot_ly(data = exam_data,
x = ~MATHS,
y = ~ENGLISH)
Working with visual variable: plot_ly() method
In the code chunk below, color argument is mapped to a qualitative visual variable (i.e. RACE).
plot_ly(data = exam_data,
x = ~ENGLISH,
y = ~MATHS,
color = ~RACE)
Creating an interactive scatter plot: ggplotly() method
The code chunk below plots an interactive scatter plot by using ggplotly().
<- 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)
The only extra line you need to include in the code chunk is ggplotly().
Interactive Data Visualisation - crosstalk methods
Crosstalk is an add-on to the htmlwidgets package. It extends htmlwidgets with a set of classes, functions, and conventions for implementing cross-widget interactions (currently, linked brushing and filtering).
::datatable(exam_data, class= "compact") DT
Linked brushing: crosstalk method
Code chunk below is used to implement coordinated brushing.
<- 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)