I wrote a function(haven't shared the code) named gg()
. It has 2 ggplots superimposed over each other such that I have 2 y axes - one on right and other on left. something like This has been shared from How to manage the t, b, l, r coordinates of gtable() to plot the secondary y-axis's labels and tick marks properly
The final object that gets printed is of class gtable, grob
because I have used gtable
, grid
packages to accomplish this.
Now coming to the problem:
I loaded this function to a package,and in the namespace file I have imported specifically all the functions from each package like gtable
, grid
etc and entire package of ggplot2
. Now after attaching this package using library()
I call the function gg()
. It creates a blank plot in RStudio and even in the pdf if used pdf()
and dev.off()
the first time I call it. But if I call it again, plots are generated.
When I call the same function independently i.e. not by loading the package but using source()
to load the function file alone and then calling gg()
, it plots even the first time.
I want help regarding what all could be the reasons/causes for the same?
i). Has anyone had any issues similar to this with gtable
objects?
What do you think could be the different possibilities of error/bugs. I agree that code needs to be shared for better understanding. I shall add a small-simpler version of the function along with any other details required very soon.
Edit 1 :- Adding a simple version of gg()
gg <- function(arg1, arg2, ...){
# let p1, p2 be a ggplot object(too big)
xx <- ggplot_build(p1)
yy <- ggplot_build(p2)
g1 <- ggplot_gtable(xx)
g2 <- ggplot_gtable(yy)
func1 <- function(grob){ # this function reverses the grobs
widths <- grob$widths
grob$widths[1] <- widths[3]
grob$widths[3] <- widths[1]
grob$vp[[1]]$layout$widths[1] <- widths[3]
grob$vp[[1]]$layout$widths[3] <- widths[1]
grob$children[[1]]$hjust <- 1 - grob$children[[1]]$hjust
grob$children[[1]]$vjust <- 1 - grob$children[[1]]$vjust
grob$children[[1]]$x <- unit(1, "npc") - grob$children[[1]]$x
return(grob)
}
pp <- c(subset(g1$layout, grepl("panel", g1$layout$name), se = t:r))
g <- gtable_add_grob(g1, g2$grobs[grepl("panel", g1$layout$name)],
pp$t, pp$l, pp$b, pp$l, name = "2ndpanel - ")
# inserts the 2nd y-axis label
index <- which(g2$layout$name == "ylab")
ylab <- g2$grobs[[index]]
ylab <- func1(ylab)
ylab$children[[1]]$rot <- ylab$children[[1]]$rot + 180
g <- gtable_add_cols(g, g2$widths[g2$layout[index, ]$l], pos = max(pp$r))
g <- gtable_add_grob(g,ylab, t = min(pp$t), l = max(pp$r)+1,
b = max(pp$b), r = max(pp$r)+1,
clip = "off", name = "2ndylab")
grid.draw(g)
if (newpage)
grid.newpage() # incase you want to have multiple pages of a pdf
I shall explain the "newpage" argument in the second last line of the code. This is an argument passed by the user telling I want to create another page of plot in the same pdf which is open using pdf() and will be closed after the last plot with dev.off(). In the last plot user will pass newpage = FALSE