0

I am currently doing spatial analysis project where I have a shapefile (in Delhi region). That shapefile also consists of ward level data. Sample data set is given below:

A    B    C   Ward_No
1    2    3     6
4    5    6    12
43   4    82   26
..    ...  ....

I want to plot that file into my Google map (based of Ward_No) and then plot some points on it.

So far I have done the following parts in R:

# Reading a shapefile with rgdal
library(rgdal)
library(ggplot2)
library(tmap)
s1 <- readOGR(".", "Delhi")
summary(s1)

# Convert it into data frame
y = as.data.frame(s1)

# Plotting that shapefile... Not in top of Google map
z = tm_shape(s1) +
  tm_borders("black") +
  tm_layout(frame = FALSE,
            inner.margins = c(0, 0.25, 0, 0.01)) 


# Create a data frame which having lat/lon data

# No     lon         lat
# 1   77.138015   28.6516444
# 2   77.0738028  28.6387532
# 3   77.1337863  28.7146212
# 4   77.0714699  28.5897667
# 5   77.0919553  28.626676
# 6   77.2860315  28.6320635

# Plot coordinates above the shapefile.

pt <- SpatialPointsDataFrame(coords = geodata[,c("lon","lat")], data = geodata,proj4string = CRS("+proj=longlat +datum=WGS84"))
z = tm_shape(s1) +
  tm_borders("black") +
  tm_layout(frame = FALSE,
            inner.margins = c(0, 0.25, 0, 0.01)) +
  tm_shape(pt) +
  tm_dots(size=1, col="yellow", border.col="black") 

It's working fine but when I try to plot this in my Google map it's not working. The code is:

sgmap <- get_map(location="New Delhi", zoom=11,   
                 source="google")

p <- ggmap(sgmap) + z
p
1

You can't mix ggplot (ggmap) and tmap structures. They are two different systems.

You need to either do everything with ggplot or tmap.

  • @Spaceman - You are right..just now solve this problem – Nikita Agarwal Mar 20 '18 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.