简体   繁体   English

在G的ggmaps上加载地图PNG的栅格数据

[英]Loading a raster data of map PNG on ggmaps of R

This will be cross posted on R's mailing list. 这将在R的邮件列表中交叉发布。

I have the map as a png, so I won't be using the get_map function. 我把地图作为png,所以我不会使用get_map函数。 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. 我已经从png中提取了栅格数据,我希望在R的显示上加载地图,然后我想在其上绘制一个点。

So, here's the way I have tried ggmaps . 所以,这是我尝试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. 实际上我是第一次用source (uff.R)运行它,这个命令没有显示任何错误。 Then I tried Rscript . 然后我尝试了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

Your ggimage is failing because there's no x and y in it. 你的ggimage失败了,因为它没有xy Rename your lat-long coords to x and y. 将您的lat-long coords重命名为x和y。 Here is a completely reproducible example. 这是一个完全可重复的例子。 This is basic ggplot stuff: 这是基本的ggplot东西:

> 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. 您的R没有与显示器的X11连接。 Is this all running on a local Linux machine? 这一切都在本地Linux机器上运行吗? 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. 如果R无法弹出图形窗口,它可能会尝试创建一个Rplots.pdf文件。
  • 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. 在print()函数调用中包装要生成输出的所有ggplot,网格和晶格图形函数。 This is a FAQ, I think. 我想这是一个FAQ。 Paul Hiemstra put this as an answer but then deleted it... 保罗·希姆斯特拉(Paul Hiemstra)将此作为答案,但随后将其删除......

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: 调用基于grid的绘图库( latticeggplot2 )时,需要显式print绘图,以便在交互式会话之外使用它时获取任何输出:

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

or shorter: 或更短:

print(ggplot(...))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM