How to plot data points at particular location in a map in R

3.1k views Asked by At

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

1

There are 1 answers

3
JasonAizkalns On BEST ANSWER

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