无法在R的ggmap包中绘制数据

时间:2015-03-10 01:26:05

标签: r ggmap

我是新用户,因此对语言不太满意。 试图在英国曼彻斯特的地图上绘制鸟类记录的位置。 已设法创建一个包含以下代码的地图

  

MyMap中< -get_map(C(LON = 53.46388,LAT = -2.294037),变焦= 3,列= “BW”)

将电子表格作为xlsx文件从excel通过gdata读取,包含long和lat的列分配给Lon&纬度。

似乎能够qplot lon& lat但不能作为地图上的图层,当我尝试这个时我得到以下错误

错误:ggplot2不知道如何处理班级列表的数据

我现在已经尝试了很多代码组合,我不可能提供关于我如何将数据附加到我的地图上的示范性线,在线跟踪教程无济于事 - 这是一个问题在我的xlsx文件中?

已编辑:示例代码:

 #Here is what Jamie Dunning tried:
require(ggmap)
origin<-c("Worsley,Salford","Elton reservoir","Etherow country park","Blackleach country park","Low Hall,LNR, Wigan","Cheadle royal","Rhodes lodges,Middleton","Persons flash,Wigan","Sale water park","Plattfields","Higher Boarshaw","Clifton country park","Horrocks flash")  

ringing.origins<-geocode(origin) 

map<-c(get_map("Greater Manchester") 

swans.coor<-cbind(ringing.origins$lon,ringing.origins$lat)

我还没有一个成功绘制它们的例子。

2 个答案:

答案 0 :(得分:1)

使用plotGoogleMaps

的另一种选择

1-获取坐标

require(ggmap)

#List places to find GPS coordinates for:
    origin<-c("Worsley,Salford","Elton reservoir","Etherow country park","Elton reservoir","Blackleach country park","Low Hall,LNR, Wigan","Cheadle royal","Rhodes lodges,Middleton","Persons flash,Wigan","Sale water park","Plattfields","Higher Boarshaw","Clifton country park","Horrocks flash")

#Get coordinates via geocode function    
    ringing.origins<-geocode(origin)

#Put these coordinates in a data frame for creating an SP object later 
    df <- as.data.frame(origin)
    row.names(df) <- 1:nrow(df)

2-创建空间对象

require(sp)
#Coordinates must be numeric and binded together as one element and rows numbered:
    ringing.origins$lon <- as.numeric(ringing.origins$lon)
    ringing.origins$lat <- as.numeric(ringing.origins$lat)
    coord <- cbind(ringing.origins$lon,ringing.origins$lat)
    row.names(coord) <- 1:nrow(coord)
#Define a mapping projection
    AACRS <- CRS("+proj=longlat +ellps=WGS84")
#Creating a spatial object of "df" using the binded coordinates "coord":
    Map2 <- SpatialPointsDataFrame(coord, df, proj4string = AACRS, match.ID = TRUE) 

3 - 创建交互式html googlemap:

 require(plotGoogleMaps)
    #Simple Map
    plotGoogleMaps(Map2)
    #Map with some options, filename creates a file in the working directory.
    plotGoogleMaps(Map2, mapTypeId="ROADMAP", colPalette="red", legend=FALSE, filename="Swan_Map.htm")

enter image description here

答案 1 :(得分:0)

使用ggmap

绘图
require(ggmap)
#Get your coordinates
origin<-c("Worsley,Salford","Elton reservoir","Etherow country park","Elton reservoir","Blackleach country park","Low Hall,LNR, Wigan","Cheadle royal","Rhodes lodges,Middleton","Persons flash,Wigan","Sale water park","Plattfields","Higher Boarshaw","Clifton country park","Horrocks flash") 
ringing.origins<-geocode(origin) 

#Map of Greater Manchester
map<-get_map("Greater Manchester")
ggmap(map, extent = 'normal') +
  geom_point(aes(x = lon, y = lat), data = ringing.origins)
#Box is too small...

#Bounding box with All points
mymap<-get_map(c(lon=-2.294037,lat=53.46388),zoom=10)
ggmap(mymap, extent = 'device') +
  geom_point(aes(x = lon, y = lat), data = ringing.origins, alpha = 1)