Alexey Ferapontov Alexey Ferapontov - 2 months ago 13x
R Question

geocode/google restricts 2500 requests per day or code run?

Building on top of this one:
Plot USA map - big white triangle. FIXED. New: geocode limit of 2500 requests per day

I want to mark cities on map with number of ads/calls/etc - a data frame of 4900 locations. However, google restricts usage to non-business users to 2500 per day. Or one run? I could successfully run the code more than one time, if I cut my dataset at 2500. What is the rule then?

Anyways. Do you know of a more elegant solution other than breaking the DF in smaller (<= 2500) rows data frames, making a geopoint, and stitching?

E.g. like that, with pseudodata. (Apparently, I could exceed 2500 limit per day, and I ran it 10 times.)

state = rep("IL", 2500)
city = rep("Chicago", 2500)
ads = rep(15, 2500)

ads_df = data.frame(state,city,ads)
ads_df <- cbind(geocode(as.character(ads_df$city)), ads_df)

state= rep("FL", 2500)
city = rep("Miami", 2500)
ads = rep(15, 2500)

ads_df1 = data.frame(state,city,ads)
ads_df1 <- cbind(geocode(as.character(ads_df1$city)), ads_df1)

ads_df = rbind(ads_df,ads_df1)

plot(ggplot(US, aes(x=long, y=lat)) +
geom_polygon(aes(group = group) ) +
coord_map() + geom_point(data=ads_df, aes(x=lon, y=lat, size=ads), color="orange"))


Here's a complete example that also uses a proper US projection:

# devtools::install_github("hrbrmstr/localgeo")

state <- c(rep("IL", 2500), rep("FL", 2500))
city <- c(rep("Chicago", 2500), rep("Miami", 2500))
ads <- c(rep(15, 2500), rep(15, 2500))

ads_df <- data.frame(state, city, ads, stringsAsFactors=FALSE)
ads_df <- bind_cols(ads_df, geocode(ads_df$city, ads_df$state))

us <- map_data("state")
us <- fortify(us, region="region")

# for theme_map

gg <- ggplot()
gg <- gg + geom_map(data=us, map=us,
                    aes(x=long, y=lat, map_id=region, group=group),
                    fill="#ffffff", color="#7f7f7f", size=0.25)
gg <- gg + geom_point(data=ads_df,
                      aes(x=lon, y=lat, size=ads), color="orange")
# life's too short for bad projections
gg <- gg + coord_map("albers", lat0=39, lat1=45)
gg <- gg + theme_map()
gg <- gg + theme(legend.position="bottom")

enter image description here

Note that this package will not be a full replacement for an API service and is not nearly as forgiving about names as Nokia or Google is. But, it can help when you hit API limits and have fairly decent/clean city names/data.