Embedding interactive charts into R Markdown

The next way is to embed interactive chart types into R Markdown documents by using various R packages that enable us to create interactive charts. Some of these package, we have already been introduced to in Chapter 3, R Lesson I – Graphics System. They are as follows:

Therefore, we will not introduce them again, but will introduce some more packages that enable us to build interactive charts. They are:

Please keep in mind that the interactivity logically only works with the HTML output of R Markdown.

We already know the ggvis package from Chapter 3, R Lesson I: Graphics System. Broadly speaking, ggvis is the successor to the well-known graphic package, ggplot2. The interactivity options of ggvis, which are based on the reactive programming model of the Shiny framework, are also useful for creating interactive R Markdown documents.

Using ggvis for interactive R Markdown documents

If you want to include an interactive ggvis plot within a normal R Markdown file, make sure to include the runtime: shiny argument in the YAML header. To create an interactive R markdown document with ggvis, you need to click on the new file, then on R Markdown..., choose Shiny in the left menu of the new window, and finally, click on OK to create the document. As told before, since ggvis uses the reactive model of Shiny, we need to create an R Markdown document with ggvis this way.

Using ggvis for interactive R Markdown documents

As shown, readers of this R Markdown document can easily adjust the bandwidth, and also, the kernel model. The interactive controls are created with input_. In our example, we used the controls, input_slider() and input_select(). For example, some of the other controls are input_checkbox(), input_numeric(), and so on. These controls have different arguments depending on the type of input. For both controls in our example, we used the label argument, which is just a text label shown next to the controls. Other arguments are ID (a unique identifier for the assigned control) and map (a function that remaps the output).

Since the rCharts package creates JavaScript visualizations, the corresponding R code just needs to be embedded in the normal code chunks. The following code produces an interactive line chart:

It is important to include the argument, results = 'asis', in the plot rendering chunk. Furthermore, you need to add the argument, include_assets = TRUE, to the printing function:

rCharts

The googleVis package uses the Google Charts API, and therefore, its charts can also be easily executed within the code chunks. The following code produces an interactive bar and line chart combination:

Again, it is important to include the argument, results = 'asis', in the chunk that creates the chart.

googleVis

We already introduced the htmlwidgets package that enables R users to create interactive web visualizations. Of course, you can also embed HTMNL widgets in R Markdown documents to make them interactive.

As already known from rCharts and googleVis, the dygraphs package, which was created directly by RStudio, only works with the HTML output of R Markdown.

The following code produces an interactive dygraph with a range selector:

dygraphs

After installing the required htmltools package via CRAN, you need to install the metricsgraphics package from GitHub:

The following code produces a D3 scatterplot of the mtcars dataset variables, wt and mpg, and adds a least square regression line within an R Markdown document. The interactivity is given by the fact that you can hover over the dots to get the exact values:

metricsgraphics