4/7/2017

Why be interactive?

Examples on the web

Main tools in R

Make your scatter plots, line plots, bar plots, etc interactive

  • Plotly

  • Highcharts

  • crosstalk

Make D3.js plots in R (no javascript required!)

  • rCharts (D3.js)

  • d3scatter

  • networkD3

Making ggplot2 interactive

ggplot2

library(ggplot2)
g <- ggplot(txhousing, aes(x = date, y = sales, group = city)) +
  geom_line(alpha = 0.4)
g

ggplot2 + plotly

library(plotly)
g <- ggplot(txhousing, aes(x = date, y = sales, group = city)) +
  geom_line(alpha = 0.4) 
ggplotly(g, tooltip = c("city"))

plotly

g <- txhousing %>% 
  # group by city
  group_by(city) %>%
  # initiate a plotly object with date on x and median on y
  plot_ly(x = ~date, y = ~median) %>%
  # add a line plot for all texan cities
  add_lines(name = "Texan Cities", hoverinfo = "none", 
            type = "scatter", mode = "lines", 
            line = list(color = 'rgba(192,192,192,0.4)')) %>%
  # plot separate lines for Dallas and Houston
  add_lines(name = "Houston", 
            data = filter(txhousing, 
                          city %in% c("Dallas", "Houston")),
            hoverinfo = "city",
            line = list(color = c("red", "blue")),
            color = ~city)
g

plotly

plotly: adding a slider

g %>% rangeslider

Linking with Crosstalk

Linking with Crosstalk

library(crosstalk)
# define a shared data object
d <- SharedData$new(mtcars)
# make a scatterplot of disp vs mpg
scatterplot <- plot_ly(d, x = ~mpg, y = ~disp) %>%
  add_markers(color = I("navy"))
# define two subplots: boxplot and scatterplot
subplot(
  # boxplot of disp
  plot_ly(d, y = ~disp) %>% 
    add_boxplot(name = "overall", 
                color = I("navy")),
  # scatterplot of disp vs mpg
  scatterplot, 
  shareY = TRUE, titleX = T) %>% 
  layout(dragmode = "select")

Linking with Crosstalk

Linking with Crosstalk

# make subplots
p <- subplot(
  # histogram (counts) of gear
  plot_ly(d, x = ~factor(gear)) %>% 
    add_histogram(color = I("grey50")),
  # scatterplot of disp vs mpg
  scatterplot, 
  titleX = T
) 
layout(p, barmode = "overlay")

Linking with Crosstalk

Easy D3.js in R

Force networks

library(networkD3)
data(MisLinks, MisNodes)
head(MisLinks, 3)
##   source target value
## 1      1      0     1
## 2      2      0     8
## 3      3      0    10
head(MisNodes, 3)
##              name group size
## 1          Myriel     1   15
## 2        Napoleon     1   20
## 3 Mlle.Baptistine     1   23

Force networks

forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source",
             Target = "target", Value = "value", NodeID = "name",
             Group = "group", opacity = 0.9, Nodesize = 3, 
             linkDistance = 100, fontSize = 20)

Force networks

Chord diagrams

# devtools::install_github("mattflor/chorddiag")
library(chorddiag)
# plot the hair preferences data
hair.preference
##         prefer
## have     black blonde brown  red
##   black  11975   5871  8916 2868
##   blonde  1951  10048  2060 6171
##   brown   8010  16145  8090 8045
##   red     1013    990   940 6907
# make a chord diagram
groupColors <- c("#000000", "#FFDD89", "#957244", "#F26223")
chorddiag(hair.preference, 
          groupColors = groupColors, groupnamePadding = 40)

Chord diagrams

References