Effective InteractiveData Visualization with R

With Rmarkdown, R, DataTables, and plotly

Daniel Jachetta true
06-15-2020

This is the official first blog post of this blog! We will start out with a simple tutorial inspired from Interactive web-based data visualization with R, plotly, and shiny by Carson Sievert Published by CRC press.

In this tutorial, we will use 3 packages:

  1. ggplot2
  2. plotly
  3. DT

let’s load them into R.


library(ggplot2)
library(plotly)
library(DT)

Below is the data visualization we will create with interactive capabilities, accompanied by a dynamic Datatable.

We will be using the mpg dataset for this example, which comes inside ggplot2. The first function we use is the highlight_key() function from plotly, which was written by the book author Carso Sievert. This function creates an object based on a primary key. In this example, we use the entire data frame.


m<-highlight_key(mpg)

Next we create a ggplot2 plot with geom_point(), where we map the cylinder of each car to the color arguement, and geom_smooth for the standard error line.


p<-ggplot(m,aes(displ,hwy))+geom_point(aes(color = cyl)) + geom_smooth(se = TRUE)

Next, highlight() is the function used for the brushing/highlighting multiple plots or points. we used the argument “plotly_selected”.


gg<-highlight(ggplotly(p),"plotly_selected")

Finally we pass the previous 3 lines of code to crosstalk::bscols(), which is using Bootstrap and HTML to align our Datatable. The code below is all we have to write, 4 lines of code give us an interactive plot with brushing, and a dynamic table with sorting and searching.


m<-highlight_key(mpg)
p<-ggplot(m,aes(displ,hwy))+geom_point(aes(color = cyl)) + geom_smooth(se = TRUE)
gg<-highlight(ggplotly(p),"plotly_selected")
crosstalk::bscols(gg,DT::datatable(m))