-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapmeetings.R
59 lines (47 loc) · 2.12 KB
/
mapmeetings.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Make a map of all cities ESA has been held at
## This code has an MIT license
## functions getDocNodeVale and gGeoCode are from https://github.com/ezgraphs/R-Programs/blob/master/GoogleGeocodeMap.R
library(ggmap)
library(mapproj)
library(XML)
library(stringr)
file <- "~/Dropbox/website redesign/content/Maps_Graphs/Full Annual Meetings Table – The Ecological Society of Americas History and Records.csv"
getDocNodeVal=function(doc, path)
{
sapply(getNodeSet(doc, path), function(el) xmlValue(el))
}
gGeoCode=function(str)
{
library(XML)
u=paste('http://maps.google.com/maps/api/geocode/xml?sensor=false&address=',str)
doc = xmlTreeParse(u, useInternal=TRUE)
str=gsub(' ','%20',str)
lat=getDocNodeVal(doc, "/GeocodeResponse/result/geometry/location/lat")
lng=getDocNodeVal(doc, "/GeocodeResponse/result/geometry/location/lng")
c(lat,lng)
}
getlatlong <- function(locations) { #get lat and long for each location using gGeoCode
dat <- cbind(lat=numeric(0), long=numeric(0))
for (i in 1:length(locations)) {
latlon <- gGeoCode(locations[i])[c(1:2)] #choose only the first location returned. sometimes gGeoCode returns an empty list instead of a vector of latitude and longitude. That breaks this loop.
dat <- rbind(dat, latlon, deparse.level=0)
Sys.sleep(.5) #returns null values without this
}
dat <- data.frame(location=locations, dat)
}
#read in and clean city cata
citydat <- read.csv(file, stringsAsFactors=FALSE)
cities <- str_replace(cities, ",", "") #strip commas from cities so search doesn't return a list
# get lat/long for cities and put in a dataframe
dat <- getlatlong(cities)
##fix classes
dat$location <- as.character(dat$location)
dat$lat <- as.numeric(levels(dat$lat))[dat$lat]
dat$long <- as.numeric(levels(dat$long))[dat$long]
# map all the cities!
map <- get_map(location = "Iowa", zoom = 3, maptype = "roadmap") #make a basemap centered on kansas
p <- ggmap(map)
p <- p + labs(title = "ESA Meetings")
p <- p + geom_point(data = dat, aes(x = long, y = lat), size = 4)
#p <- p + geom_text(data = dat, aes(x = long, y = lat, label = location), hjust = -.2)
print(p)