0

I have a data frame c which include lat-long and name of some districts and some other information. my ultimate goal is to represent this information on the map.

using these codes I am made a map which its center is the city of Tehran.

teh_center <- as.numeric(geocode("tehran"))
map <- get_map(location = c(lon = 51.38897, lat = 35.6892), zoom = 14,
               source = "google")

these codes are trying to expand the map throughout given lat-long (from here):

foo <-ggmap(map) +
  scale_x_continuous(limits = c(51, 51.8), expand = c(0, 0)) +
  scale_y_continuous(limits = c(35.45, 35.85), expand = c(0, 0)) 

at the end I tried to get map and information on it by:

foo + 
geom_point(aes(x=lon, y=lat), data=c, col=c$decile, alpha=0.4, size = 3)

but I receive this:enter image description here

as you can see, the map is in center of image but in a small size. how can I fix it?

mjoudy
  • 149
  • 1
  • 1
  • 10

1 Answers1

0

You should decrease the zoom level, as it's now, you are getting a small area from google, but you data seems to be covering a larger area around it

library(ggmap)
# teh_center <- as.numeric(geocode("tehran"))
map <- get_map(location = c(lon = 51.38897, lat = 35.6892), zoom = 10,
               source = "google")
#> Source : https://maps.googleapis.com/maps/api/staticmap?center=35.6892,51.38897&zoom=10&size=640x640&scale=2&maptype=terrain&language=en-EN


ggmap(map) +
  scale_x_continuous(limits = c(51, 51.8), expand = c(0, 0)) +
  scale_y_continuous(limits = c(35.45, 35.85), expand = c(0, 0)) 
#> Scale for 'x' is already present. Adding another scale for 'x', which
#> will replace the existing scale.
#> Scale for 'y' is already present. Adding another scale for 'y', which
#> will replace the existing scale.
#> Warning: Removed 1 rows containing missing values (geom_rect).

If you already know the lat/lon range of your data, you can get the desired map using RgoogleMaps::GetMap.bbox()

library(ggmap)
#> Loading required package: ggplot2
#> Google Maps API Terms of Service: http://developers.google.com/maps/terms.
#> Please cite ggmap if you use it: see citation("ggmap") for details.
library(RgoogleMaps)

bbox <- GetMap.bbox(c(35.45, 35.85), c(51, 51.8))
map <- get_map(location = c(bbox$lat.center, bbox$lon.center), zoom = bbox$zoom,
               source = "google")
#> Source : https://maps.googleapis.com/maps/api/staticmap?center=35.65,51.4&zoom=9&size=640x640&scale=2&maptype=terrain&language=en-EN


ggmap(map)

Created on 2018-05-10 by the reprex package (v0.2.0).

GGamba
  • 13,140
  • 3
  • 38
  • 47