Jamie Dunning
Jamie Dunning

Reputation: 153

trouble plotting data in the ggmap package for R

I'm a New R user so not quite comfortable with the language. Attempting to plot the locations of bird records on a map of Manchester, England. have managed to create a map with following code

mymap<-get_map(c(lon=53.46388,lat=-2.294037),zoom=3,col="bw")

Have read spreadsheet as an xlsx file from excel via gdata, columns containing both long and lat assigned to Lon & Lat.

Seem to be able to qplot lon&lat but not as a layer on the map, when I attempt this I get the following error

Error: ggplot2 doesn't know how to deal with data of class list

I've now tried so many combinations of code it would be impossible for me to offer a demonstrative line on how I'm attempting to attach the data onto my map, have followed tutorials online to no avail - is it a problem in my xlsx file?

Edited: Sample code :

 #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)

I'm yet to have an example where they are plotted successfully.

Upvotes: 0

Views: 403

Answers (2)

Kvasir EnDevenir
Kvasir EnDevenir

Reputation: 927

Plotting using 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)

Upvotes: 0

Kvasir EnDevenir
Kvasir EnDevenir

Reputation: 927

Another Alternative using plotGoogleMaps

1- Get coordinates

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- Create Spatial Object

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-Create an interactive 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

Upvotes: 1

Related Questions