2

I have a dataset that looks like this:

   LOCALITY      numbers
1   Airoli          72
2   Andheri East    286
3   Andheri west    208
4   Arya Nagar      5
5   Asalfa          7
6   Bandra East     36
7   Bandra West     72

I want to plot bubbles (bigger the number bigger would be the bubble) inside the map of mumbai for each location in dataset.

I loaded the map of mumbai using 'maps' library but now I am not sure on how to plot these in the map. Is it possible to do in R ?

I used this to load the map:

library(ggmap)
library(mapproj)
maps <- get_map(location = 'Mumbai', zoom = 12)
ggmap(maps)

enter image description here

Jaap
  • 81,064
  • 34
  • 182
  • 193
Dheeraj Singh
  • 715
  • 1
  • 12
  • 24
  • When you are using `ggmap`, you will need longitude and latitude coordinates for the points you want to plot as wel. Those coordinates are now missing in your example data. For more inspiration, see [this](http://stackoverflow.com/a/22459372/2204410) and [this](http://stackoverflow.com/a/23143501/2204410) answer. – Jaap Jun 22 '15 at 14:13
  • Can you add those coordinates to your example data? – Jaap Jun 22 '15 at 14:14

1 Answers1

3

This should get you headed in the right direction, but be sure to check out the examples pointed out by @Jaap in the comments.

library(ggmap)

map <- get_map(location = "Mumbai", zoom = 12)

df <- data.frame(location = c("Airoli",
                              "Andheri East",
                              "Andheri West",
                              "Arya Nagar",
                              "Asalfa",
                              "Bandra East",
                              "Bandra West"),
                 values = c(72, 286, 208, 5, 7, 36, 72),
                 stringsAsFactors = FALSE)

locs_geo <- geocode(df$location)
df <- cbind(df, locs_geo)
df
#       location values      lon      lat
# 1       Airoli     72 72.99348 19.15793
# 2 Andheri East    286 72.87270 19.11549
# 3 Andheri West    208 72.82766 19.13632
# 4   Arya Nagar      5 80.32170 26.48341
# 5       Asalfa      7 72.89514 19.10023
# 6  Bandra East     36 72.84935 19.06053
# 7  Bandra West     72 72.83625 19.06069

ggmap(map) +
  geom_point(data = df, aes(x = lon, y = lat, size = values))

Map

JasonAizkalns
  • 20,243
  • 8
  • 57
  • 116
  • Nice, +1. I didn't know about the `geocode` function. – Jaap Jun 22 '15 at 14:20
  • Several tips for improvement: use `alpha=0.8` and `scale_size_continuous(range=c(2,10), breaks=c(10,100,200))` to improve the appearance of the plot. Furthermore, I would change the zoom-level to 11. This will include five points in the plot (the 6th is to far away). – Jaap Jun 22 '15 at 15:07
  • For some reason both my R-GUI and RStudio crashes when I try your answer on the last line. – SKG Jun 22 '15 at 16:59