1

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 thanks to Sandy Muspratt for the reference 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

Community
  • 1
  • 1
joel.wilson
  • 8,243
  • 5
  • 28
  • 48
  • Maybe its in the order of loading and that some packages are loaded within a function so that it only comes into affect after the first time. Try ussing ggplot::... and so on before each function and check if that makes a difference – Huub Hoofs Sep 18 '16 at 21:25
  • Sounds like yet another instance of R-FAQ 7.22: https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-lattice_002ftrellis-graphics-not-work_003f in which case the answer is `print` – IRTFM Sep 18 '16 at 21:28
  • @42- since its a gtable object, print() doesn't work. It will print the details of the grid and coordinates rather than the plot. – joel.wilson Sep 19 '16 at 03:25
  • it's a common issue with grid functions that query sizes: they need a device open, sometimes resulting in unwanted blank pages. In my experience Rstudio's device is also a bit more temperamental, and some complex grid-based plots don't always show up the first time. – baptiste Sep 19 '16 at 04:44
  • @baptiste thank you Sir. Even in the initial stage I thought it was an RStudio bug. however when R code was run through command line again the same issues persisted. So got confused with the cause – joel.wilson Sep 19 '16 at 04:48
  • @baptiste when you say that grid objects need a device open, do you have any remedies to resolve this? Any workarounds? – joel.wilson Sep 19 '16 at 04:51
  • If you can produce a minimal reproducible example and post a clear bug report people (gtable/rstudio/grid) might look into it. – baptiste Sep 19 '16 at 05:10
  • @baptiste I have added a smaller version of the code. Please do suggest your views – joel.wilson Sep 19 '16 at 06:03
  • @Huub Hoofs - so if i add package:: before each call, then in the namespace file i will have to use only import() right? rather than the importFrim() that im using currently to load just the specific functions. I checked that there are no packages being loaded inside thw function. and even if there was any then how could it generate plots when i called from outside the package? – joel.wilson Sep 20 '16 at 16:20

0 Answers0