Created
October 30, 2019 11:45
-
-
Save achetverikov/0b896a7ad7d981e3db5c80e4179a7358 to your computer and use it in GitHub Desktop.
a function for sequentially presenting ggplot2 layers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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