Introduction

At this file you may find a wide variety of interactive charts and visualizations using RStudio and RMarkdown.

Main packages - htmlwidgets and rCharts

Challenge in learning

Learning approach

  • Follow examples, questions, and issues presented in different venues
  • Stick to few JS libraries which provide many choices of charts and become very familiar with them. Don’t forget that few have restrictions for commercial use.

Few Resources

A sampling of the several htmlwidget packages

Interactive tables DT, formattable,datacomb, rpivotTable, rhandsontable, D3TableFilter, sortableR
Heatmaps d3heatmap
Dimple rcdimple
Tau Charts taucharts
Metrics Graphics metricsgraphics
Bokeh rbokeh

Few more sample packages

streamgraph streamgraph
Scatterplot matrices pairsD3, scatterMatrixD3
Pan and Zoom graphs svgPanZoom
Parallel Coordinate Charts parcoords
Sequences Sunburst sunburstR

Example 1 - Interactive table

library(htmlwidgets)
suppressPackageStartupMessages(library(dplyr))
library(DT)
dt = datatable(iris, 
               caption = 'Table 1: Measurements of three flowers species of iris',
               filter = 'top',
               options = list(pageLength = 5))
dt

Example 2 - Heatmap

head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
if (!require("d3heatmap")) devtools::install_github("rstudio/d3heatmap")
library(d3heatmap)
myheatmap = d3heatmap(mtcars, scale="column")
saveWidget(myheatmap, file = "myheatmap.html", selfcontained = TRUE)

Example 3 - rcdimple: Many graph types available

See examples of different types here.

suppressPackageStartupMessages(library(rcdimple)) # demo(dimple) for tons of examples
if (!('rcdimple' %in% installed.packages())) devtools::install_github("timelyportfolio/rcdimple")
myirisplot = iris%>% dimple(x = "Sepal.Length",
  y = "Sepal.Width",
  groups = "Species",
  type = "bubble", width = 600, height = 380) %>% 
  yAxis(overrideMin = 2) %>%
  add_legend(horizontalAlign = 'left')
myirisplot

Example 4 - Grouped Bar - rcdimple: Many graph types available

gearam = mtcars %>% group_by(gear, am) %>% summarise(count = length(gear))
gearam$am <- ifelse(gearam$am==0, yes = "Auto", no = "Manual")
gearam
gear am count
3 Auto 15
4 Auto 4
4 Manual 8
5 Manual 5
mydimplebarplot = gearam %>% dimple(
    x = c("gear","am"), y = "count",
    groups = "am", type = "bar", width=600, height=390)%>%
   add_legend(horizontalAlign = 'left')
mydimplebarplot

Example 5 - taucharts: Many graph types

if (!("taucharts" %in% installed.packages())) devtools::install_github("hrbrmstr/taucharts")
library(taucharts)
tauirisplot = iris %>% tauchart(height = 500, width = 800)%>% 
  tau_point(x = "Sepal.Length", y = "Sepal.Width", color = "Species")%>%
  tau_tooltip(c("Sepal.Length", "Sepal.Width", "Species"))%>% 
  tau_guide_y(auto_scale = FALSE) %>% 
  tau_guide_x(auto_scale = FALSE) %>% tau_legend()
tauirisplot

Example 6 - taucharts: Many graph types

gearam$gear = as.factor(gearam$gear)
gearbartauplot = tauchart(gearam) %>% tau_bar("gear","count","am") %>%
  tau_tooltip(c("gear", "am", "count")) %>% tau_legend()
gearbartauplot

Example 7 - metricsgraphics: Many graph types available

Scatter and line plots (time-series data), grids/facets of graphs, and connected graphs

if (!("metricsgraphics" %in% installed.packages())) devtools::install_github("hrbrmstr/metricsgraphics")
library(metricsgraphics)
irismetricsplot = iris %>% mjs_plot(x=Sepal.Length, y=Sepal.Width) %>%
  mjs_point(color_accessor = Species, color_type="category")%>%
    mjs_labs(x = "Sepal.Length", y = "Sepal.Width")%>% 
    mjs_axis_y(min_y=2)
irismetricsplot

Example 8 - rbokeh: Many graph types available

http://hafen.github.io/rbokeh/: Bar missing

if (!("rbokeh" %in% installed.packages())) devtools::install_github("bokeh/rbokeh")
library(rbokeh)
bokehirisplot = figure() %>%
  ly_points(Sepal.Length, Sepal.Width, data = iris,
    color = Species,
    hover = list(Sepal.Length, Sepal.Width))
bokehirisplot

Example 9 - Scatter plot matrix pairsD3

if (!("pairsD3" %in% installed.packages())) devtools::install_github("garthtarr/pairsD3")
library(pairsD3)
pairsd3plot=pairsD3(iris[,1:4],group=iris[,5]) # %>% savePairs(file = 'iris.html')
pairsd3plot

Example 10 - Scatter plot matrix scatterMatrixD3

if (!("scatterMatrixD3" %in% installed.packages())) devtools::install_github("jcizel/scatterMatrixD3")
library(scatterMatrixD3)
scattermatplot=scatterMatrix(
   data = iris
)
scattermatplot

Example 11 - streamgraph

if (!require("streamgraph")) devtools::install_github("hrbrmstr/streamgraph")
library(streamgraph)
library(babynames)
head(babynames, 3)
year sex name n prop
1880 F Mary 7065 0.0723836
1880 F Anna 2604 0.0266790
1880 F Emma 2003 0.0205215
babynames %>%
  filter(grepl("^Mar", x = babynames$name)) %>%
  group_by(year, name) %>%
  tally(wt = n) %>%
  streamgraph("name", "n", "year")

Example 12 - svgPanZoom

if (!("svgPanZoom" %in% installed.packages())) devtools::install_github("timelyportfolio/svgPanZoom")
library(svgPanZoom)
library(SVGAnnotation)
library(ggplot2)
g = ggplot(iris, aes(Sepal.Length, Sepal.Width, color=Species)) + geom_point()
mynewggplot = svgPanZoom(svgPlot(show(g)), controlIconsEnabled = T)
mynewggplot

Example 13 - parcoords

if (!("parcoords" %in% installed.packages())) devtools::install_github("timelyportfolio/parcoords")
library(parcoords)
mymtcarsplot = parcoords(mtcars, brushMode = "1D-axes-multi")
mymtcarsplot

Example 14 - sunburstr

if (!("sunburstR" %in% installed.packages())) devtools::install_github("timelyportfolio/sunburstR")
library(sunburstR)
sequences <- read.csv(
  system.file("examples/visit-sequences.csv", package = "sunburstR"),
  header=F,
  stringsAsFactors = FALSE
)
head(sequences)
V1 V2
account-account-account-account-account-account 22781
account-account-account-account-account-end 3311
account-account-account-account-account-home 906
account-account-account-account-account-other 1156
account-account-account-account-account-product 5969
account-account-account-account-account-search 692
sunburst(sequences)

Legend

More packages