# NOT RUN {
bs <- bootstrapper(3)
bs(data.frame(letter = letters[1:4]))
library(ggplot2)
library(dplyr)
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point() +
geom_smooth(data = bootstrapper(5, Species), aes(group = .draw), se = FALSE) +
facet_wrap(~Species)
# it is important to set grouping correctly for bootstrapping
set.seed(1234)
df <- data.frame(
type = c(rep("A", 100), rep("B", 10), rep("C", 3)),
y = rnorm(113)
)
# incorrect: bootstrapping ungrouped dataset leads to missing category C
ggplot(df, aes(type, y)) +
geom_pointrange(data = bootstrapper(6, seed = 562), stat = "summary") +
facet_wrap(~.draw)
# correct: bootstrapping within groups
ggplot(df, aes(type, y)) +
geom_pointrange(data = bootstrapper(6, type, seed = 562), stat = "summary") +
facet_wrap(~.draw)
# also correct: use grouped data frame
ggplot(group_by(df, type), aes(type, y)) +
geom_pointrange(data = bootstrapper(6, seed = 562), stat = "summary") +
facet_wrap(~.draw)
# }
# NOT RUN {
library(gganimate)
set.seed(69527)
x <- rnorm(15)
data <- data.frame(
x,
y = x + 0.5*rnorm(15)
)
bs <- bootstrapper(9)
p <- ggplot(data, aes(x, y)) +
geom_point(shape = 21, size = 6, fill = "white") +
geom_text(label = "0", hjust = 0.5, vjust = 0.5, size = 10/.pt) +
geom_point(data = bs, aes(group = .row), shape = 21, size = 6, fill = "blue") +
geom_text(
data = bs, aes(label = .copies, group = .row),
hjust = 0.5, vjust = 0.5, size = 10/.pt, color = "white"
) +
geom_smooth(data = bs, method = "lm", se = FALSE) +
ggtitle("Bootstrap demonstration") +
theme_bw()
p + facet_wrap(~.draw)
p + transition_states(.draw, 1, 1) +
enter_fade() + exit_fade()
# }
Run the code above in your browser using DataLab