::p_load(ggiraph, plotly, patchwork, DT, tidyverse) pacman
Hands-on_Ex03_a
3.1 Learning Object
In this hands-on exercise, we will learn how to create interactive data visualisation by using functions provided by ggiraph
and plotlyr
packages. Beside that, we will learn how to make marginal histograms and bar charts in Tableau.
3.2 Getting Started
First, write a code chunk to check, install and launch the following R packages:
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.
The code chunk below will be used to accomplish the task:
3.3 Importing Data
In this section, Exam_data.csv provided will be used. Using read_csv() of readr package, import Exam_data.csv into R.
<- read_csv("Exam_data.csv") exam_data
3.4 Interactive Data Visualisation - ggiraph methods
ggiraph is a tool that allows you to create dynamic ggplot graphs. This allows you to add tooltips and this package allows the selection of graphical elements when used in shiny applications.
Interactive is made with ggplot geometries that can understand three arguments:
Tooltip: a clolumn 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 used within a shiny application, elements associated with an id (data_id) can be selected and manipulated on client and server sides.
3.4.1 Tooltip effect with tooltip aesthetic
Below shows a typical code chunk to plot an interactive statistical graph by using ggiraph package. The code chunk consists of two parts. First, an ggplot object will be created. Next, girafe() of ggiraph will be used to create an interactive svg object.
#|eval:true
#|echo:true
#create ggplot object
<- ggplot(data=exam_data,
p aes(x = MATHS)) +
geom_dotplot_interactive(
aes(tooltip = ID),
stackgroups = TRUE,
binwidth = 1,
method = "histodot") +
#remove the y-axis to avoid misleading visualization
scale_y_continuous(NULL,
breaks = NULL)
#use girafe() to create an interactive svg object(the tooltips)compatible in html environments.
girafe(
ggobj = p,
width_svg = 6,
height_svg = 6*0.618
)
Notice that two steps are involved. 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.
3.5 Interactivity
By hovering the mouse pointer on an data point of interest, the student’s ID will be displayed.
3.5.1 Displaying multiple information on tooltip
The content of the tooltip can be customised by including a list object as shown in the code chunk below. Creating a new filed called tooltip and it populates text in ID and CLASS fields into newly created field.
c()
: This function in R is used to combine values into a vector.Vectors are often used as building blocks for more complex data structures like data frames, matrices, and lists in R. In this case, it’s combining the result of thepaste0()
function. The$
operator is used to refer to and create or access columns in a data frame.
$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.6 Interactivity
3.6.1 Customising Tooltip style
Code chunk below uses opts_tooltip() of ggiraph to customize tooltip rendering by add css declarations.
<- "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.6.2 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)
3.6.3 Hover effect with data_id aesthetic
Code chunk below shows the second interactive feature of ggiraph, namely data_id.
<- 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.6.4 Styling hover effect
In the code chunk below, css codes are used to change the highlighting 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.6.5 Combining tooltip and hover effect
There are time that we want to combine tooltip and hover effect on the interactive statistical graph as shown in the code chunk below.
<- 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.6.6 Click effect with onclick
onclick
argument of ggiraph provides hotlink interactivity on the web.
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)
3.6.7 Coordinated Multiple Views with ggiraph
Coordinated multiple views methods has been implemented in the data visualisation below.
- scale_x_continuous() and scale_y_continuous() are the default scales for continuous x and y aesthetics.
- The Cartesian coordinate system is the most familiar, and common, type of coordinate system. Usage: coord_cartesian( xlim = NULL, ylim = NULL, expand = TRUE, default = FALSE, clip = “on” )
<- 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;")
) )
Notice that 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 as shown in the example above, 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.
3.7 Interactive Data Visualisation - plotly methods!
There are two ways to create interactive graph by using plotly, they are:
by using plot_ly(), and by using ggplotly()
3.7.1 Creating an interactive scatter plot: plot_ly() method
The tabset below shows an example a basic interactive plot created by using plot_ly().
plot_ly(data = exam_data,
x = ~MATHS,
y = ~ENGLISH)
3.7.2 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)
3.7.3 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)
3.7.4 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.
<- 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))
highlight_key() simply creates an object of class crosstalk::SharedData.
3.8Interactive Data Visualisation - crosstalk methods!
3.8.1 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).
::datatable(exam_data, class= "compact") DT
3.8.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)
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!.