1

This will be cross posted on R's mailing list.

I have the map as a png, so I won't be using the get_map function. I have extracted the raster data from the png, and I wish to load the map as it is on the display of R, and then I would like to plot a point on it.

So, here's the way I have tried ggmaps. The program is compiling fine. Problem here is that there isn't any output being shown.

library (png)
library (ggmap)

latitude  = c(40.702147,40.718217,40.711614)
longitude = c(-74.012318,-74.015794,-73.998284)

# Reads a PNG and outputs a raster array.
img <- readPNG (system.file ("img", "My.png", package="png"))

df <- data.frame (latitude, longitude)

# img: raster array read from the map png.
ggimage (img, fullpage = TRUE) + geom_point (data = df, aes_auto (df), size = 2)

qplot (latitude, longitude, data = df, colour = I("red"), size = I(3))

Of course I am doing something wrong. Please point out.

> sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: x86_64-unknown-linux-gnu (64-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  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggmap_2.1     ggplot2_0.9.1 png_0.1-4    

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         proto_0.3-9.2      RColorBrewer_1.0-5 reshape2_1.2.1    
[13] RgoogleMaps_1.2.0  rjson_0.2.8        scales_0.2.1       stringr_0.6       
[17] tools_2.15.1      
> 

EDIT: I have found an error. Actually I was first running it with source (uff.R), and this command didn't show any error. Then I tried Rscript.

anisha@linux-y3pi:~> Rscript uff.R
Loading required package: ggplot2
Loading required package: methods
Error in eval(expr, envir, enclos) : object 'x' not found
Calls: print ... sapply -> lapply -> eval.quoted -> lapply -> FUN -> eval
Execution halted
www
  • 38,575
  • 12
  • 48
  • 84
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
  • If you have two separate question, please ask two separate questions, especially if they are quite different. – Paul Hiemstra Jul 24 '12 at 08:44
  • @PaulHiemstra alright, I'll remove the second one. – Aquarius_Girl Jul 24 '12 at 08:44
  • Feel free to repost your second question as a separate question – Paul Hiemstra Jul 24 '12 at 08:48
  • 1
    If its map-related, post it to R-sig-geo, not R-help – Spacedman Jul 24 '12 at 09:34
  • @Spacedman Yes, ofcourse, I have posted this to R-sig-geo. :) – Aquarius_Girl Jul 24 '12 at 09:45
  • Don't cross-post at all, until it's clear that you don't get a satisfactory answer after some delay. – Andrie Jul 24 '12 at 09:45
  • @Andrie What is time duration considered for delay? How many hours according to you? Besides all people of R list may not be on the stackoverflow. Also If the solution is found on one side, I do update the other. – Aquarius_Girl Jul 24 '12 at 09:46
  • Your problem in this question is that you are not using `print` to display the image. This will be solved in seconds. I would suggest waiting at least 24 hours before cross-posting, if not 48. – Andrie Jul 24 '12 at 09:48
  • The real problem is not breaking the problem down, trying it one line at a time on the command line. For example currently the ggimage line is a total red herring. It sits there, looking complicated and scary, but has nothing to do with the problem of not calling your coords x and y, or not wrapping the qplot in a print in a script. A little more elimination is required! Read any Sherlock Holmes? – Spacedman Jul 24 '12 at 09:51
  • @Andrie Being a newbie these problems appear like mountains to me. But I do search google and try different things before asking for sure. – Aquarius_Girl Jul 24 '12 at 09:52
  • @user462608: Your question helped me a lot, there's only one thing left that I need to know: How did you adjust the axes so that the points can be plotted to the right positions? – maj Oct 23 '13 at 12:57
  • @maj Am really sorry, I worked on this long back and therefore I don't remember much of it now. – Aquarius_Girl Oct 23 '13 at 14:40

2 Answers2

3

Your ggimage is failing because there's no x and y in it. Rename your lat-long coords to x and y. Here is a completely reproducible example. This is basic ggplot stuff:

> library(png)
> library(ggplot2)
> img <- readPNG(system.file("img", "Rlogo.png", package="png"))
> latitude  = c(40.702147,40.718217,40.711614)
> longitude = c(-74.012318,-74.015794,-73.998284)
> df <- data.frame (x=longitude,y=latitude)
> qplot(x,y,data = df, colour = I("red"), size = I(3))

Run those commands on your command line and you should see a plot. Possible reasons for failing are:

  • Your R doesn't have an X11 connection to your display. Is this all running on a local Linux machine? You haven't connected to a server? If R can't pop up a graphics window it will probably try and create an Rplots.pdf file.
  • You are running in a script which isn't printing. Wrap all ggplot, grid, and lattice graphics functions that you want to produce output in print() function calls. This is a FAQ, I think. Paul Hiemstra put this as an answer but then deleted it...
Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • thanks, error is gone now. But the "display window" is still NOT shown. Does `qplot` show a display window? – Aquarius_Girl Jul 24 '12 at 09:44
  • 1
    The OP seems to be running the code non-interactively. So it might be useful to show `p <- qplot(...); print(p)` – Andrie Jul 24 '12 at 09:46
  • @Andrie AHH, I have indeed written this code in a file, and I am executing that file as "source (uff.R)". Is that the reason of the problems? – Aquarius_Girl Jul 24 '12 at 09:49
  • @AnishaKaul Yes, this is the problem. It's a FAQ: http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-is-the-output-not-printed-when-I-source_0028_0029-a-file_003f – Andrie Jul 24 '12 at 09:53
  • @Andrie Thanks, will read FAQ properly before asking the next question. BTW, I am very happy that finally the map is getting displayed. Though I haven't been able to plot any points it yet. But I'll read more. – Aquarius_Girl Jul 24 '12 at 09:59
1

When calling grid based plotting libaries (lattice and ggplot2), you need to explicitly print the plot in order to get any output when using it outside of an interactive session:

bla = ggplot(...)
print(bla)

or shorter:

print(ggplot(...))
Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149