Background

  • Talk is available as an R package on github (or contact the author).

Motivation - why do we plot data?

  • Summarise
  • Explore
  • Discover

Anscombes quartet

data(anscombe)
df <- gather(anscombe) %>%
    mutate(dataset = gsub("[xy](\\d)", "\\1", key)) %>%
    mutate(key = gsub("\\d", "", key)) %>%
    group_by(key) %>% 
    mutate(id = 1:n()) %>% 
    spread(key, value) %>% 
    select(-id)

lms <- df %>% 
    group_by(dataset) %>% 
    summarise(
        intercept = broom::tidy(lm(y ~ x))[1, "estimate"][[1]],
        slope = broom::tidy(lm(y ~ x))[2, "estimate"][[1]],
        x = min(df$x),
        xend = max(df$x),
        y = (x * slope) + intercept,
        yend = (xend * slope) + intercept
    )


ax <- list(
    title = "",
    zeroline = FALSE,
    showline = FALSE,
    showticklabels = FALSE,
    showgrid = FALSE
)

plot_ly(df, 
        x = ~x, 
        y = ~y, 
        frame = ~dataset, 
        mode = "markers",
        type = "scatter",
        showlegend = FALSE) %>%
    add_segments(
        data = lms,
        inherit = FALSE,
        x = ~x,
        xend = ~xend,
        y = ~y,
        yend = ~yend,
        frame = ~dataset
    ) %>%
    layout(xaxis = ax, yaxis = ax) %>%
    animation_opts(frame = 2500, transition = 500)

Dirty Dozen

datasaurus_dozen$dataset %<>% factor(
    levels = c(
        "away", 
        "high_lines", 
        "wide_lines",
        "h_lines", 
        "v_lines",
        "slant_down", 
        "slant_up", 
        "dots",
        "bullseye",
        "circle",
        "star", 
        "x_shape",
        "dino")
    )

plot_ly(datasaurus_dozen, 
    x = ~x, 
    y = ~y, 
    frame = ~dataset, 
    mode = "markers",
    type = "scatter",
    showlegend = FALSE) %>%
    layout(xaxis = ax, yaxis = ax) %>%
    animation_opts(frame = 2500, transition = 500)

What can we do with plotly?

  • ggplotly - convert static ggplot objects to dynamic graphs
  • Scatter plots
  • Barplots
  • Boxplots
  • Line plots (including density plots)
  • Heatmaps
  • Combinations of all of the above!
  • Custom htmlwidgets with useful functionality

Not covered

  • Shiny