Reputation: 1
So i have the shapefile with all the geodata that i need
so far my code i have tried is this
tm_shape(LSOA_London) + tm_fill( "$Crime_rate", palette = "spectral") + tm_border()
the issue i am having is that the "crime_rate" data is from a csv file and i dont know how to convert it so that it will work in my map.
do i need to rasterise the data?
any help would be great!
as shown above that is what i have tried, im yet to merge the csv with the .shp file but again im unsure on how this works. i havent done rstudio coding for a while so its all very rusty.
Upvotes: 0
Views: 77
Reputation: 3604
You have to join the shape data with the data in .csv
file. As it's hard to guess the data you are using, below example based on some online data available. The join type depends on kind of the data in two datasets: either you can use left_join()
from {dplyr} package to join on particular columns, either left_join()
from {sf} package, if both datasets are spatial data.
The boundaries came from {geographr} package:
london_lsoa <- geographr::boundaries_lsoa11
london_lsoa
#> Simple feature collection with 34753 features and 2 fields
#> Geometry type: MULTIPOLYGON
#> Dimension: XY
#> Bounding box: xmin: -6.378393 ymin: 49.88235 xmax: 1.762942 ymax: 55.81107
#> Geodetic CRS: WGS 84
#> # A tibble: 34,753 × 3
#> lsoa11_name lsoa11_code geometry
#> * <chr> <chr> <MULTIPOLYGON [°]>
#> 1 City of London 001A E01000001 (((-0.0972656 51.52158, -0.0947445 51.…
#> 2 City of London 001B E01000002 (((-0.092737 51.52139, -0.088103 51.51…
#> 3 City of London 001C E01000003 (((-0.0967596 51.52325, -0.0945333 51.…
[...]
And crime data from online resource:
download.file(url = "https://data.london.gov.uk/download/recorded_crime_summary/11f0256e-bbde-46ca-ac49-89eaba2bd4b8/MPS%20LSOA%20Level%20Crime%20%28most%20recent%2024%20months%29.csv",
destfile = "~/Downloads/LSOA_crime.csv",
method = "wget",
extra = "-c")
lsoa_crime <-
read.csv(file = "~/Downloads/LSOA_crime.csv")
head(lsoa_crime)
#> LSOA.Code LSOA.Name Borough
#> 1 E01000006 Barking and Dagenham 016A E09000002
#> 2 E01000006 Barking and Dagenham 016A E09000002
#> 3 E01000006 Barking and Dagenham 016A E09000002
#> Major.Category Minor.Category X202202 X202203
#> 1 Arson and Criminal Damage Criminal Damage 1 0
#> 2 Burglary Domestic Burglary 0 1
#> 3 Drug Offences Drug Trafficking 0 0
#> X202204 X202205 X202206 X202207 X202208 X202209 X202210 X202211 X202212
#> 1 0 0 0 2 1 0 0 0 0
#> 2 0 0 0 0 0 0 0 2 0
#> 3 0 0 0 0 0 0 0 0 0
As you can see, both datasets have a 'common' column lsoa11_code
in boundaries and LSOA.Code
in crime data. We can use this column to join both datasets, filter out just example column with numbers of crime and summarise it for each LSOA area:
lsoa <- london_lsoa |>
dplyr::left_join(lsoa_crime, by = c("lsoa11_code" = "LSOA.Code")) |>
subset(!is.na(X202202), select = c("lsoa11_name", "lsoa11_code", "X202202", "geometry")) |>
dplyr::group_by(lsoa11_code) |>
dplyr::summarise(sum = sum(X202202), .groups = "keep")
tmap::tm_shape(lsoa) +
tmap::tm_polygons(fill = "sum",
fill.scale = tmap::tm_scale_intervals(n = 6,
values = "Blues")) +
tmap::tm_borders(col = "grey60", lwd = 0.1)
Created on 2024-03-04 with reprex v2.1.0
Upvotes: 0