使用R将底图添加到SpatialPointDataFrames [英] Add basemap to SpatialPointDataFrames using R

查看:82
本文介绍了使用R将底图添加到SpatialPointDataFrames的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的绘图中添加一个底图,以可视化三个SpatialPointDataFrames. 我已经尝试过maptools和RgoogleMaps包,但是我都不想这样做.
我的问题:SpatialPointDataFrames不在GoogleMaps背景地图上绘制.

I want to add a basemap to my plot, which visualizes three SpatialPointDataFrames. I've already tried the maptools as well as RgoogleMaps package, but both don't work in the way, which I want to.
My problem: The SpatialPointDataFrames are not drawn on the GoogleMaps Background map.

一个最小的例子:

city.csv,其中包含以下示例内容:

The city.csv with the following example content:

FID,city,POINT_X,POINT_Y
0,New York,-73.996786,40.720813
1,Newark,-74.172237, 40.732196

R代码:

# Load packages
library(RgoogleMaps)
library(sp)

# load .csv file 
city= read.csv("city.csv", header = TRUE)

# convert to SpatialPointDataFrame
coordinates(city) <- c("POINT_X", "POINT_Y")
proj4string(city) <- CRS("+proj=longlat +datum=WGS84")

# use RgoogleMaps
gc <- geocode('new york, usa')
center <- as.numeric(gc)
ggmap(get_googlemap(center = center, color = 'bw', scale = 4), fullpage = T)
# Plot the city dataset
plot(city, pch = 22, col="black", bg= "yellow", cex = 1.5, add = TRUE)

结果应为包含背景图和两个点的图,但是这些点未绘制在地图上. 是否存在地址解析问题或我错过了什么?可以将ggmap和plt函数结合起来吗?

The result should be a plot with the background map and the two points, but the points are not drawn on the map. Is there a geocoding problem or do I miss anything? Is it possible to combine ggmap and plt functions?

非常感谢您的帮助!

推荐答案

使用ggplot2进行此类工作要容易得多,您可以在ggmap图层中添加点,多边形,2密度等.

Using ggplot2 for this kind of work is much easier, you can add points, polygons, 2densities, etc... to ggmap layers.

library(RgoogleMaps)
library(sp)
library(ggplot2)
library(ggmap)

P是SpatialPointsDataFrame对象:

P is the SpatialPointsDataFrame object:

DB <- data.frame(FID=P$FID, city=P$city)
DB <- cbind(DB, P@coords)


DB <- data.frame(FID=c(0,1), city=c("New York", "Newark"),   POINT_X=c(-73.996786,-74.172237), POINT_Y=c(40.720813,40.732196 ))
gc <- geocode("new york, usa")
center <- as.numeric(gc)
G <- ggmap(get_googlemap(center = center, color = 'bw', scale = 4), extent = "device")
G1 <- G + geom_point(aes(x=POINT_X, y=POINT_Y ),data=DB, color="red", size=5)
plot(G1)

这是输出:

这篇关于使用R将底图添加到SpatialPointDataFrames的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆