[英]Loading a raster data of map PNG on ggmaps of R
这将在R的邮件列表中交叉发布。
我把地图作为png,所以我不会使用get_map
函数。 我已经从png中提取了栅格数据,我希望在R的显示上加载地图,然后我想在其上绘制一个点。
所以,这是我尝试ggmaps
的方式。 该程序编译正常。 这里的问题是没有显示任何输出。
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))
我当然做错了什么。 请指出。
> 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
>
编辑:我发现了一个错误。 实际上我是第一次用source (uff.R)
运行它,这个命令没有显示任何错误。 然后我尝试了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
你的ggimage
失败了,因为它没有x
和y
。 将您的lat-long coords重命名为x和y。 这是一个完全可重复的例子。 这是基本的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))
在命令行上运行这些命令,您应该看到一个图。 失败的可能原因是:
调用基于grid
的绘图库( lattice
和ggplot2
)时,需要显式print
绘图,以便在交互式会话之外使用它时获取任何输出:
bla = ggplot(...)
print(bla)
或更短:
print(ggplot(...))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.