include ggmap using knitr
Asked Answered
T

2

5

I am trying to include a map using the ggmap library in knitr:

library(ggmap)
murder = subset(crime, offense='murder')
qmplot(lon, lat, data=murder)

It runs fine outside of knitr, but when I try to run this code chunk in knitr, I get the error:

Error in UseMethod("depth"): no applicable method for 'depth' applied to an object of class "NULL"

And there is no map in the final PDF

I tried wrapping the qmplot line inside of evaluate(...), but that didn't work


Output of library(knitr); sessionInfo()

> library(knitr)
> sessionInfo()
version 3.0.1 (2013-05-16)
Platform: i686-redhat-linux-gnu (32-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5]   LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8      
 [7] LC_PAPER=C                 LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C               
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  utils     datasets  grDevices methods   base     

other attached packages:
[1] knitr_1.2      vimcom_0.9-8   setwidth_1.0-3

loaded via a namespace (and not attached):
[1] digest_0.6.3   evaluate_0.4.3 formatR_0.7    stringr_0.6.2  tools_3.0.1  

I also tried running the same code via Rscript (i.e. not from within VIM) with the following:

Rscript -e "library(knitr); knit('map.Rnw')"

And I get a similar error:

Error in UseMethod("depth") : 
no applicable method for 'depth' applied to an object of class "NULL"
Calls: knit ... grid.draw -> grid.draw.gTableChild -> upViewport -> depth

Using ggmap v 2.3 and ggplot v 0.9.3.1

Thrave answered 1/6, 2013 at 15:22 Comment(2)
I cannot reproduce the problem. Please post library(knitr); sessionInfo().Bors
and ggmap is v2.3? this is weird; I have almost exactly the same environment as you, but I still cannot reproduce the problemBors
T
7

I found the problem: I was using the tikz device to include the map in my final document. That tikz device doesn't seem to work for maps (or at least larger maps).

When I changed the device to pdf or png, and the map was included in my document with no problems.

Thrave answered 25/8, 2013 at 3:34 Comment(1)
Any reasons why tikz isn't working? I'd really like it to.Turnout
C
0

Encountered this error (R3.4.1) when calling a long ggplot command with many additional commands appended over a large dataframe (28x32000)

ggplot(z, aes(x=T,y=A,fill=F), title="D") + geom_bar(aes(fill=F), stat ="identity", position="dodge",  colour="black") + facet_grid(P~T,scales='free_x',space='free_x')
+ theme(strip.text.y = element_text(angle = 0), axis.text.x = element_text(angle = 270)) + guides(col=guide_legend(ncol=1))

separating out the commands solved this, and arguably, improved readability:

z2 <- ggplot(z, aes(x=T,y=A,fill=F), title="D")
z2 <- z2 + geom_bar(aes(fill=F), stat ="identity", position="dodge",  colour="black")
z2 <- z2 + facet_grid(P~T,scales='free_x',space='free_x')
z2 <- z2 + theme(strip.text.y = element_text(angle = 0), axis.text.x = element_text(angle = 270))
z2 <- z2 + guides(col=guide_legend(ncol=1))
z2

Wondering whether this is a more general error related to (size of command)*(size of object)?

EDIT: rediscovered this error on smaller commands to ggplot2 when resizing the plotting window - presumably larger, time-intensive tasks more likely to be interrupted in this way - or again, possibly a memory issue.

Calista answered 3/8, 2017 at 11:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.