Skip to content

Instantly share code, notes, and snippets.

@achetverikov
Created October 30, 2019 11:45
Show Gist options
  • Save achetverikov/0b896a7ad7d981e3db5c80e4179a7358 to your computer and use it in GitHub Desktop.
Save achetverikov/0b896a7ad7d981e3db5c80e4179a7358 to your computer and use it in GitHub Desktop.
a function for sequentially presenting ggplot2 layers
seq_presentation <- function(p, name = 'plot_layers_%i.png', res = 320, width = 6.2, height = 6.2){
n_layers <- length(p$layers)
y_range <- ggplot_build(p)$layout$panel_scales_y[[1]]$range$range
for (i in c(n_layers:0)){
if (!is.null(name))
png(file = sprintf(name , i), res = res, width = width*res, height = height*res)
if (i!=0) {
p_copy <- copy(p)
p_copy$layers[n_layers:i] <- NULL
print(p_copy+coord_cartesian(ylim = y_range))
} else print(p+coord_cartesian(ylim = y_range))
if (!is.null(name))
dev.off()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment