0
votes

I am trying to create map of California state following the ggmap link. However,when I am running the below code it is giving me below error.

get_map(location ='california',zoom =4)

Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=california&zoom=4&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=california&sensor=false 1280x1280 terrain map image from Google Maps. see ?ggmap to plot it.

In addition, I am planning on trying to plot some location on same map. Any help is much appreciated.

My dataset for plotting latitude and longitude looks like following:

structure(list(hotel_name = structure(c(1L, 5L, 2L, 4L, 3L), .Names = c("h1_Loc", 
"h2_Loc", "h3_Loc", "h4_Loc", "h5_Loc"), .Label = c("Grand Hyatt San Diego", 
"Grand Hyatt San Francisco", "Hyatt Regency Orange County", "Hyatt Regency Sacramento", 
"Hyatt Regency San Francisco"), class = "factor"), longi = c(-117.1687, 
-122.3954, -122.4073, -121.4908, -117.9164), lati = c(32.70974, 
37.79459, 37.78922, 38.57763, 33.78932)), .Names = c("hotel_name", 
"longi", "lati"), row.names = c("h1_Loc", "h2_Loc", "h3_Loc", 
"h4_Loc", "h5_Loc"), class = "data.frame")
1
get_map() per the documentation, returns a ggmap object. To plot it, you will want to use ggmap. For example, ggmap(get_map(location ='california', zoom=4)). Alternatively, cali <- get_map(location ='california', zoom=4); ggmap(cali);.JasonAizkalns
@JasonAizkalns. I tried your alternative approach. However, the map is NOT zooming on 'California' . Please see above map. Any suggestion on how to fix this ?Data_is_Power
Try playing with the zoom argument.JasonAizkalns
@JasonAizkalns. Great. Thanks! would it be possible for you to show me how to plot latitude on map as well ?Data_is_Power
There are a lot of great resources and questions - please try and search, but to get you started: my_points <- data.frame(lon = c(-120, -116), lat = c(36, 33)); ggmap(get_map(location = 'California', zoom = 6, maptype = "roadmap")) + geom_point(aes(x = lon, y = lat), data = my_points, color = "red")JasonAizkalns

1 Answers

0
votes

Expanding upon JasonAizkaln's answer, but the get_map function just downloads the tile from google maps. The message shown is not an error, but just a message to let you know about this download.

If you want to disable the message, you can add messaging = FALSE to get_map, as explained in the documentation.

To plot the result, you need to use the ggmapfunction, which creates a typical ggplot object:

map <- get_map(location ='california',zoom = 4)
ggmap(map)

enter image description here

You can easily add data to this using the standard ggplot functions. Loading your supplied data as df, we can plot the points as follows:

map <- get_map(location ='california',zoom = 5)
ggmap(map) + 
  geom_point(data = df, aes(x =longi, y = lati))

enter image description here