library(tidyverse)
library(sf)

INFORMACIÓN GEOGRÁFICA DE LA CIUDAD DE LONDRES

Los datos de shapes de los distritos de Londres fueron tomadas de https://joshuaboyd1.carto.com/tables/london_boroughs_proper/public/map

Mientras que otros datos estadísticos de Londres fueron tomados del portal de datos abiertos de dicha ciudad https://data.london.gov.uk/

Definimos un dataframe con las geometrias de los distritos de Londres con sus nombres

london_area <- st_read("london_boroughs_proper.geojson", stringsAsFactors=TRUE)
## Reading layer `london_boroughs_proper' from data source `C:\Users\LUIS.PEREZ\Documents\curso UTDT\Modulo2\london_boroughs_proper.geojson' using driver `GeoJSON'
## Simple feature collection with 33 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -0.508813 ymin: 51.28691 xmax: 0.335677 ymax: 51.69207
## Geodetic CRS:  WGS 84
head(london_area, 10)
## Simple feature collection with 10 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -0.41808 ymin: 51.28691 xmax: 0.225322 ymax: 51.69207
## Geodetic CRS:  WGS 84
##                    name cartodb_id          created_at          updated_at
## 1  Barking and Dagenham          1 2015-07-01 06:57:45 2015-07-01 06:57:45
## 2                Barnet          2 2015-07-01 06:57:45 2015-07-01 06:57:45
## 3                Bexley          3 2015-07-01 06:57:45 2015-07-01 06:57:45
## 4                 Brent          4 2015-07-01 06:57:45 2015-07-01 06:57:45
## 5               Bromley          5 2015-07-01 06:57:45 2015-07-01 06:57:45
## 6                Camden          6 2015-07-01 06:57:45 2015-07-01 06:57:45
## 7        City of London          7 2015-07-01 06:57:45 2015-07-01 06:57:45
## 8               Croydon          8 2015-07-01 06:57:45 2015-07-01 06:57:45
## 9                Ealing          9 2015-07-01 06:57:45 2015-07-01 06:57:45
## 10              Enfield         10 2015-07-01 06:57:45 2015-07-01 06:57:45
##                          geometry
## 1  MULTIPOLYGON (((0.148209 51...
## 2  MULTIPOLYGON (((-0.183361 5...
## 3  MULTIPOLYGON (((0.158044 51...
## 4  MULTIPOLYGON (((-0.212138 5...
## 5  MULTIPOLYGON (((0.076463 51...
## 6  MULTIPOLYGON (((-0.140804 5...
## 7  MULTIPOLYGON (((-0.083712 5...
## 8  MULTIPOLYGON (((-0.077495 5...
## 9  MULTIPOLYGON (((-0.334018 5...
## 10 MULTIPOLYGON (((-0.010576 5...
summary(london_area)
##                    name      cartodb_id   created_at                 
##  Barking and Dagenham: 1   Min.   : 1   Min.   :2015-07-01 06:57:45  
##  Barnet              : 1   1st Qu.: 9   1st Qu.:2015-07-01 06:57:45  
##  Bexley              : 1   Median :17   Median :2015-07-01 06:57:45  
##  Brent               : 1   Mean   :17   Mean   :2015-07-01 06:57:45  
##  Bromley             : 1   3rd Qu.:25   3rd Qu.:2015-07-01 06:57:45  
##  Camden              : 1   Max.   :33   Max.   :2015-07-01 06:57:45  
##  (Other)             :27                                             
##    updated_at                           geometry 
##  Min.   :2015-07-01 06:57:45   MULTIPOLYGON :33  
##  1st Qu.:2015-07-01 06:57:45   epsg:4326    : 0  
##  Median :2015-07-01 06:57:45   +proj=long...: 0  
##  Mean   :2015-07-01 06:57:45                     
##  3rd Qu.:2015-07-01 06:57:45                     
##  Max.   :2015-07-01 06:57:45                     
## 

Analizando el dataset a combinar con london_area

all_schools_base <- read.csv("all_schools_xy_2016_lgp2.csv", sep=",", stringsAsFactors = TRUE)
head(all_schools_base)
##   X OBJECTID    URN                                         SCHOOL_NAM
## 1 1        1 135155                        Ayesha Siddiqa Girls School
## 2 2        2 140492                                 Beis Medrash Elyon
## 3 3        3 141411                    Big Creative Independent School
## 4 4        4 142336                             Wetherby Senior School
## 5 5        5 100042 St Mary's Kilburn Church of England Primary School
## 6 6        6 100224                         De Beauvoir Primary School
##                       TYPE          PHASE                  ADDRESS        TOWN
## 1 Other Independent School Not applicable     165-169 The Broadway    Southall
## 2 Other Independent School Not applicable 233 West Hendon Broadway      London
## 3 Other Independent School Not applicable       Silver Birch House Walthamstow
## 4 Other Independent School Not applicable      100 Marylebone Lane      London
## 5   Voluntary Aided School        Primary                Quex Road      London
## 6         Community School        Primary        80 Tottenham Road      London
##   POSTCODE STATUS GENDER EASTING NORTHING              WARD_NAME
## 1  UB1 1LR   Open  Girls  521263   180470      Southall Broadway
## 2  NW9 7DG   Open   Boys  521939   188148            West Hendon
## 3  E17 5SD   Open  Mixed  535764   190188            Higham Hill
## 4  W1U 2QB   Open   Boys  528432   181474 Marylebone High Street
## 5  NW6 4PG   Open  Mixed  525453   183984                Kilburn
## 6   N1 4BS   Open  Mixed  533252   184710            De Beauvoir
##             LSOA_NAME        LA_NAME                                 WEBLINK
## 1         Ealing 026C         Ealing                                        
## 2         Barnet 036F         Barnet                                        
## 3 Waltham Forest 014C Waltham Forest                                        
## 4    Westminster 011B    Westminster                                        
## 5         Camden 020C         Camden http://www.stmarykilburn.camden.sch.uk/
## 6        Hackney 021E        Hackney          www.debeauvoir.hackney.sch.uk/
##       AGE     map_icon NEW_URN OLD_URN map_icon_l Primary          x       y
## 1  19-nov                   NA      NA          2       0 -0.3784960 51.5075
## 2  16-nov                   NA      NA          2       0 -0.2416280 51.5790
## 3 15 - 16                   NA      NA          2       0 -0.0425897 51.5940
## 4  16-nov                   NA      NA          2       0 -0.1504090 51.5176
## 5  11-mar    VOLUNTARY      NA      NA          2       1 -0.1933670 51.5404
## 6  11-mar STATE-FUNDED      NA      NA          2       1 -0.0769998 51.5453
##        y.Type
## 1   515074997
## 2 515,789,986
## 3 515,940,018
## 4  51,517,601
## 5 515,404,015
## 6 515,452,995

varias de las columnas del dataset no son significativas para el proceso que llevaremos adelante, por lo cual simplificaremos el data set seleccionando solo algunas

all_schools <- all_schools_base %>% 
  select(OBJECTID, SCHOOL_NAM, TYPE,    PHASE,  ADDRESS,    TOWN, STATUS,   GENDER, x,  y) %>%
  filter(!is.na(y))
head(all_schools)
##   OBJECTID                                         SCHOOL_NAM
## 1        1                        Ayesha Siddiqa Girls School
## 2        2                                 Beis Medrash Elyon
## 3        3                    Big Creative Independent School
## 4        4                             Wetherby Senior School
## 5        5 St Mary's Kilburn Church of England Primary School
## 6        6                         De Beauvoir Primary School
##                       TYPE          PHASE                  ADDRESS        TOWN
## 1 Other Independent School Not applicable     165-169 The Broadway    Southall
## 2 Other Independent School Not applicable 233 West Hendon Broadway      London
## 3 Other Independent School Not applicable       Silver Birch House Walthamstow
## 4 Other Independent School Not applicable      100 Marylebone Lane      London
## 5   Voluntary Aided School        Primary                Quex Road      London
## 6         Community School        Primary        80 Tottenham Road      London
##   STATUS GENDER          x       y
## 1   Open  Girls -0.3784960 51.5075
## 2   Open   Boys -0.2416280 51.5790
## 3   Open  Mixed -0.0425897 51.5940
## 4   Open   Boys -0.1504090 51.5176
## 5   Open  Mixed -0.1933670 51.5404
## 6   Open  Mixed -0.0769998 51.5453

Primer esquema

ggplot(all_schools)+
  geom_point(aes(x=x, y=y))

superponemos el mapa con los puntos

ggplot()+
  geom_sf(data=london_area, fill="gray75", color="white")+
  geom_point(data=all_schools, color="blue", aes(x=x, y=y))

Vemos mayor densidad de escuelas en el Inner London, la parte central, y algunas fuera de los límites de Londres. para poder identificarlas y procesar en función del distrito al que pertenecen necesitamos asociar ambos datasets

all_schools_geo <- st_as_sf(all_schools, coords = c("x","y"), crs = 4326) 
#transforma a formato geométrico, 
head(all_schools_geo)
## Simple feature collection with 6 features and 8 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -0.378496 ymin: 51.5075 xmax: -0.0425897 ymax: 51.594
## Geodetic CRS:  WGS 84
##   OBJECTID                                         SCHOOL_NAM
## 1        1                        Ayesha Siddiqa Girls School
## 2        2                                 Beis Medrash Elyon
## 3        3                    Big Creative Independent School
## 4        4                             Wetherby Senior School
## 5        5 St Mary's Kilburn Church of England Primary School
## 6        6                         De Beauvoir Primary School
##                       TYPE          PHASE                  ADDRESS        TOWN
## 1 Other Independent School Not applicable     165-169 The Broadway    Southall
## 2 Other Independent School Not applicable 233 West Hendon Broadway      London
## 3 Other Independent School Not applicable       Silver Birch House Walthamstow
## 4 Other Independent School Not applicable      100 Marylebone Lane      London
## 5   Voluntary Aided School        Primary                Quex Road      London
## 6         Community School        Primary        80 Tottenham Road      London
##   STATUS GENDER                   geometry
## 1   Open  Girls  POINT (-0.378496 51.5075)
## 2   Open   Boys   POINT (-0.241628 51.579)
## 3   Open  Mixed  POINT (-0.0425897 51.594)
## 4   Open   Boys  POINT (-0.150409 51.5176)
## 5   Open  Mixed  POINT (-0.193367 51.5404)
## 6   Open  Mixed POINT (-0.0769998 51.5453)

combinamos ambos datasets

all_schools_geo <- st_join(all_schools_geo, london_area)
## although coordinates are longitude/latitude, st_intersects assumes that they are planar
## although coordinates are longitude/latitude, st_intersects assumes that they are planar
head(all_schools_geo)
## Simple feature collection with 6 features and 12 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -0.378496 ymin: 51.5075 xmax: -0.0425897 ymax: 51.594
## Geodetic CRS:  WGS 84
##   OBJECTID                                         SCHOOL_NAM
## 1        1                        Ayesha Siddiqa Girls School
## 2        2                                 Beis Medrash Elyon
## 3        3                    Big Creative Independent School
## 4        4                             Wetherby Senior School
## 5        5 St Mary's Kilburn Church of England Primary School
## 6        6                         De Beauvoir Primary School
##                       TYPE          PHASE                  ADDRESS        TOWN
## 1 Other Independent School Not applicable     165-169 The Broadway    Southall
## 2 Other Independent School Not applicable 233 West Hendon Broadway      London
## 3 Other Independent School Not applicable       Silver Birch House Walthamstow
## 4 Other Independent School Not applicable      100 Marylebone Lane      London
## 5   Voluntary Aided School        Primary                Quex Road      London
## 6         Community School        Primary        80 Tottenham Road      London
##   STATUS GENDER           name cartodb_id          created_at
## 1   Open  Girls         Ealing          9 2015-07-01 06:57:45
## 2   Open   Boys         Barnet          2 2015-07-01 06:57:45
## 3   Open  Mixed Waltham Forest         31 2015-07-01 06:57:45
## 4   Open   Boys    Westminster         33 2015-07-01 06:57:45
## 5   Open  Mixed         Camden          6 2015-07-01 06:57:45
## 6   Open  Mixed        Hackney         12 2015-07-01 06:57:45
##            updated_at                   geometry
## 1 2015-07-01 06:57:45  POINT (-0.378496 51.5075)
## 2 2015-07-01 06:57:45   POINT (-0.241628 51.579)
## 3 2015-07-01 06:57:45  POINT (-0.0425897 51.594)
## 4 2015-07-01 06:57:45  POINT (-0.150409 51.5176)
## 5 2015-07-01 06:57:45  POINT (-0.193367 51.5404)
## 6 2015-07-01 06:57:45 POINT (-0.0769998 51.5453)

Debemos eliminar las escuelas que quedan fuera de el área de Londres

all_schools_geo <- filter(all_schools_geo, !is.na(name))
head(all_schools_geo)
## Simple feature collection with 6 features and 12 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -0.378496 ymin: 51.5075 xmax: -0.0425897 ymax: 51.594
## Geodetic CRS:  WGS 84
##   OBJECTID                                         SCHOOL_NAM
## 1        1                        Ayesha Siddiqa Girls School
## 2        2                                 Beis Medrash Elyon
## 3        3                    Big Creative Independent School
## 4        4                             Wetherby Senior School
## 5        5 St Mary's Kilburn Church of England Primary School
## 6        6                         De Beauvoir Primary School
##                       TYPE          PHASE                  ADDRESS        TOWN
## 1 Other Independent School Not applicable     165-169 The Broadway    Southall
## 2 Other Independent School Not applicable 233 West Hendon Broadway      London
## 3 Other Independent School Not applicable       Silver Birch House Walthamstow
## 4 Other Independent School Not applicable      100 Marylebone Lane      London
## 5   Voluntary Aided School        Primary                Quex Road      London
## 6         Community School        Primary        80 Tottenham Road      London
##   STATUS GENDER           name cartodb_id          created_at
## 1   Open  Girls         Ealing          9 2015-07-01 06:57:45
## 2   Open   Boys         Barnet          2 2015-07-01 06:57:45
## 3   Open  Mixed Waltham Forest         31 2015-07-01 06:57:45
## 4   Open   Boys    Westminster         33 2015-07-01 06:57:45
## 5   Open  Mixed         Camden          6 2015-07-01 06:57:45
## 6   Open  Mixed        Hackney         12 2015-07-01 06:57:45
##            updated_at                   geometry
## 1 2015-07-01 06:57:45  POINT (-0.378496 51.5075)
## 2 2015-07-01 06:57:45   POINT (-0.241628 51.579)
## 3 2015-07-01 06:57:45  POINT (-0.0425897 51.594)
## 4 2015-07-01 06:57:45  POINT (-0.150409 51.5176)
## 5 2015-07-01 06:57:45  POINT (-0.193367 51.5404)
## 6 2015-07-01 06:57:45 POINT (-0.0769998 51.5453)

Podemos agrupar y sumarizar para obtener información de la cantidad de escuelas por distrito

all_schools_geo_res <- all_schools_geo %>% 
  group_by(name) %>% 
  summarize(cantidad=n())
summary(all_schools_geo_res)
##                    name       cantidad               geometry 
##  Barking and Dagenham: 1   Min.   :  5.00   MULTIPOINT   :33  
##  Barnet              : 1   1st Qu.: 73.00   epsg:4326    : 0  
##  Bexley              : 1   Median : 87.00   +proj=long...: 0  
##  Brent               : 1   Mean   : 85.12                     
##  Bromley             : 1   3rd Qu.:100.00                     
##  Camden              : 1   Max.   :148.00                     
##  (Other)             :27

Ahora reconvertimos a all_schools_geo_res en un dataset no geométrico

all_schools_geo_res <- all_schools_geo_res %>% 
  st_set_geometry(NULL)
head(all_schools_geo_res)
## # A tibble: 6 x 2
##   name                 cantidad
##   <fct>                   <int>
## 1 Barking and Dagenham       53
## 2 Barnet                    148
## 3 Bexley                     75
## 4 Brent                      92
## 5 Bromley                   114
## 6 Camden                     92

podemos ahora rearmar london_area con la información de cantidad de escuelas que nos da all_schools_geo_res

london_area_full <- left_join(london_area, all_schools_geo_res, by="name")
head(london_area_full)
## Simple feature collection with 6 features and 5 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -0.333999 ymin: 51.2895 xmax: 0.225322 ymax: 51.67036
## Geodetic CRS:  WGS 84
##                   name cartodb_id          created_at          updated_at
## 1 Barking and Dagenham          1 2015-07-01 06:57:45 2015-07-01 06:57:45
## 2               Barnet          2 2015-07-01 06:57:45 2015-07-01 06:57:45
## 3               Bexley          3 2015-07-01 06:57:45 2015-07-01 06:57:45
## 4                Brent          4 2015-07-01 06:57:45 2015-07-01 06:57:45
## 5              Bromley          5 2015-07-01 06:57:45 2015-07-01 06:57:45
## 6               Camden          6 2015-07-01 06:57:45 2015-07-01 06:57:45
##   cantidad                       geometry
## 1       53 MULTIPOLYGON (((0.148209 51...
## 2      148 MULTIPOLYGON (((-0.183361 5...
## 3       75 MULTIPOLYGON (((0.158044 51...
## 4       92 MULTIPOLYGON (((-0.212138 5...
## 5      114 MULTIPOLYGON (((0.076463 51...
## 6       92 MULTIPOLYGON (((-0.140804 5...

representamos

ggplot(london_area_full)+
  geom_sf(aes(fill=cantidad))

Incluir un mapa de fondo

#install.packages(ggmap)
library(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
bbox_london <- as.numeric(st_bbox(london_area_full))
mapa_london <- get_stamenmap(bbox = bbox_london,
                     maptype = "terrain",
                     zoom=10)
## Source : http://tile.stamen.com/terrain/10/510/339.png
## Source : http://tile.stamen.com/terrain/10/511/339.png
## Source : http://tile.stamen.com/terrain/10/512/339.png
## Source : http://tile.stamen.com/terrain/10/510/340.png
## Source : http://tile.stamen.com/terrain/10/511/340.png
## Source : http://tile.stamen.com/terrain/10/512/340.png
## Source : http://tile.stamen.com/terrain/10/510/341.png
## Source : http://tile.stamen.com/terrain/10/511/341.png
## Source : http://tile.stamen.com/terrain/10/512/341.png
ggmap(mapa_london)

ggmap(mapa_london)+
  geom_sf(data=london_area_full, aes(fill=cantidad), alpha=0.75, inherit.aes = FALSE)+
  labs(title="Mapa Escuelas de Londres", 
       subtitle="cantidad por distrito",
       x="longitud",
       y="latitud")
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.