library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.4     v dplyr   1.0.2
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.0
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(mosaic)
## Registered S3 method overwritten by 'mosaic':
##   method                           from   
##   fortify.SpatialPolygonsDataFrame ggplot2
## 
## The 'mosaic' package masks several functions from core packages in order to add 
## additional features.  The original behavior of these functions should not be affected by this.
## 
## Attaching package: 'mosaic'
## The following object is masked from 'package:Matrix':
## 
##     mean
## The following objects are masked from 'package:dplyr':
## 
##     count, do, tally
## The following object is masked from 'package:purrr':
## 
##     cross
## The following object is masked from 'package:ggplot2':
## 
##     stat
## The following objects are masked from 'package:stats':
## 
##     binom.test, cor, cor.test, cov, fivenum, IQR, median, prop.test,
##     quantile, sd, t.test, var
## The following objects are masked from 'package:base':
## 
##     max, mean, min, prod, range, sample, sum
library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(rgdal)
## Loading required package: sp
## rgdal: version: 1.5-18, (SVN revision 1082)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.0.4, released 2020/01/28
## Path to GDAL shared files: C:/Users/kiera/OneDrive - University of Bristol/Documents/R/win-library/4.0/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 6.3.1, February 10th, 2020, [PJ_VERSION: 631]
## Path to PROJ shared files: C:/Users/kiera/OneDrive - University of Bristol/Documents/R/win-library/4.0/rgdal/proj
## Linking to sp version:1.4-4
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading rgdal.
## 
## Attaching package: 'rgdal'
## The following object is masked from 'package:mosaic':
## 
##     project
library(tmap)
IMD2019 = read_csv("NewcastleUponTyne.csv")
## 
## -- Column specification --------------------------------------------------------
## cols(
##   Code = col_character(),
##   Region = col_character(),
##   CodeRegion = col_character(),
##   SCORE = col_double(),
##   Decile = col_double()
## )
LTHP = read_csv("long-term_health_problem_or_disability_(LTHP).csv")
## 
## -- Column specification --------------------------------------------------------
## cols(
##   Name = col_character(),
##   LSOA = col_character(),
##   LTHP = col_double(),
##   Perc_LTHP = col_double()
## )
NOCAR = read_csv("no_car_data.csv")
## 
## -- Column specification --------------------------------------------------------
## cols(
##   Name = col_character(),
##   LSOA = col_character(),
##   NoCars = col_double(),
##   Perc_NoCars = col_double()
## )
favstats(~SCORE,data=IMD2019)
##  min   Q1 median       Q3   max     mean      sd   n missing
##   32 3120  10560 22504.75 32714 12921.83 10612.5 166       0
round(favstats(~SCORE,data=IMD2019),digits=1)
##  min   Q1 median      Q3   max    mean      sd   n missing
##   32 3120  10560 22504.8 32714 12921.8 10612.5 166       0
gf_histogram(~SCORE,data=IMD2019,title="IMD Scores Newcastle")

gf_dhistogram(~SCORE,data=IMD2019,title="IMD Scores Newcastle") %>% gf_dens(color="red")

round(favstats(~Perc_NoCars,data=NOCAR),digits=1)
##  min Q1 median   Q3  max mean   sd   n missing
##  5.3 25   39.4 55.2 78.3 40.5 18.7 175       0
gf_dhistogram(~Perc_NoCars,data=NOCAR,title="Percentage Households with No Car",) %>% gf_dens(color = "red") %>% gf_labs(x="Percentage without Cars")

stem(NOCAR$Perc_NoCars)
## 
##   The decimal point is 1 digit(s) to the right of the |
## 
##   0 | 55899
##   1 | 22333444
##   1 | 55566778888888999
##   2 | 0011222333444
##   2 | 556666777779999
##   3 | 0011223344
##   3 | 55666677777888899999
##   4 | 01122222233444
##   4 | 5566666677888999
##   5 | 00000011122
##   5 | 5556667778999
##   6 | 0001123334
##   6 | 555567788
##   7 | 0222244
##   7 | 5556678
NEWCASTLE = read_sf("england_lsoa_2011.shp")
plot(NEWCASTLE["name"])

head(IMD2019)
## # A tibble: 6 x 5
##   Code      Region                   CodeRegion SCORE Decile
##   <chr>     <chr>                    <chr>      <dbl>  <dbl>
## 1 E01008288 Newcastle upon Tyne 027A E08000021   9092      3
## 2 E01008289 Newcastle upon Tyne 027B E08000021   1985      1
## 3 E01008290 Newcastle upon Tyne 027C E08000021   1567      1
## 4 E01008291 Newcastle upon Tyne 027D E08000021    137      1
## 5 E01008292 Newcastle upon Tyne 027E E08000021  26799      9
## 6 E01008293 Newcastle upon Tyne 004A E08000021  15011      5
head(NEWCASTLE)
## Simple feature collection with 6 features and 3 fields
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: 419707.3 ymin: 563103.6 xmax: 426470.8 ymax: 571146
## projected CRS:  OSGB 1936 / British National Grid
## # A tibble: 6 x 4
##   label         name         code                                       geometry
##   <chr>         <chr>        <chr>                                 <POLYGON [m]>
## 1 E08000021E02~ Newcastle u~ E01033~ ((425400 564505, 425421 564502, 425444 564~
## 2 E08000021E02~ Newcastle u~ E01033~ ((425276 564514.2, 425273.3 564494.7, 4252~
## 3 E08000021E02~ Newcastle u~ E01033~ ((424797 564019.3, 424797.1 564018.3, 4247~
## 4 E08000021E02~ Newcastle u~ E01033~ ((420966.4 564478.5, 420966.1 564469.5, 42~
## 5 E08000021E02~ Newcastle u~ E01033~ ((426198.4 564384.2, 426193 564323.5, 4261~
## 6 E08000021E02~ Newcastle u~ E01033~ ((423911 571142, 423927 571131, 423935.6 5~
NEWCASTLE = merge(NEWCASTLE,IMD2019,by.x="code",by.y="Code")
names(NEWCASTLE)
## [1] "code"       "label"      "name"       "Region"     "CodeRegion"
## [6] "SCORE"      "Decile"     "geometry"
qtm(NEWCASTLE,fill="SCORE",title="IMD SCORES")

qtm(NEWCASTLE,fill="SCORE",title="IMD SCORES", format="NLD_wide",style="classic")

CENSUS = merge(LTHP,NOCAR)
head(CENSUS)
##                       Name      LSOA LTHP Perc_LTHP NoCars Perc_NoCars
## 1 Newcastle upon Tyne 001A E01008294  115      18.6    120        19.4
## 2 Newcastle upon Tyne 001B E01008306  219      30.4    152        21.1
## 3 Newcastle upon Tyne 001C E01008307  140      28.0    190        38.0
## 4 Newcastle upon Tyne 001D E01008308  166      26.9    177        28.7
## 5 Newcastle upon Tyne 001E E01008311  124      18.0     91        13.2
## 6 Newcastle upon Tyne 001F E01008312  101      20.1     81        16.1
NEWCASTLE = merge(NEWCASTLE,CENSUS,by.x="code",by.y="LSOA")
qtm(NEWCASTLE,fill="Perc_NoCars",title="NO-CAR HOUSEHOLDS", format="NLD_wide",style="classic")

qtm(NEWCASTLE,fill="Perc_NoCars",fill.style="jenks",fill.n=5,title="NO-CAR HOUSEHOLDS", title.position=c("right","TOP"),format="NLD_wide",style="classic") + tm_compass(north=0,type="arrow",position=c("right","top")) + tm_scale_bar(position=c("left","bottom"))

tm_shape(NEWCASTLE) + tm_fill("SCORE",style="jenks",n=5) + tm_borders() + tm_layout(title="IMD SCORES", title.position=c("centre","TOP")) + tm_legend(legend.format=list(digits=1)) + tm_compass(north=0,type="arrow",position=c("right","top")) + tm_scale_bar(position=c("right","bottom"))