pacman::p_load(ggiraph, plotly, patchwork, DT, tidyverse) Hands-on Exercise 3A: Programming Interactive Data Visualisation with R
Learning Objectives
This exercise will focus on creating interactive data visualisation by using functions provided by:
ggiraph
plotlyr
Getting Started
Installing and importing R packages
In this exercise, the following R packages will be used:
| Packages | Description |
|---|---|
| ggiraph | for making ‘ggplot’ graphics interactive |
| plotly | 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 |
Importing the Data
In this exercise, I will work with a file named Exam_data. The below code uses a function called read_csv() from a package called readr, which is part of a collection of packages known as tidyverse.
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.
ggiraph methods
ggiraph is an htmlwidget and a ggplot2 extension which allows ggplot graphics to be interactive.
Interactive 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.
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.
If it us used within a shiny application, elements associated with an id (data_id) can be selected and manipulated on client and server sides. This article provides more detailed explanations.
Tooltip effect with tooltip aesthetic
Below shows a typical code chunk to plot an interactive statistical graph by using ggiraph package. First, a ggplot object will be created. Next, girafe() of ggiraph will be used to create an interactive svg object, which allows users to hover the mouse pointer on a data point of interest to display the student’s ID.
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
)Displaying multiple information on tooltip
The content of the tooltip can be customised by including a list object.
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
)The first three lines of the above code 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 line 7. By hovering the mouse pointer on an data point of interest, the student’s ID and Class will be displayed.
Customising Tooltip style
The code below uses opts_tooltip() of ggiraph to customize tooltip rendering by adding css declarations.
tooltip_css <- "background-color:white; #<<
font-style:bold; color:black;" #<<
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)) #<<
) The background colour of the tooltip is black and the font colour is white and bold. This link provides more about how to customise ggiraph objects.
Displaying statistics on tooltip
A way to customise tooltip can be seen below. In this example, a function is used to compute 90% confident interval of the mean. The derived statistics are then displayed in the tooltip.
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),
) +
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)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, such as CLASS will be highlighted upon mouse over.
p <- ggplot(data=exam_data,
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 below, css codes are used to change the highlighting effect. Elements associated with a data_id, such as CLASS will be highlighted upon mouse over.
p <- ggplot(data=exam_data,
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;")
)
) Different from the previous example, this one encodes the ccs customisation request directly.
Combining tooltip and hover effect
p <- ggplot(data=exam_data,
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;")
)
) The tooltip will show the CLASS upon hovering the mouse over it, including highlights of elements associated with a data_id, such as CLASS.
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.
exam_data$onclick <- sprintf("window.open(\"%s%s\")",
"https://www.moe.gov.sg/schoolfinder?journey=Primary%20school",
as.character(exam_data$ID))
p <- ggplot(data=exam_data,
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) Note that click actions must be a string column in the dataset containing valid javascript instructions.
Coordinated Multiple Views with ggiraph
p1 <- ggplot(data=exam_data,
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)
p2 <- ggplot(data=exam_data,
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;")
)
) Notable points:
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.
to build coordinated multiple views, the following will be used:
appropriate interactive functions of ggiraph will be used to create the multiple views.
patchwork function of the patchwork package will be used inside the 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 hovering over data points with the mouse.
plotly methods
Plotly’s R graphing library creates interactive web graphics from ggplot2 graphs and/or a custom interface to the JavaScript library plotly.js inspired by the grammar of graphics. Different from other plotly platform, plot.R is free and open source.
The two ways to create interactive graphs by using plotly are:
plot_ly(), and
ggplotly()
Creating an interactive scatter plot: plot_ly() method
The tabset below shows an example of a basic interactive plot created by using plot_ly().
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
plot_ly(data = exam_data,
x = ~MATHS,
y = ~ENGLISH)Working with visual variable: plot_ly() method
The color argument is mapped to a qualitative visual variable (i.e. 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
Interactive:
- Click on the colour symbol at the legend.
plot_ly(data = exam_data,
x = ~ENGLISH,
y = ~MATHS,
color = ~RACE)Creating an interactive scatter plot: ggplotly() method
p <- ggplot(data=exam_data,
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 that needs to be ncluded in the code chunk is ggplotly().
Coordinated Multiple Views with plotly
The creation of a coordinated linked plot by using plotly involves three steps:
highlight_key() of plotly package is used as shared data.
two scatterplots will be created by using ggplot2 functions.
lastly, subplot() of plotly package is used to place them next to each other side-by-side.
Click on a data point of one of the scatterplot and see how the corresponding point on the other scatterplot is selected.
d <- highlight_key(exam_data)
p1 <- ggplot(data=d,
aes(x = MATHS,
y = ENGLISH)) +
geom_point(size=1) +
coord_cartesian(xlim=c(0,100),
ylim=c(0,100))
p2 <- ggplot(data=d,
aes(x = MATHS,
y = SCIENCE)) +
geom_point(size=1) +
coord_cartesian(xlim=c(0,100),
ylim=c(0,100))
subplot(ggplotly(p1),
ggplotly(p2))Highlights:
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).
Interactive Data Table: DT package
A wrapper of the JavaScript Library DataTables
Data objects in R can be rendered as HTML tables using the JavaScript library ‘DataTables’ (typically via R Markdown or Shiny).
DT::datatable(exam_data, class= "compact")Linked brushing: crosstalk method
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.
The code below is used to implement the coordinated brushing shown above.
d <- highlight_key(exam_data)
p <- ggplot(d,
aes(ENGLISH,
MATHS)) +
geom_point(size=1) +
coord_cartesian(xlim=c(0,100),
ylim=c(0,100))
gg <- highlight(ggplotly(p),
"plotly_selected")
crosstalk::bscols(gg,
DT::datatable(d),
widths = 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.
Things to learn from the code chunk:
highlight() is a function of plotly package. It sets a variety of options for brushing (i.e., highlighting) multiple plots. These options are primarily designed for linking multiple plotly graphs, and may not behave as expected when linking plotly to another htmlwidget package via crosstalk. In some cases, other htmlwidgets will respect these options, such as persistent selection in leaflet.
bscols() is a helper function of crosstalk package. It makes it easy to put HTML elements side by side. It can be called directly from the console but is especially designed to work in an R Markdown document. Warning: This will bring in all of Bootstrap!.
References
ggiraph
This link provides online version of the reference guide and several useful articles. Use this link to download the pdf version of the reference guide.
This link provides code example on how ggiraph is used to interactive graphs for Swiss Olympians - the solo specialists.
plotly for R
A collection of plotly R graphs are available via this link.
Carson Sievert (2020) Interactive web-based data visualization with R, plotly, and shiny, Chapman and Hall/CRC is the best resource to learn plotly for R. The online version is available via this link
Plotly R Figure Reference provides a comprehensive discussion of each visual representations.
Plotly R Library Fundamentals is a good place to learn the fundamental features of Plotly’s R API.
Visit this link for a very interesting implementation of gganimate by your senior.