0

I am making a map by tmap in R by combining the data using the area code. Not all the crime data is available. After I combining the data of the area and the crime data, I cannot plot the map

uk_la1 <- readOGR(dsn = "./infuse_dist_lyr_2011", layer = "infuse_dist_lyr_2011")

Totalcrime <- read.csv('Total_crime_in_each_area_full.csv', header = TRUE)

# Calculate the nnumber of crime per 1000 people
Totalcrime <- transform(Totalcrime, Crime_per_1000_people = Total / Population * 1000)

Totalcrimeno<-Totalcrime %>% select(geo_code, Crime_per_1000_people)

uk_la1@data<-left_join(uk_la1@data, Totalcrimeno,
                               by=c('geo_code'))

qtm(uk_la, fill="Crime_per_1000_people")

But I get an error:

Error in $<-.data.frame(*tmp*, "geometry", value = list(list(list( : replacement has 404 rows, data has 405

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
Florence
  • 7
  • 1
  • Hard to tell without the data where the issue is (maybe in the joining?), but I noticed that you plot `uk_la` instead of `uk_la1` - is that just a typo? – Valentin_Ștefan Dec 30 '18 at 20:39

1 Answers1

0

You need to convert Longitude and Latitude with WGS84 Coordinate System:

library(rgdal)
library(spdplyr)
library(geojsonio)
library(rmapshaper)

uk_la <- readOGR(dsn = "./infuse_dist_lyr_2011", layer = "infuse_dist_lyr_2011")

wgs84 <- "+proj=longlat +datum=WGS84"
uk_la_trans <- spTransform(uk_la, CRS(wgs84))

#Convert from Spatial Dataframe to GeoJson 
uk_la_trans_json <- geojson_json(uk_la_trans)
uk_la_trans_sim <= ms_simplify(uk_la_trans_json)

More info on WGS84 Coordinate System