suppressPackageStartupMessages({
library("datasauRus")
library("plotly")
library("magrittr")
library("plotlyutils")
library("tidyr")
library("dplyr")
})
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)
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)