Search code examples
rggplot2ggmap

How to label point on ggmap using geom_label_repel in R?


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,


Solution

  • 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.