1

I have a dataframe that consist of 5 Hotel names and their respective locations. I am ggmap to plot my hotels on Map using below code. However, I ran into issue when I tried to display my Hotel names on to the Plot. Initially, I thought, I should put Hotel names in legend and color of 'points/dots" should correspond to hotel names. My issue here was that I couldn't get my 'points/dots' of same size. After research I found better way that would plot the hotel names directly onto Map. This is where I came across the Plot created by Tung. However, Whenever I run below code It is giving me an error.

My datafarme is as follows:

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.168713, 
-122.395447, -122.407291, -121.490768, -117.916417), lati = c(32.709745, 
37.794589, 37.789216, 38.577627, 33.789322)), .Names = c("hotel_name", 
"longi", "lati"), row.names = c("h1_Loc", "h2_Loc", "h3_Loc", 
"h4_Loc", "h5_Loc"), class = "data.frame")
> ggmap(get_map(location = 'California', zoom = 6, maptype = "roadmap"))+ggplot(d1, aes(x= d1$longi, y = d1$lati)) +geom_point(color = "blue", size = 3)

Code I tried to get my Hotel names on Map instead of Legend:

ggmap(get_map(location = 'California', zoom = 6, maptype = "roadmap"))+ggplot(d1, aes(x= d1$longi, y = d1$lati)) +geom_point(color = "blue", size = 3)
+ geom_label_repel(
  aes(d1$longi, d1$lati, label =d1$hotel_name),
  box.padding = 0.35, point.padding = 0.5,
  segment.color = 'grey50') +
  theme_classic(base_size = 12)

Error I am getting after running the above code:

Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=California&zoom=6&size=640x640&scale=2&maptype=roadmap&language=en-EN&sensor=false
Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=California&sensor=false
Error: Don't know how to add o to a plot

Thanks in advance for help and effort. Being a newbie to R if you can provide and explanation with the code, is much appreciated. Thanks in advance,

Community
  • 1
  • 1
Data_is_Power
  • 765
  • 3
  • 12
  • 30

1 Answers1

2

Pretty simple solution: ggmap creates a ggplot object, so the bit you have about ggmap(...) + ggplot(...) is essentially adding 2 ggplots together, which doesn't work. That's where the error comes from.

The data from your dataframe will be the data argument to your call to geom_point.

Change it to

ggmap(get_map(location = 'California', zoom = 6, maptype = "roadmap")) +
    geom_point(aes(x = longi, y = lati), data = d1, color = "blue", size = 3)

And that adds the geom_point layer onto the ggplot object created by ggmap.

Also, in ggplot you don't want to name the dataframe in your aes arguments---hence why I changed x = d1$longi to x = longi, and similarly for y and any other aesthetics you might need to map.

camille
  • 16,432
  • 18
  • 38
  • 60
  • Thanks for your response. How should I proceed about adding my Hotel names onto the Map ? – Data_is_Power Mar 24 '18 at 00:37
  • You can add another layer with the appropriate aesthetics and data: `geom_label_repel(aes(x = longi, y = lati, label = hotel_name), data = d1)` plus whatever color, padding, etc you want should do it – camille Mar 25 '18 at 02:08
  • Thanks for comments! I have added my hotel names using geom_label_repel(aes(x=longi, y=lati, label=hotel_names), data=d1,box.padding = 2, point.padding = 0.5) – Data_is_Power Mar 25 '18 at 16:31