Skip to content

Instantly share code, notes, and snippets.

@clauswilke
Last active May 3, 2018 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clauswilke/fe857758c98796ccbf8e636a38e14f75 to your computer and use it in GitHub Desktop.
Save clauswilke/fe857758c98796ccbf8e636a38e14f75 to your computer and use it in GitHub Desktop.
library(purrr)
library(ggplot2)
# function that does the work
make_partial_plots <- function(p) {
g <- ggplot_build(p)
n <- length(g$plot$layers)
blank_out <- function(i) {
g1 <- g
if (i > n)
return(g1)
for (j in n:i) {
g1$plot$layers[[j]] <- NULL
}
g1
}
map(1:(n + 1), blank_out)
}
# example plot with 2 layers
df <- data.frame(x = 1:3, y = 1:3, label = letters[1:3])
p <- ggplot(df, aes(x, y, label = label)) + geom_point(color = "red") + geom_text()
# make partial plots and display in sequence
l <- make_partial_plots(p)
l[[1]]
l[[2]]
l[[3]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment