0
votes
library(ggmap) # -- for geocoding, obtaining city locations
readRDS("gadm36_IND_2_sp.rds")
ind2 = gadm

The output at the end is as below:

Slot "bbox": min max x 68.186249 97.41516 y 6.754256 35.50133

Slot "proj4string": CRS arguments: +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 Error in eval(expr, envir, enclos): object 'gadm' not found Traceback:

After that the next code is :

# plotting districts of a State, in this case West Bengal
wb2 = (ind2[ind2$NAME_1=="West Bengal",])

nam = c("Purulia","Bankura","Midnapur")
pos = geocode(nam)
tlat = pos$lat+0.05    # -- the city name will be above the marker
cities = data.frame(nam, pos$lon,pos$lat,tlat)
names(cities)[2] = "lon"
names(cities)[3] = "lat"


text1 = list("panel.text", cities$lon, cities$tlat, cities$nam,col="red", cex = 0.75)
mark1 = list("panel.points", cities$lon, cities$lat, col="blue")
text2 = list("panel.text",87.0,26.0,"GADM map", col = "dark green", cex = 1.2)
spplot(wb2, "NAME_1",
sp.layout=list(text1,mark1, text2),
main="West Bengal Districts",
colorkey=FALSE, scales=list(draw=TRUE))

The output for the above code is: Error in eval(expr, envir, enclos): object 'ind2' not found Traceback:

How to plot locations in map?

1

1 Answers

0
votes

You didn't provide enough information to create a reproducible question, but I was able to put together some similar data and then plotted your locations on a map with the following code. I chose to go with simple feature objects rather than spatial objects.

First, created a simple feature object with the names and list column having the lon/lat of the cities.

pts.sf <- st_as_sf(city_pts, pts.sfc)

Then created the base map.

wmap <- rnaturalearth::ne_countries(scale = 'medium', type = 'map_units', returnclass = 'sf')
india <- wmap %>% filter(admin == "India") 
imap <- india$geometry
st_crs(imap) <- 4326

And then plotted the points on the basemap.

my_plot <- ggplot(data = imap) + 
    geom_sf() + 
    geom_sf(data = pts.sf, aes(),
    fill = "blue", color = "blue", size=10, alpha=0.5 ) +
    coord_sf(xlim = c(85, 90), ylim = c(21, 24), expand = FALSE) + 
    xlab("Longitude") + ylab("Latitude")

my_plot

The plot is provide at the link below. Good Question!

[ Link to Plot 1