4
votes

I have a package located here on Github which makes use of ggplot2 and so I import it in my NAMESPACE file and my DESCRIPTION FILE

NAMESPACE:

# Generated by roxygen2 (4.1.0): do not edit by hand

export(HybRIDS)
exportClasses(HybRIDS)
import(Biostrings)
import(ape)
import(ggplot2)
import(grid)
import(gridExtra)
importFrom(png,readPNG)
useDynLib(HybRIDS)

DESCRIPTION:

Package: HybRIDS
Type: Package
Title: Detection and dating of Recombinant Regions in DNA sequence data.
Version: 1.0
Date: 2014-12-18
Author: Ben J. Ward
Maintainer: Ben J. Ward <[email protected]>
Description: An R package for the detection and dating of
    Recombinant Regions in DNA sequence data.
License: GPL-2
Depends: methods
Imports: 
    Rcpp (>= 0.11.0),ggplot2,grid,gridExtra,png,ape,Biostrings
LinkingTo: Rcpp
Suggests: knitr,testthat
VignetteBuilder: knitr

However when I use some of the plotting functionality in the package I get an error:

Error: could not find function "ggplotGrob"

And my trace-back in RStudio takes me to these few lines in my package:

arrangeGrob(bars, legendgrob, widths = c(1, 0.13), ncol = 2) at TripletReference.R#244
6 plotBars(plottingSettings) at TripletReference.R#157
5 x$plotTriplet(plotSettings) at TripletReference.R#361
4 FUN(X[[1L]], ...) 
3 lapply(tripletsToPlot, function(x) x$plotTriplet(plotSettings)) at TripletReference.R#361
2 triplets$plotTriplets(Selections, plottingSettings) at HybRIDSObject.R#253
1 test$plotTriplets() 

That line that uses arrangeGrob at TripletReference.R#244 is quite simple. It takes two inputs, one is a ggplot object generated earlier in the function by the following:

bars <- ggplot(plottingFrame, aes(x = X, y = as.factor(Y))) +
                               geom_raster(aes(fill = colour)) + scale_fill_identity() +
                               xlab("Approximate Base Position") +
                               ylab("Sequence Name") +
                               scale_x_continuous(breaks = c(seq(from = 1, to = plottingSettings$MosaicScale, by = plottingSettings$MosaicScale / 10), plottingSettings$MosaicScale), labels = c(frame$bpX[seq(from = 1, to = plottingSettings$MosaicScale, by = plottingSettings$MosaicScale / 10)], max(frame$bpX))) + 
                               scale_y_discrete(labels = c(ContigNames[3], ContigNames[2], ContigNames[1]))

It's lengthy but there's nothing special about it - it's a fairly normal use of ggplot and some geoms and scales.

The second input is a rasterGrob created from an image:

legendgrob <- rasterGrob(image=legend)

All the arrangeGrob line is supposed to do is take the two things and arrange them in one grob, side by side - a grob with two columns.

Notice I haven't in any of the above, tried to use the function that is not found myself (the ggplotGrob function).

I have checked R's documentation of ggplotGrob with ?ggplotGrob, and whilst my package is loaded, if I do ggplot2::ggplotGrob, the following is returned:

function (x) 
{
    ggplot_gtable(ggplot_build(x))
}
<environment: namespace:ggplot2>

So I know the ggplot2 namespace has been loaded - my question is why is my package throwing this error, even when I have imported ggplot2? and the only Depends ggplot2 has now are stats and methods?

Thanks, Ben W.

1
I think I've found the answer - is this because gridExtra does not import ggplot2 so the arrangeGrob of gridExtra is not finding ggplotGrob of ggplot2? - Ward9250
One potential problem I see is that gridExtra depends on grid, so when you import gridExtra, its' grid-based functionality will break. You'll have to put gridExtra to DEPENDS. - tonytonov

1 Answers

3
votes

Try adding ggplot2 under depends, rather than imports, in your DESCRIPTION file.

I've had a similar issue while using the grid.arrange function from gridExtra, and this solution worked for me.

My, admittedly rudimentary, understanding of the issue is that gridExtra is calling the ggplotGrob function at a point when the ggplot2 library isn't yet loaded.