# NOT RUN {
spl <- sampler(3, 2)
spl(data.frame(letter = letters[1:4]))
library(ggplot2)
library(dplyr)
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point(aes(color = Species), alpha = 0.3) +
geom_point(
data = sampler(1, 5, group = Species),
aes(fill = Species),
color = "black", shape = 21
) +
theme_bw()
# it is important to set grouping correctly for sampling
set.seed(1234)
df <- data.frame(
type = c(rep("A", 100), rep("B", 10), rep("C", 3)),
y = rnorm(113)
)
# incorrect: sampling ungrouped dataset leads to missing data
# in some categories
ggplot(df, aes(type, y)) +
geom_pointrange(
data = sampler(6, 3, replace = TRUE, seed = 559),
stat = "summary"
) +
facet_wrap(~.draw)
# correct: sampling within groups
ggplot(df, aes(type, y)) +
geom_pointrange(
data = sampler(6, 3, replace = TRUE, group = type, seed = 559),
stat = "summary"
) +
facet_wrap(~.draw)
# also correct: use grouped data frame
ggplot(group_by(df, type), aes(type, y)) +
geom_pointrange(
data = sampler(6, 3, replace = TRUE, seed = 559),
stat = "summary"
) +
facet_wrap(~.draw)
# }
# NOT RUN {
library(gganimate)
p <- ggplot(iris, aes(Sepal.Length, Species, color = Species)) +
geom_point(color = "grey50", alpha = 0.3, size = 2) +
geom_point(data = sampler(20, 1, group = Species), size = 4) +
scale_color_brewer(type = "qual", palette = 2, guide = "none") +
theme_bw()
p + facet_wrap(~.draw)
p + transition_states(.draw, 1, 2)
# }
Run the code above in your browser using DataLab