9

I have the following code:

library(ggmap) 
ggmap(get_map(location=c(lon = 5.218922, lat = 52.342366), zoom =14))

Which by my account should work fine, but I only get a standard ggplot2 image (grey background, etc), with the correct axis, but without the map shown: enter image description here

When I look in my working directory I do find a file called "ggmapTemp" which has the correct map in it. But the map is not shown by ggplot2.

I work with RStudio and have limited rights on my work pc. Could this be the reason that the map is not shown correctly? My version of RStudio is 0.96.316 and R is R-2.15.1.

> sessionInfo()
 R version 2.15.1 (2012-06-22)
 Platform: x86_64-pc-mingw32/x64 (64-bit)

 locale:
 [1] LC_COLLATE=Dutch_Netherlands.1252  LC_CTYPE=Dutch_Netherlands.1252   
 [3] LC_MONETARY=Dutch_Netherlands.1252 LC_NUMERIC=C                      
 [5] LC_TIME=Dutch_Netherlands.1252    

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

 other attached packages:
 [1] ggmap_2.1     ggplot2_0.9.1

 loaded via a namespace (and not attached):
 [1] colorspace_1.1-1   dichromat_1.2-4    digest_0.5.2       grid_2.15.1       
 [5] labeling_0.1       MASS_7.3-18        memoise_0.1        munsell_0.3       
 [9] plyr_1.7.1         png_0.1-4          proto_0.3-9.2      RColorBrewer_1.0-5
 [13] reshape2_1.2.1     RgoogleMaps_1.2.0  rjson_0.2.9        scales_0.2.1      
 [17] stringr_0.6        tools_2.15.1   

Update: My sessionInfo() after answer 1:

sessionInfo()

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

 other attached packages:
 [1] mapproj_1.1-8.3 maps_2.2-6      ggmap_2.1       ggplot2_0.9.1  

 loaded via a namespace (and not attached):
 [1] colorspace_1.1-1    dichromat_1.2-4     digest_0.5.2        grid_2.15.1        
 [5] labeling_0.1        MASS_7.3-20         memoise_0.1         munsell_0.3        
 [9] plyr_1.7.1          png_0.1-4           proto_0.3-9.2       RColorBrewer_1.0-5 
 [13] reshape2_1.2.1      RgoogleMaps_1.2.0.2 rjson_0.2.9         scales_0.2.1       
 [17] stringr_0.6.1       tools_2.15.1  

As requested:

capabilities()
jpeg      png     tiff    tcltk      X11     aqua http/ftp  sockets   libxml     fifo 
TRUE     TRUE     TRUE     TRUE    FALSE    FALSE     TRUE     TRUE     TRUE    FALSE 
cledit    iconv      NLS  profmem    cairo 
TRUE     TRUE     TRUE     TRUE     TRUE 
9
  • 2
    Works for me. Can you add the versions of R and ggmap you are using to your question.
    – Spacedman
    Aug 28, 2012 at 10:33
  • 1
    That code works for me on the R console and RStudio. I get a map of somewhere in Holland just east of Amsterdam.
    – smillig
    Aug 28, 2012 at 10:36
  • 2
    I don't see mapproj there, but I needed it to be able to get the plot. Aug 28, 2012 at 10:53
  • 1
    Can you run the capabilities() function and paste in the output. I reckon you've not got PNG support...
    – Spacedman
    Aug 28, 2012 at 16:04
  • 1
    @Timror I have this error as well. I also assumed that it was because I have limited rights on my work PC as it works for me too outside of the Terminal Server I use at work.
    – dom_oh
    Nov 7, 2012 at 12:03

5 Answers 5

7

On my machine, the list of attached packages is:

other attached packages:
[1] mapproj_1.1-8.3 maps_2.2-6      ggmap_2.1       ggplot2_0.9.1  

This means you probably need mapproj as well as maps to run your code, since these are suggested packages from ggplot to enable correct map projections.

Try:

install.packages(c("mapproj", "maps"))

then rerun your code.

3
  • 2
    Actually, I'm surprised the OP doesn't get an error without mapproj installed. I didn't have it installed and it mentioned that mapproj was required to do this, but it worked fine after installing it (hence my comment). Aug 28, 2012 at 11:01
  • Thanks for the suggestion. Unfortunately, it did not solve my problem. I still get the same result after installing both packages. I have updated all my other packages as well.
    – Timror
    Aug 28, 2012 at 11:35
  • 3
    I have the same problem, when I use the option filename for get_map and go to the directory the file is saved the map image is there - it just does not plot when I use ggplot(map). I'm at a loss
    – dom_oh
    Mar 19, 2013 at 5:53
1

I had this error but it is solved now that I upgraded to the latest version of ggmap (V2.3)

1
  • I'm having the same problem, updating to latest (2.7) didn't help -- nor did installing from the Github. Mar 26, 2017 at 2:03
0

I had the same problem. Reinstalling ggmap fixed the problem.

0

In my situation the solution was in graphics itself. Something has changed my color depth to 16-bit, so changing to 32-bit resolved problem.

-2

So if you are a RStudio user like myself, you have to remember that code in a .R acts slightly differently than running code from the R command line. The code below, taken from Max Marchi's R blog, works perfectly if executed from the command line.

library(ggmap)
library(mapproj)
map <- get_map(location = 'England', zoom = 7)
ggmap(map)

However, take the same code above and place it within a .R in RStudio, and the result will be a blank window where a map was expected to appear. To fix this problem, place ggmap(map) inside of print() like the code below.

library(ggmap)
library(mapproj)
map <- get_map(location = 'England', zoom = 7)
print(ggmap(map))

The code above should produce a map of England in the plotting window. At least it does for me.

My explanation for this may be incorrect so don't hold me to it. Since RStudio runs on top of R, you need to explicitly tell RStudio that certain commands are line commands and need treated like such. By placing the line command inside of print(), RStudio knows to execute that command within R's command line.

I welcome comments and a better explanation of what is going on.

2
  • This isn't the issue the user is having. You need to call print when running code that calls trellis/lattice/ggplot code from source, see: Calling print isn't an RStudio related issue. See: stackoverflow.com/questions/6783120/… . When you don't call print, nothing displays at all. In this case, the user is getting an empty ggplot window. Dec 26, 2013 at 13:27
  • not a RStudio issue, and not related to the question Feb 10, 2014 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.