Created
October 18, 2018 00:47
-
-
Save EmilHvitfeldt/8ccb811bb0aed8e5a8feeabe37d22755 to your computer and use it in GitHub Desktop.
Plot every since ggplot2 plot
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
| library(ggplot2) | |
| directory <- "~/Desktop/dataviz/" | |
| fs::dir_create(directory) | |
| print.ggplot <- function(x, newpage = is.null(vp), vp = NULL, ...) { | |
| set_last_plot(x) | |
| if (newpage) grid::grid.newpage() | |
| # Record dependency on 'ggplot2' on the display list | |
| # (AFTER grid.newpage()) | |
| grDevices::recordGraphics( | |
| requireNamespace("ggplot2", quietly = TRUE), | |
| list(), | |
| getNamespace("ggplot2") | |
| ) | |
| data <- ggplot_build(x) | |
| gtable <- ggplot_gtable(data) | |
| if (is.null(vp)) { | |
| grid::grid.draw(gtable) | |
| } else { | |
| if (is.character(vp)) seekViewport(vp) else pushViewport(vp) | |
| grid::grid.draw(gtable) | |
| upViewport() | |
| } | |
| ggsave(paste0(directory, Sys.time(), ".png"), plot = x) | |
| invisible(x) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment