Interactive charts





An interactive charts allows the user to perform actions: zooming, hovering a marker to get a tooltip, choosing a variable to display and more. R offers a set of packages called the html widgets: they allow to build interactive dataviz directly from R.

Scatter and bubble plots: use plotly.

The best way to build an interactive bubble chart from R is through the plotly library. If you know how to make a ggplot2 chart, you are 10 seconds away to rendering an interactive version= just call the ggplotly() function, and you’re done.

library(ggplot2)
library(plotly)
library(gapminder)

p <- gapminder %>%
  filter(year==1977) %>%
  ggplot( aes(gdpPercap, lifeExp, size = pop, color=continent)) +
  geom_point() +
  theme_bw()

ggplotly(p)

this chart is interactive: hover, drag, zoom, export and more.





Heatmaps: use plotly, d3heatmap or heatmaply.

Three options exist to build an interactive heatmap from R:

  • plotly: as described above, plotly allows to turn any heatmap made with ggplot2 interactive.

  • d3heatmap: a package that uses the same syntax as the base R heatmap() function to make interactive version.

  • heatmaply: the most flexible option, allowing many different kind of customization. See the code of the chart beside here.
See code Heatmap section




Interactive streamgraph and the streamgraph package

The streamgraph package allows to build interactive streamgraphs. Hover a group to highlight it, get its name and its exact value. It's also the only way to build a streamchart from R.

Streamgraph section Chart code




Entirely customizable charts: ggiraph.

ggiraph is an amazing package when it comes to customizing interactive charts. It provides many great features:

  • JavaScript integration
  • CSS customization
  • Connect graph
  • and much more!
Chart code




interactive area chart with plotly.

Once more, plotly is very handy when it comes to build interactive area chart, since its ggplotly() function turns a ggplot2 version interactive. On the chart beside you can:

  • Zoom by selecting an area of interest
  • Hover the line to get exact time and value
  • Export to png
  • Slide axis
  • Double click to re-initialize.
Saving interactive charts to .png and .html.

It is possible to save an interactive chart to both .html and .png formats. To do so, you have to rely on the htmlwidget and webshot packages respectively. It is then possible to embed your viz using an iframe of an img tag in any webpage.

Note: it is also possible to build the interactive chart in an R markdown document to have it embedded directly.

Read more
# create a dataset:
data <- data_frame(
  from=c("A", "A", "B", "D"),
  to=c("B", "E", "F", "A")
)

# Plot
library(igraph)
p <- simpleNetwork(data, height="100px", width="100px")

# save the widget at .html format
library(htmlwidgets)
saveWidget(p, file="myFile.html")

# Save at .png
library(webshot)
webshot::install_phantomjs()
webshot("paste_your_html_here" , "output.png", delay = 0.2 , cliprect = c(440, 0, 1000, 10))

Interactive maps with leaflet.

.pngme>

Build a stunning interactive map in minute thanks to the leaflet package.

It allows to build any type of maps: background, bubble, choropleth, hexbin and more.





3D charts with rgl

The rgl package is the best tool to work in 3D from R. Here is an illustration: a 3d scatterplot showing the relationship between 3 numerical variables.

Note that rgl automatically builds interactive charts. Zooming and rotating can indeed make the chart more insightful.

Source code 3D section


Trying zooming / rotating →





Interactive chord diagram with chordiag

The chorddiag package is an htmlwidget: it automatically builds interactive charts. On the chart below, hovering a group or a connection will highlight the related flow and give additional information.

Note: The example below comes directly from the chorddiagdocumentation. Thanks to Mattflor for developping such a nice package.

Chord section Chart code





Interactive circle packing with circlepackeR

The circlepackeR library allows to get an interactive circular packing with several levels of hierarchy. Click on a group to zoom on it. The post describes how to use the package from different types of input dataset.

Circular packing section Chart code

Interactive dendrogram with collapsibletree

The collapsibletree package is an htmlwidget: it automatically builds collapsible interactive tree diagram. On the chart below, click a node to reveal the next branch, and zoom in/out if necessary.

Dendrogram section Chart code


Interactive network graph: networkD3

The networkD3 package allows to build interactive network diagrams with R. On the chart below, try to hover a node and drag it to see how it works. You can also scroll to zoom in and out. Visit the corresponding post to see how to use this tool on your dataset.

Get code
Time series: dygraph

The dygraph package offers zooming, hovering, minimaps and much more. Try it on the example below!

Get code Time series section

Related chart types


Ggplot2
Animation
Interactivity
3D
Caveats
Data art