-
Notifications
You must be signed in to change notification settings - Fork 28
/
print.ggedit.R
60 lines (48 loc) · 1.44 KB
/
print.ggedit.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#' @title Print ggedit objects
#' @description Plots lists of ggplot2 plot objects
#' layout functionality.
#' @param x list of ggplot2 plot objects
#' @param layout matrix, layout of plots like in \code{\link[graphics]{layout}},
#' if NULL then a default layout is used, Default: NULL
#' @param byrow boolean, argument passed to default layout (when layout is NULL), used to
#' transpose the output.
#' @param ... not used
#' @examples
#' p <- as.gglist(pList[1:2])
#' p
#'
#' p1 <- p+geom_hline(aes(yintercept=3))
#' p1
#'
#' print(p1,byrow=TRUE)
#'
#' print(p1,layout = matrix(c(2,2,NA,1),ncol=2))
#'
#' @export
print.ggedit <- function(x, layout=NULL, byrow = FALSE, ...) {
if (!is.null(x$UpdatedPlots)) {
x <- x$UpdatedPlots
}
if (is.null(layout)) {
numPlots <- length(x)
cols <- min(numPlots, 2)
rows <- ceiling(numPlots / cols)
plot_vec <- 1:numPlots
if(cols*rows>numPlots){
plot_vec <- c(plot_vec,rep(NA,(cols*rows)-numPlots))
}
layout <- matrix(plot_vec,ncol=cols,nrow=rows,byrow = byrow)
if(byrow&rows==1)
layout <- matrix(plot_vec,ncol=rows,nrow=cols)
}
grid::grid.newpage()
grid::pushViewport(
grid::viewport(
layout = grid::grid.layout(nrow(layout), ncol(layout))
)
)
for (i in 1:max(layout,na.rm = TRUE)) {
thisdim <- which(layout==i,arr.ind = TRUE)
print(x[[i]], vp = vplayout(thisdim[,1], thisdim[,2]))
}
}