-
Notifications
You must be signed in to change notification settings - Fork 0
/
censusDataLookz.R
2675 lines (2002 loc) · 100 KB
/
censusDataLookz.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Look at the five and three census data, see what we have.
geolibs <- c("ggplot2","RColorBrewer","spdep","ggmap","rgdal","rgeos","maptools","dplyr","tidyr","tmap","raster", "dplyr", "tidyr","assertthat",
"data.table","pryr","geoR","plyr","data.table")
lapply(geolibs, require, character.only = TRUE)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Five census CoB----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Load all the CoB 5-census data
fiveCensus71 <- readShapeSpatial("C:/Data/Census/StitchOutputs/Scotland/fiveCensus_to2011_IZs_raw/CountryOfBirth/1971_CoB_from_71EDs_to_2011_IntermediateGeog.shp")
fiveCensus81 <- readShapeSpatial("C:/Data/Census/StitchOutputs/Scotland/fiveCensus_to2011_IZs_raw/CountryOfBirth/1981_CoB_from_81EDs_to_2011_IntermediateGeog.shp")
fiveCensus91 <- readShapeSpatial("C:/Data/Census/StitchOutputs/Scotland/fiveCensus_to2011_IZs_raw/CountryOfBirth/1991_CoB_from_01OAs_to_2011_IntermediateGeog.shp")
fiveCensus01 <- readShapeSpatial("C:/Data/Census/StitchOutputs/Scotland/fiveCensus_to2011_IZs_raw/CountryOfBirth/2001_CoB_from_2001OAs_to_2011_IntermediateGeog.shp")
fiveCensus11 <- readShapeSpatial("C:/Data/Census/StitchOutputs/Scotland/fiveCensus_to2011_IZs_raw/CountryOfBirth/2011_CoB_IntermediateGeog_via_lookupFromOAs.shp")
fc71_df <- data.frame(fiveCensus71)
#A lot of columns we can drop from all of those. Might as well do that when they're merged.
allFiveList <- c(fiveCensus71, fiveCensus81, fiveCensus91, fiveCensus01, fiveCensus11)
#add column marking which census
yrz <- seq(1971,2011,by = 10)
#Dunno why the lapply doesn't work...
#lapply(seq(1:5), function(x) allFiveList[[x]]@data$year <- yrz[x])
for(x in 1:5){
allFiveList[[x]]@data$year <- yrz[x]
}
#test <- data.frame(allFiveList[[2]])]
allFive <- do.call(rbind,c(allFiveList,makeUniqueIDs = T))
#Tick!
test <- data.frame((allFive))
#Give the countries back some slightly more sensible names
orignames <- names(allFive)
#From the CoB stitching code for the 5 censuses
countryNames <- c(
'England',
'Scotland',
'Wales',
'Rest of UK',
'Irish Republic',
'Old Commonwealth',
'Africa (New-C)',
'India',
'Pakistan',
'Other Europe',
'SE Asia New C',
'Caribbean New C',
'New Commonwealth other',
'Rest of World'
)
names(allFive) <- c(orignames[1:11],countryNames,orignames[26])
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Look at total CoB cats for each decade, see how they change.-----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Sum per decade
allFive_df <- data.frame(allFive)
fiveC_decadeSums <- allFive_df %>%
dplyr::select(England:year) %>%
group_by(year) %>%
summarise_each(funs(sum))
#Long by CoB
fiveC_decadeSumsLong <- fiveC_decadeSums %>% gather(CoB, count, England:Rest.of.World)
#Colours from http://stackoverflow.com/questions/21352683/randomising-qualitative-colours-for-large-sets-in-ggplot
cbPalette <- c("#89C5DA", "#DA5724", "#74D944", "#CE50CA", "#3F4921", "#C0717C", "#CBD588", "#5F7FC7",
"#673770", "#D3D93E", "#38333E", "#508578", "#D7C1B1", "#689030", "#AD6F3B", "#CD9BCD",
"#D14285", "#6DDE88", "#652926", "#7FDCC0", "#C84248", "#8569D5", "#5E738F", "#D1A33D",
"#8A7C64", "#599861")
#http://www.cse.unsw.edu.au/~mike/myrlibrary.old/RColorBrewer/html/ColorBrewer.html
cbPalette <- brewer.pal(12,"Paired")
#Re-order based on max for all years
reodz <- fiveC_decadeSumsLong %>% group_by(CoB) %>%
mutate(av = max(count))
#Christ knows why mutate turns it into a list. This took far too long to work out. I hate reordering!
fiveC_decadeSumsLong$CoB <- reorder(fiveC_decadeSumsLong$CoB,-unlist(reodz[,4]))
#output <- ggplot(fiveC_decadeSumsLong, aes(x = year, y = count, colour = CoB)) +
# output <- ggplot(fiveC_decadeSumsLong %>% filter(CoB != "Scotlnd"), aes(x = year, y = count, colour = CoB)) +
#Log?
output <- ggplot(fiveC_decadeSumsLong %>% filter(CoB != "Scotland", CoB != "England"), aes(x = year, y = count, colour = CoB)) +
#geom_line(size =1 )
geom_line(size =1.5, alpha = 0.3) +
geom_point(size = 6.5, colour = "#595959") +
geom_point(size = 6) +
#scale_y_log10() + ylab("count (log 10)") +
scale_colour_manual(values=cbPalette)
output
#ggsave("R_outputs/5census_countNoScotsEngLog.png",output,width = 7,height = 4)
ggsave("R_outputs/5census_countNoScotsEng.png",output,width = 7,height = 4)
#~~~~~~~~~~~~~~
#Drop other europe, order appropriately...
#nooo, don't need to. Removing them leaves the same underlying order for the remainder, obv.
# reodz <- fiveC_decadeSumsLong %>% group_by(CoB) %>%
# mutate(av = max(count))
#
# #Christ knows why mutate turns it into a list. This took far too long to work out. I hate reordering!
# fiveC_decadeSumsLong$CoB <- reorder(fiveC_decadeSumsLong$CoB,-unlist(reodz[,4]))
output <- ggplot(fiveC_decadeSumsLong %>% filter(!(CoB %in% c("Scotland","England","Other.Europe"))),
aes(x = year, y = count, colour = CoB)) +
#geom_line(size =1 )
geom_line(size =1.5, alpha = 0.3) +
geom_point(size = 6.5, colour = "#595959") +
geom_point(size = 6) +
#scale_y_log10() + ylab("count (log 10)") +
#To match previous colours
scale_colour_manual(values=cbPalette[2:12])
output
ggsave("R_outputs/5census_countNoScotsEngRestOFEurope.png",output,width = 7,height = 4)
#~~~~~~~~~~~~~~
#Run again for first three decades
output <- ggplot(fiveC_decadeSumsLong %>% filter(!(year %in% c(2001,2011)),CoB != "Scotland", CoB != "England"), aes(x = year, y = count, colour = CoB)) +
#geom_line(size =1 )
geom_line(size =1.5, alpha = 0.3) +
geom_point(size = 6.5, colour = "#595959") +
geom_point(size = 6) +
#scale_y_log10() + ylab("count (log 10)") +
scale_colour_manual(values=cbPalette)
output
#ggsave("R_outputs/5census_countNoScotsEngLog.png",output,width = 7,height = 4)
ggsave("R_outputs/5census_first3_countNoScotsEng.png",output,width = 7,height = 4)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Five census econ active--------------
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Load all the CoB 5-census data
fiveCensus71_EA <- readShapeSpatial("C:/Data/Census/StitchOutputs/Scotland/fiveCensus_to2011_IZs_raw/Employment/1971_econActive_from_71EDs_to_2011_IntermediateGeog.shp")
fiveCensus81_EA <- readShapeSpatial("C:/Data/Census/StitchOutputs/Scotland/fiveCensus_to2011_IZs_raw/Employment/1981_econActive_from_81EDs_to_2011_IntermediateGeog.shp")
fiveCensus91_EA <- readShapeSpatial("C:/Data/Census/StitchOutputs/Scotland/fiveCensus_to2011_IZs_raw/Employment/1991_econActive_from_91OAs_to_2011_IntermediateGeog.shp")
fiveCensus01_EA <- readShapeSpatial("C:/Data/Census/StitchOutputs/Scotland/fiveCensus_to2011_IZs_raw/Employment/2001_econActive_from_01OAs_to_2011_IntermediateGeog.shp")
fiveCensus11_EA <- readShapeSpatial("C:/Data/Census/StitchOutputs/Scotland/fiveCensus_to2011_IZs_raw/Employment/2011_econActive_IntermediateGeog_via_lookupFromOAs.shp")
#fc71_df <- data.frame(fiveCensus71)
#A lot of columns we can drop from all of those. Might as well do that when they're merged.
allFiveList_EA <- c(fiveCensus71_EA, fiveCensus81_EA, fiveCensus91_EA, fiveCensus01_EA, fiveCensus11_EA)
lapply(allFiveList_EA,names)
#Get same columns
allFiveList_EA[1:4] <- lapply(allFiveList_EA[1:4],function(x) x[,c(1,12:14)])
#EA and unempl wrong way round in 71
allFiveList_EA[[1]] <- allFiveList_EA[[1]][,c(1,3,2,4)]
#add column marking which census
yrz <- seq(1971,2011,by = 10)
#Dunno why the lapply doesn't work...
#lapply(seq(1:5), function(x) allFiveList[[x]]@data$year <- yrz[x])
for(x in 1:5){
allFiveList_EA[[x]]@data$year <- yrz[x]
}
#test <- data.frame(allFiveList[[2]])]
allFive_EA <- do.call(rbind,c(allFiveList_EA,makeUniqueIDs = T))
#Tick!
allFive_EA_df <- data.frame((allFive_EA))
#~~~~~~~~~~~~~~
#Check between-census EA correlations
percentEmpWide <- do.call(cbind,
list(allFive_EA_df$prcntEm[allFive_EA_df$year==1971],
allFive_EA_df$prcntEm[allFive_EA_df$year==1981],
allFive_EA_df$prcntEm[allFive_EA_df$year==1991],
allFive_EA_df$prcntEm[allFive_EA_df$year==2001],
allFive_EA_df$prcntEm[allFive_EA_df$year==2011]
)
) %>% data.frame()
#invert: wanna see % UNemployed
percentEmpWide <- 100 - percentEmpWide
names(percentEmpWide) <- c('1971',
'1981',
'1991',
'2001',
'2011')
pairs((percentEmpWide))
#Reckon I just wanna see particular ones
plot(percentEmpWide$`1971`,percentEmpWide$`1981`)
plot(percentEmpWide$`1981`,percentEmpWide$`1991`)
plot(percentEmpWide$`1991`,percentEmpWide$`2001`)
plot(percentEmpWide$`2001`,percentEmpWide$`2011`)
#Back to original: think I can do lag between years with one thingyo
#Need the lag to be by place, silly person. Year should be in correct order
#Plus drop 71, no lag possible
lagz <- allFive_EA_df %>%
arrange(interzn, year) %>%
group_by(interzn) %>%
mutate(change = prcntEm - lag(prcntEm)) %>%
filter(year != 1971)
ggplot() +
geom_boxplot(data = lagz, aes(x = factor(year), y = change))
#save to look in QGIS
#Different column for each year's change
lagz4gis <- lagz %>% dplyr::select(interzn,year,change) %>%
spread(year, change)
#check zones are in same order. Ah, newp! Yeah, I arranged remember? Will need to merge in.
fiveCensus11_EA@data$interzn == lagz4gis$interzn
useThis <- fiveCensus11_EA
useThis@data <- merge(useThis@data,lagz4gis,by = 'interzn')
useThisdf <- data.frame(useThis)
fiveCensus11_EA_df <- data.frame(fiveCensus11_EA)
writeSpatialShape(useThis, 'QGIS/temp/employment5censusChange.shp')
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Three census CoB--------------
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
threeCensus91 <- readShapeSpatial("C:/Data/Census/StitchOutputs/Scotland/LBS_postcodeSector_3Census_raw/CountryOfBirth/1991_CountryOfBirthRecode_91LBS_noZeroPCS_straightMatch.shp")
threeCensus01 <- readShapeSpatial("C:/Data/Census/StitchOutputs/Scotland/LBS_postcodeSector_3Census_raw/CountryOfBirth/2001_CountryOfBirthRecode_91LBS_noZeroPCS.shp")
threeCensus11 <- readShapeSpatial("C:/Data/Census/StitchOutputs/Scotland/LBS_postcodeSector_3Census_raw/CountryOfBirth/2011_CountryOfBirthRecode_91LBS_noZeroPCS.shp")
#tc71_df <- data.frame(fiveCensus71)
#Accidentally added two middle other other cols. Drop second ones.
#Also make equal length and make cols same names
names(threeCensus91)
names(threeCensus01)
names(threeCensus11)
#Update: I appear to have fixed it. Did I? Yup!
table(names(threeCensus91)==names(threeCensus01))
table(names(threeCensus11)==names(threeCensus01))
#So no longer need all this
# threeCensus91 <- threeCensus91[,c(1:11,13:42)]
# threeCensus01 <- threeCensus01[,c(2:12,14:43)]
# threeCensus11 <- threeCensus11[,c(2:12,14:43)]
#We know they were in the same order
#names(threeCensus91) <- names(threeCensus01)
all3List <- c(threeCensus91, threeCensus01, threeCensus11)
#add column marking which census
yrz <- seq(1991,2011,by = 10)
#Dunno why the lapply doesn't work...
#lapply(seq(1:5), function(x) allthreeList[[x]]@data$year <- yrz[x])
for(x in 1:3){
all3List[[x]]@data$year <- yrz[x]
}
#test <- data.frame(allFiveList[[2]])]
all3 <- do.call(rbind,c(all3List,makeUniqueIDs = T))
#Tick!
test <- data.frame((all3))
#Save! I keep on having to do the above...
saveRDS(all3,'R_data/all3.rds')
all3_df <- data.frame(all3)
saveRDS(all3_df,'R_data/all3_df.rds')
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#3 Census CoB: across zone shares----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Question: doesn't using across-zone shares make it autocorrelate to zone pop?
#Avoiding prop.table, use mutate_each to get per column proportions for each group.
#Add in total pop column
all3_df$totalPop <- apply(all3_df[,c(4:42)],1,sum)
#It'll annoy me the year col being in the wrong position
all3_df <- all3_df[,c(1:42,44,43)]
all3_df_shares <- all3_df %>%
dplyr::select(4:44) %>%
group_by(year) %>%
#mutate(prop = prop.table(.,margin = 2))#NONONO!
#mutate_each( funs( ((.)/sum(.))*100 ), var = c(4:43) )#YESYESYES
mutate_each( funs( ((.)/sum(.))*100 ) )#YESYESYES
#pre-selecting variables (for some reason) replaces originals
#with the proportions and keeps the correct column names
#So just add back in the zone references
all3_df_shares <- cbind(all3_df[,c(1:3)],all3_df_shares)
#Did that work? Yup!
apply(all3_df_shares[all3_df$year==2011,c(4:43)],2,sum)
#~~~~~~~~~~~~~~~~~~~~
#Now: I think all of them are just going to correlate with total pop per zone, aren't they?
#So pick some examples
corz <- all3_df_shares[all3_df_shares$year %in% c(1991,2011),c("Irish_Repu","totalPop","year")]
corz <- all3_df_shares[all3_df_shares$year %in% c(1991,2011),c("Pakistan","totalPop","year")]
corz <- all3_df_shares[all3_df_shares$year %in% c(1991,2011),c("England","totalPop","year")]
#corzwide <- dcast(corz,Irish_Repu+totalPop~year)
#And what if I just keep the top number? What's the correlation to pop then?
#Say, top 50
corztop <- corz %>%
group_by(year) %>%
top_n(n = 100,wt = Irish_Repu)
corztop <- corz
#**** **
#Annoying failure to use any widening function
# corzwide <- do.call(cbind,list(corz[corz$year==1991,c(1:2)],
# corz[corz$year==2011,c(1:2)]))
corzwide <- do.call(cbind,list(corztop[corztop$year==1991,c(1:2)],
corztop[corztop$year==2011,c(1:2)]))
#names(corzwide) <- c('Irish_Repu91','totalPop91','Irish_Repu11','totalPop11')
names(corzwide) <- c('cob91','totalPop91','cob11','totalPop11')
pairs((corzwide))
pairs(log(corzwide))
tst <- lm(cob11~totalPop91,corzwide)
summary(tst)
tst2 <- lm(cob11~cob91,corzwide)
summary(tst2)
tst3 <- lm(cob11~cob91+totalPop91,corzwide)
summary(tst3)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Look at total CoB cats for each decade, see how they change.-----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Sum per decade
all3_df <- data.frame(all3)
threeC_decadeSums <- all3_df %>%
dplyr::select(Channel_Is:year) %>%
group_by(year) %>%
summarise_each(funs(sum))
#Random colours
#http://stackoverflow.com/questions/21352683/randomising-qualitative-colours-for-large-sets-in-ggplot
cols = rainbow(40, s=.6, v=.9)[sample(1:40,40)]
#Long by CoB
threeC_decadeSumsLong <- threeC_decadeSums %>% gather(CoB, count, Channel_Is:Iran)
#Order by largest group in 2011 (3rd row in threeC_decadeSums)
levels(threeC_decadeSumsLong$CoB)
reodz <- threeC_decadeSumsLong %>% group_by(CoB) %>%
mutate(av = max(count))
#Christ knows why mutate turns it into a list. This took far too long to work out. I hate reordering!
threeC_decadeSumsLong$CoB <- reorder(threeC_decadeSumsLong$CoB,-unlist(reodz[,4]))
#Label quintiles: CoB groups in each year
threeC_decadeSumsLong <- threeC_decadeSumsLong %>% group_by(year) %>%
mutate(CoB_groups = as.numeric(cut_number(count,4)))
#pick median value, apply to all of each CoB across years
#(Or same CoBs won't appear across all years)
threeC_decadeSumsLong <- threeC_decadeSumsLong %>% group_by(CoB) %>%
mutate(CoB_groupsMedian = median(CoB_groups))
#threeC_decadeSumsLong$CoB %>% length
#reodz[,4] %>% length
#threeC_decadeSumsLong$CoB <- factor(threeC_decadeSumsLong$CoB, threeC_decadeSumsLong$count[])
# output <- ggplot(threeC_decadeSumsLong, aes(x = year, y = count, colour = CoB)) +
# output <- ggplot(threeC_decadeSumsLong %>% filter(CoB != "Scotlnd"), aes(x = year, y = count, colour = CoB)) +
# output <- ggplot(threeC_decadeSumsLong %>% filter(CoB != "Scotland", CoB != "England"), aes(x = year, y = count, colour = CoB)) +
# geom_line(size =1, alpha = 0.3) +
# geom_point(size = 2) +
# scale_colour_manual(values=cols)
#
# output
for(i in 1:max(threeC_decadeSumsLong$CoB_groupsMedian)){
output <- ggplot(threeC_decadeSumsLong
#%>% filter(CoB != "Scotland", CoB != "England", CoB_groupsMedian == i),
%>% filter(CoB_groupsMedian == i),
aes(x = year, y = count, colour = CoB)) +
geom_line(size =1.5, alpha = 0.3) +
geom_point(size = 4.5, colour = "#595959") +
geom_point(size = 4) +
#scale_y_log10() + ylab("count (log 10)") +
scale_colour_manual(values=cbPalette)
output
#ggsave("R_outputs/3census_noEngScot_log.png",output,width = 9,height = 5)
ggsave(paste0("R_outputs/3census_quarters_incEngScot",i,".png"),output,width = 9,height = 5)
}
#FACET
output <- ggplot(threeC_decadeSumsLong,
#%>% filter(CoB != "Scotland", CoB != "England", CoB_groupsMedian == i),
#%>% filter(CoB_groupsMedian == i),
aes(x = year, y = count, colour = CoB)) +
geom_line(size =1.5, alpha = 0.3) +
geom_point(size = 4.5, colour = "#595959") +
geom_point(size = 4) +
facet_wrap(~CoB_groupsMedian)
#scale_y_log10() + ylab("count (log 10)") +
#scale_colour_manual(values=cbPalette)
output
#ggsave("R_outputs/3census_noEngScot_log.png",output,width = 9,height = 5)
ggsave(paste0("R_outputs/3census_quarters_incEngScotFacet.png"),output,width = 9,height = 5)
# #Look at just individual countries that match across all three
#
# #(produced in LBS_3Census_CoB_stitchingCategories.R)
# #Get just names from foundMatches. First index
# singleCountryNames <- sapply(foundMatches, function(x) x[[1]][1])
#
# #Not the same fecking list. Good good. Manual then.
# singleCountryNames[4] <- unique(threeC_decadeSumsLong$CoB)[16] %>% as.character
# singleCountryNames[7] <- unique(threeC_decadeSumsLong$CoB)[19] %>% as.character
# singleCountryNames[14] <- unique(threeC_decadeSumsLong$CoB)[26] %>% as.character
# singleCountryNames[26] <- unique(threeC_decadeSumsLong$CoB)[38] %>% as.character
#
# output <- ggplot(threeC_decadeSumsLong %>%
# #filter(CoB %in% singleCountryNames[3:27]),
# filter(!CoB %in% singleCountryNames),
# aes(x = year, y = count, colour = CoB)) +
# geom_line(size =1.5, alpha = 0.3) +
# geom_point(size = 4.5, colour = "#595959") +
# geom_point(size = 4) +
# #scale_y_log10() + ylab("count (log 10)") +
# scale_colour_manual(values=cols)
#
# output
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Just Scot / Eng / other----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fiveScotEngOther <- cbind(fiveC_decadeSums[,c(1:3)], data.frame(restOfWorld = apply(fiveC_decadeSums[,c(4:15)],1,sum)))
fiveScotEngOther_Long <- fiveScotEngOther %>% gather(CoB, count, England:restOfWorld)
fiveScotEngOther_Long$CoB <- factor(fiveScotEngOther_Long$CoB, levels = c('Scotlnd','England','restOfWorld'))
#http://www.cse.unsw.edu.au/~mike/myrlibrary.old/RColorBrewer/html/ColorBrewer.html
cbPalette <- brewer.pal(3,"Paired")
#cbPalette <- brewer.pal(3,"BrBG")
#output <- ggplot(fiveC_decadeSumsLong, aes(x = year, y = count, colour = CoB)) +
# output <- ggplot(fiveC_decadeSumsLong %>% filter(CoB != "Scotlnd"), aes(x = year, y = count, colour = CoB)) +
output <- ggplot(fiveScotEngOther_Long, aes(x = year, y = count, colour = CoB)) +
#geom_line(size =1 )
geom_line(size =1.5, alpha = 0.3) +
geom_point(size = 7) +
scale_colour_manual(values=cbPalette)
output
ggsave("R_outputs/5census_engScotRest_count.png",output,width = 7,height = 4)
#~~~~~~~~~~~~~~~~~~~~~~~
#Proportions for those three
propz <- prop.table(as.matrix(fiveScotEngOther[,c(2:4)]), margin = 1) * 100
propz <- cbind(fiveScotEngOther[,1,drop = F],data.frame(propz))
fiveScotEngOther_Long <- propz %>% gather(CoB, percent, England:restOfWorld)
fiveScotEngOther_Long$CoB <- factor(fiveScotEngOther_Long$CoB, levels = c('Scotlnd','England','restOfWorld'))
#http://www.cse.unsw.edu.au/~mike/myrlibrary.old/RColorBrewer/html/ColorBrewer.html
cbPalette <- brewer.pal(3,"Paired")
#cbPalette <- brewer.pal(3,"BrBG")
#output <- ggplot(fiveC_decadeSumsLong, aes(x = year, y = count, colour = CoB)) +
# output <- ggplot(fiveC_decadeSumsLong %>% filter(CoB != "Scotlnd"), aes(x = year, y = count, colour = CoB)) +
output <- ggplot(fiveScotEngOther_Long, aes(x = year, y = percent, colour = CoB)) +
#geom_line(size =1 )
geom_line(size =1.5, alpha = 0.3) +
geom_point(size = 7) +
scale_colour_manual(values=cbPalette) +
#geom_text(aes(label = paste0(round(percent,1),"%"), y = percent), size = 3, colour="black")
#geom_text(aes(label = paste0(round(percent,1),"%"), y = (1.1*percent)), size = 3, colour="black")
geom_text(aes(label = paste0(round(percent,1),"%"),
y = (ifelse(percent > 25,percent,1.3*percent))), size = 3, colour="black")
output
ggsave("R_outputs/5census_engScotRest_percent.png",output,width = 7,height = 4)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Get 3 census economically active data------
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Load all the CoB 3-census data
threeCensus91_EA <- readShapeSpatial("C:/Data/Census/StitchOutputs/Scotland/LBS_postcodeSector_3Census_raw/Employment/1991_econActive_91LBS_noZeroPCS_straightMatch.shp")
threeCensus01_EA <- readShapeSpatial("C:/Data/Census/StitchOutputs/Scotland/LBS_postcodeSector_3Census_raw/Employment/2001_econActive_91LBS_noZeroPCS.shp")
threeCensus11_EA <- readShapeSpatial("C:/Data/Census/StitchOutputs/Scotland/LBS_postcodeSector_3Census_raw/Employment/2011_econActive_91LBS_noZeroPCS.shp")
#fc71_df <- data.frame(threeCensus71)
#A lot of columns we can drop from all of those. Might as well do that when they're merged.
allThreeList_EA <- c(threeCensus91_EA, threeCensus01_EA, threeCensus11_EA)
lapply(allThreeList_EA,names)
#Get same columns
allThreeList_EA[2:3] <- lapply(allThreeList_EA[2:3],function(x) x[,c(2,4:6)])
#EA and unempl wrong way round in 91
allThreeList_EA[[1]] <- allThreeList_EA[[1]][,c(1,4,3,5)]
#And the names are wrong
names(allThreeList_EA[[1]]) <- c('label','EA','Unempl','percentEmp')
#add column marking which census
yrz <- seq(1991,2011,by = 10)
#Dunno why the lapply doesn't work...
#lapply(seq(1:5), function(x) allThreeList[[x]]@data$year <- yrz[x])
for(x in 1:3){
allThreeList_EA[[x]]@data$year <- yrz[x]
}
#test <- data.frame(allThreeList[[2]])]
allThree_EA <- do.call(rbind,c(allThreeList_EA,makeUniqueIDs = T))
#Tick!
test <- data.frame((allThree_EA))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Get 3 census house price data, correlate to employment------
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Oh. Mostly already in the right shape?
house <- readRDS("Housing/1991to2001_twoYearBandMeanPrices_1991_PCSnoZeroes.rds")
#Aggregate: mean price by PCS zone
houseMeanz <- house %>% group_by(censusYear,label) %>%
summarise(meanPrice = mean(priceFinal))
#to match EA
houseMeanz$censusYear <- as.numeric(houseMeanz$censusYear)
#Couple of zones without house prices. Going to be 91 again - no houses there.
#Combine with employment levels for three censuses
allThree_EA_df <- data.frame(allThree_EA)
house_n_EA <- merge(houseMeanz,allThree_EA_df, by.x=c('label','censusYear'),by.y = c('label','year'), all.y = T)
names(house_n_EA)[names(house_n_EA)=="censusYear"] <- "year"
#Basic per-census corr
#Free scale
output <- ggplot(house_n_EA, aes(x = percentEmp, y = meanPrice)) +
geom_point() +
facet_wrap(~year, scales = 'free')
output
#Same scale
output <- ggplot(house_n_EA, aes(x = percentEmp, y = meanPrice)) +
geom_point() +
facet_wrap(~year)
output
#Break employment % into quintiles, mark
house_n_EA_q <- house_n_EA %>% group_by(year) %>%
mutate(percentEmp_quintiles = as.numeric(cut_number(percentEmp, 5)))
#Apply quintiles from first decade to the rest. Slight faff involved here...
first <- house_n_EA_q %>% dplyr::filter(year == 1991)
second <- house_n_EA_q %>% dplyr::filter(year == 2001)
third <- house_n_EA_q %>% dplyr::filter(year == 2011)
#labels are in same order, so...
second$percentEmp_quintiles <- first$percentEmp_quintiles
third$percentEmp_quintiles <- first$percentEmp_quintiles
#allz <- c(first,second,third)
#WHY NOT???
#house_n_EA_q2 <- do.call(rbind,c(allz,makeUniqueIDs = T))
house_n_EA_q2 <- rbind(first,second) %>% rbind(third)
house_n_EA_q2$percentEmp_quintiles <- factor(house_n_EA_q2$percentEmp_quintiles)
names(house_n_EA_q2)[names(house_n_EA_q2)=="percentEmp_quintiles"] <- "empl5"
#Same scale
output <- ggplot(house_n_EA_q2, aes(x = percentEmp, y = meanPrice,
#colour = factor(percentEmp_quintiles), shape = factor(percentEmp_quintiles))) +
colour = empl5, shape = empl5)) +
#facet_wrap(~year) +
facet_wrap(~year, scales = 'free') +
#facet_wrap(~year, scales = 'free') +
annotate("segment", x = 80, xend = 80, y = 0, yend = 125000,colour = "grey", size = 2) +
#annotate("segment", x = 2.5, xend = 4, y = 15, yend = 25,colour = "grey") +
#facet_wrap(~year, scales = 'free_y') +
#scale_x_log10() +
#scale_y_log10() +
geom_point(size = 1.5)
output
# ggsave("R_outputs/3census_empl_housing_quintiles_sameScale.png",output,width = 15,height = 5)
#ggsave("R_outputs/3census_empl_housing_3tiles.png",output,width = 15,height = 5)
#ggsave("R_outputs/3census_empl_housing_3tiles_sameScale.png",output,width = 15,height = 5)
ggsave("R_outputs/3census_empl_housing_3tiles_free_compline.png",output,width = 15,height = 5)
#Ooo! Identify those five groups on a map... maybe later.
#house_n_EA_q$percentEmp_quintiles <- cut_interval(house_n_EA_q$percentEmp, 5)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#3 census house vs employment: facet against CoB------
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Break down by country, then order each country's zone %s.
#Then facet by country while sizing based on % (or % rank, actually).
#So: processing CoB to stick in with house_n_EA.
all3_df <- data.frame(all3)
#Hmm. May have to do the prop.tables on each year separately. Which we still have...
one <- prop.table(threeCensus91[,c(3:41)] %>% data.frame %>% as.matrix, margin = 2) %>% data.frame
two <- prop.table(threeCensus01[,c(3:41)] %>% data.frame %>% as.matrix, margin = 2) %>% data.frame
three <- prop.table(threeCensus11[,c(3:41)] %>% data.frame %>% as.matrix, margin = 2) %>% data.frame
one <- one * 100
two <- two * 100
three <- three * 100
#Check that worked. Yup.
apply(three,2,sum)
#Add everything back in
one$year <- 1991
two$year <- 2001
three$year <- 2011
one$label <- threeCensus91@data[,1]
two$label <- threeCensus91@data[,1]
three$label <- threeCensus91@data[,1]
#Ah...
names(one)[names(one)=="year"]
names(two)[names(two)=="year"]
names(three)[names(three)=="year"]
names(house_n_EA)[names(house_n_EA)=="year"]
#It's not letting me do the merge all at once. Dunno why, it's only a one to many merge
#But can do individually
oneMerge <- left_join(one,house_n_EA,by = c("year","label"))
twoMerge <- left_join(two,house_n_EA,by = c("year","label"))
threeMerge <- left_join(three,house_n_EA,by = c("year","label"))
#CoBprops3census <- do.call(rbind,c(one,two,three,makeUniqueIDs = T))
#CoBprops3census <- rbind(one,two)
#CoBprops3census <- rbind(CoBprops3census,three)
CoBprops3census <- rbind(oneMerge,twoMerge)
CoBprops3census <- rbind(CoBprops3census,threeMerge)
#loooong
CoBprops3census_long <- CoBprops3census %>% gather(CoB,percent,1:39)
#equal size groups of CoB share per census year
CoBprops3census_long <- CoBprops3census_long %>% group_by(CoB,year) %>%
mutate(count = n())
# mutate(quintile = as.numeric(cut_number(percent, 3)))
#Or don't. Do it above individually
#merge in the housing/employment data by year and zone
# allzMerge <- merge(CoBprops3census_long,house_n_EA,
# by.x = c('year','label'),
# by.y = c('censusYear','label'),
# all.x = T)
#allzMerge <- left_join(CoBprops3census_long,house_n_EA,by = c("year","label"))
#allzMerge <- left_join(CoBprops3census_long,house_n_EA,by = c("year","label"))
#Right!
output <- ggplot(CoBprops3census_long[CoBprops3census_long$year == 1991,],
aes(x = percentEmp, y = meanPrice, size = percent)) +
geom_point() +
facet_wrap(~CoB)
output
output <- ggplot(CoBprops3census_long %>% filter(!CoB %in% c('England','Scotland'),percent > 2),
aes(x = percentEmp, y = meanPrice, colour = CoB, size = percent)) +
geom_point() +
scale_colour_manual(values=cols) +
facet_wrap(~year)
output
#Look at all CoBs separately
#pick one to test
output <- ggplot(CoBprops3census_long %>% filter(!CoB %in% c('Poland'),percent > 3),
#output <- ggplot(CoBprops3census_long %>% filter(!CoB %in% c('Poland')),
aes(x = percentEmp, y = meanPrice, colour = percent, size = percent)) +
geom_point() +
scale_size_continuous(range = c(1, 18)) +
geom_text(aes(label = label, y = meanPrice, x = percentEmp), size = 3, colour="black") +
facet_wrap(~year)
#facet_wrap(~year, scales = 'free')
output
ggsave("R_outputs/testFacet.png",output,width = 9,height = 4)
#repeat for all
for(i in unique(CoBprops3census_long$CoB)){
if((CoBprops3census_long %>% filter(CoB == i, percent > 2) %>% nrow)> 1) {
output <- ggplot(CoBprops3census_long %>% filter(CoB == i, percent > 1),
#output <- ggplot(CoBprops3census_long %>% filter(!CoB %in% c('Poland')),
aes(x = percentEmp, y = meanPrice, colour = percent, size = percent)) +
geom_point() +
scale_size_continuous(range = c(1, 18)) +
geom_text(aes(label = label, y = meanPrice, x = percentEmp), size = 3, colour="black") +
facet_wrap(~year)
#facet_wrap(~year, scales = 'free')
output
ggsave(paste0("R_outputs/facetCoB/",i,".png"),output,width = 12,height = 5)
}
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#5 census employment: facet against CoB------
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Break down by country, then order each country's zone %s.
#Then facet by country while sizing based on % (or % rank, actually).
#So: processing CoB to stick in with house_n_EA.
all5_df <- data.frame(allFive)
#Use allFive - it's got updated names added
#Hmm. May have to do the prop.tables on each year separately. Which we still have...
one <- prop.table(all5_df %>% filter(year == 1971) %>% dplyr::select(12:25) %>% as.matrix, margin = 2) %>% data.frame
two <- prop.table(all5_df %>% filter(year == 1981) %>% dplyr::select(12:25) %>% as.matrix, margin = 2) %>% data.frame
three <- prop.table(all5_df %>% filter(year == 1991) %>% dplyr::select(12:25) %>% as.matrix, margin = 2) %>% data.frame
four <- prop.table(all5_df %>% filter(year == 2001) %>% dplyr::select(12:25) %>% as.matrix, margin = 2) %>% data.frame
five <- prop.table(all5_df %>% filter(year == 2011) %>% dplyr::select(12:25) %>% as.matrix, margin = 2) %>% data.frame
one <- one * 100
two <- two * 100
three <- three * 100
four <- four * 100
five <- five * 100
#Check that worked. Yup.
apply(three,2,sum)
#Add everything back in
one$year <- 1971
two$year <- 1981
three$year <- 1991
four$year <- 2001
five$year <- 2011
one$label <- fiveCensus91@data[,1]
two$label <- fiveCensus91@data[,1]
three$label <- fiveCensus91@data[,1]
four$label <- fiveCensus91@data[,1]
five$label <- fiveCensus91@data[,1]
#Ah...
names(one)[names(one)=="year"]
names(two)[names(two)=="year"]
names(three)[names(three)=="year"]
names(house_n_EA)[names(house_n_EA)=="year"]
#ADD ECONOMICALLY ACTIVE HERE----
#It's not letting me do the merge all at once. Dunno why, it's only a one to many merge
#But can do individually
# oneMerge <- left_join(one,house_n_EA,by = c("year","label"))
# twoMerge <- left_join(two,house_n_EA,by = c("year","label"))
# threeMerge <- left_join(three,house_n_EA,by = c("year","label"))
#CoBprops3census <- do.call(rbind,c(one,two,three,makeUniqueIDs = T))
#CoBprops3census <- rbind(one,two)
#CoBprops3census <- rbind(CoBprops3census,three)
#Run just for CoB props...
CoBprops5census <- rbind(one,two) %>% rbind(three) %>% rbind(four) %>% rbind(five)
#CoBprops5census <- rbind(CoBprops3census,threeMerge)
#loooong
CoBprops5census_long <- CoBprops5census %>% gather(CoB,percent,1:14)
#Or don't. Do it above individually
#merge in the housing/employment data by year and zone
# allzMerge <- merge(CoBprops3census_long,house_n_EA,
# by.x = c('year','label'),
# by.y = c('censusYear','label'),
# all.x = T)
#allzMerge <- left_join(CoBprops3census_long,house_n_EA,by = c("year","label"))
#allzMerge <- left_join(CoBprops3census_long,house_n_EA,by = c("year","label"))
#Right!
output <- ggplot(CoBprops5census_long[CoBprops5census_long$year == 1991,],
aes(x = percentEmp, y = meanPrice, size = percent)) +
geom_point() +
facet_wrap(~CoB)
output
output <- ggplot(CoBprops3census_long %>% filter(!CoB %in% c('England','Scotland'),percent > 2),
aes(x = percentEmp, y = meanPrice, colour = CoB, size = percent)) +
geom_point() +
scale_colour_manual(values=cols) +
facet_wrap(~year)
output
#Look at all CoBs separately
#pick one to test
output <- ggplot(CoBprops3census_long %>% filter(!CoB %in% c('Poland'),percent > 3),
#output <- ggplot(CoBprops3census_long %>% filter(!CoB %in% c('Poland')),
aes(x = percentEmp, y = meanPrice, colour = percent, size = percent)) +
geom_point() +
scale_size_continuous(range = c(1, 18)) +
geom_text(aes(label = label, y = meanPrice, x = percentEmp), size = 3, colour="black") +
facet_wrap(~year)
#facet_wrap(~year, scales = 'free')
output
ggsave("R_outputs/testFacet.png",output,width = 9,height = 4)
#repeat for all
for(i in unique(CoBprops3census_long$CoB)){
if((CoBprops3census_long %>% filter(CoB == i, percent > 2) %>% nrow)> 1) {
output <- ggplot(CoBprops3census_long %>% filter(CoB == i, percent > 2),
#output <- ggplot(CoBprops3census_long %>% filter(!CoB %in% c('Poland')),
aes(x = percentEmp, y = meanPrice, colour = percent, size = percent)) +
geom_point() +
scale_size_continuous(range = c(1, 18)) +
geom_text(aes(label = label, y = meanPrice, x = percentEmp), size = 3, colour="black") +
facet_wrap(~year)
#facet_wrap(~year, scales = 'free')
output
ggsave(paste0("R_outputs/facetCoB/",i,".png"),output,width = 12,height = 5)
}
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#3 Census: spatial pattern exploring------
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Attach coordinates to the previous 3-census data (centroids taken via coordinates)
#Convert to latlon for below
proj4string(threeCensus91) <- CRS("+init=epsg:27700")
threeCensusLatLon <- spTransform(threeCensus91, CRS("+proj=longlat +datum=WGS84"))
coordzLatLon <- coordinates(threeCensusLatLon)
coordz <- coordinates(threeCensus91)
#plot(coordz)
coordz <- data.frame(label = threeCensus91@data$label, eastings = coordz[,1],northings = coordz[,2])
coordzLatLon <- data.frame(label = threeCensus91@data$label, eastings = coordzLatLon[,1],northings = coordzLatLon[,2])
CoBprops3census_longGeo <- merge(CoBprops3census_long,coordz,by = "label")
CoBprops3census_longGeoLatLon <- merge(CoBprops3census_long,coordzLatLon,by = "label")
map <- get_map("Glasgow", zoom = 12, source = "osm", color = "bw")
mapPoints <- ggmap(map)
mapPoints
#output <- ggplot(CoBprops3census_longGeo %>% filter(!CoB %in% c('Scotland','England'),percent > 1),
output <- ggplot(CoBprops3census_longGeo,
# output <- ggplot(CoBprops3census_longGeo %>% filter(!CoB %in% c('Scotland','England')),
#output <- ggplot(CoBprops3census_longGeo,
aes(x = eastings, y = northings, colour = CoB, size = percent)) +
geom_point() +
#geom_polygon(data = threeCensus91@polygons) +
scale_size_continuous(range = c(1, 18)) +
facet_wrap(~year) +
#Glasgow (national grid...)
coord_cartesian(xlim = c(244447, 279006), ylim = c(674747,652751)) +
guides(size = F)
#facet_wrap(~year, scales = 'free')
output
output <- mapPoints +
#geom_point(data = CoBprops3census_longGeoLatLon %>% filter(!CoB %in% c('Scotland','England')),
geom_point(data = CoBprops3census_longGeoLatLon %>% filter(CoB %in% c('Pakistan')),
# output <- ggplot(CoBprops3census_longGeo %>% filter(!CoB %in% c('Scotland','England')),
#output <- ggplot(CoBprops3census_longGeo,
aes(x = eastings, y = northings, colour = percent, size = percent)) +
scale_size_continuous(range = c(1, 12)) +
guides(size = F) +
geom_text(data = CoBprops3census_longGeoLatLon %>% filter(percent > 1.5),
aes(label = label, x = eastings, y = northings), size = 3, colour="black") +
#facet_wrap(~year) +
#scale_colour_manual(values=cols) +
theme(line = element_blank(),
#text = element_blank(),
title = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank())
#guides(size = F)
# opts(axis.text.x = theme_blank(),axis.text.y = theme_blank())
#scale_x_continuous(expand=c(0,0)) +
#scale_y_continuous(expand=c(0,0))
output
ggsave("R_outputs/glasgow_Pakistan_percent.png",output,width = 15,height = 5)
#~~~~~~~~~~~~~
#Output three of those, one for each census, for cycling visually through
#yrz <- seq(1991,2011,by = 10)
for(yrz in seq(1991,2011,by = 10)) {
output <- mapPoints +
#geom_point(data = CoBprops3census_longGeoLatLon %>% filter(!CoB %in% c('Scotland','England')),
geom_point(data = CoBprops3census_longGeoLatLon %>% filter(CoB %in% c('Pakistan'), year == yrz),
# output <- ggplot(CoBprops3census_longGeo %>% filter(!CoB %in% c('Scotland','England')),
#output <- ggplot(CoBprops3census_longGeo,
aes(x = eastings, y = northings, colour = percent, size = percent)) +
scale_size_continuous(range = c(1, 12)) +
guides(size = F) +
geom_text(data = CoBprops3census_longGeoLatLon %>% filter(percent > 1.5),
aes(label = label, x = eastings, y = northings), size = 3, colour="black") +
guides(size = F, colour = F) +
#facet_wrap(~year) +
#scale_colour_manual(values=cols) +
theme(line = element_blank(),
#text = element_blank(),
title = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank())
# opts(axis.text.x = theme_blank(),axis.text.y = theme_blank())