-
Notifications
You must be signed in to change notification settings - Fork 0
/
nc-pop-change.qmd
1260 lines (1075 loc) · 44.6 KB
/
nc-pop-change.qmd
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
# NC population and population change; under/over 18 years old
While family size in the USA has been shrinking since before the start of this dataset^[<https://www.statista.com/statistics/183657/average-size-of-a-family-in-the-us/> ], the decline was not uniform across NC counties. Net population change hides interesting dynamics. The following uses decennial US Census results 1970 to 2020 provided by IPUMS NHGIS.
Growth rates are over ten years.
:::{.callout-note collapse="true"}
## Note on 2020 census differential privacy
The `tidycensus` package offers the following helpful warning when returning results from 2020, which is relevant to this endeavor:
>2020 decennial Census data use differential privacy, a technique that introduces errors into data to preserve respondent confidentiality. Small counts should be interpreted with caution. See <https://www.census.gov/library/fact-sheets/2021/protecting-the-confidentiality-of-the-2020-census-redistricting-data.html> for additional guidance.
:::
<br>
```{r}
#| label: setup
#| echo: false
#| warning: false
#| message: false
source("./scripts/my-setup.R") # use for all explorationXX.qmd files
source("./scripts/constants2.R")
# TODO: create functions2.R
source("./scripts/functions2.R")
# library(geofacet)
# library(ggridges)
# library(ggrepel)
#library(elevatr)
library(terra)
library(stars)
library(rcartocolor)
library(fs)
library(english) #for ordinal() to spell out words
library(geomtextpath)
# install.packages("Rcpp", repos="https://rcppcore.github.io/drat")
# to address https://github.com/rspatial/terra/issues/30 until the next Rccp release
# "Error in x$.self$finalize() : attempt to apply non-function"
# which I only see when running 'quarto render' from the command line 2022-11-25
```
```{r}
nc_city_boundaries <- tar_read(nc_city_boundaries)
county_region_df <- tar_read(nc_county_region_mapping)
nc_county_boundaries <- tar_read(nc_county_boundaries) |>
inner_join(
county_region_df,
by = "county"
)
urban_crescent_boundary <- nc_county_boundaries |>
filter(county %in% urban_crescent) |>
summarize(boundary = st_union(geometry))
coastal_boundary <- nc_county_boundaries |>
filter(county %in% coastal) |>
summarize(boundary = st_union(geometry))
mountain_boundary <- nc_county_boundaries |>
filter(county %in% mountain) |>
summarize(boundary = st_union(geometry))
manufacturing_boundary <- nc_county_boundaries |>
filter(county %in% manufacturing) |>
summarize(boundary = st_union(geometry))
# agriculture boundary is everything else
nc_state_boundary <- st_union(nc_county_boundaries)
nc_state_boundary_sf <- st_as_sf(nc_state_boundary)
```
```{r}
#| label: define-pop_u18
#| message: false
# TODO: put this in a function and pre-compute using target
years_in_scope <- c("1970", "1980", "1990", "2000", "2010", "2020")
# can use 2020 for these; otherwise need to use 2015-2019:
# Table 1: (A00) Total Population
# A00AA: Persons: Total
# Table 2: (D08) Persons by Age [2]: Children and Adults
# D08AA: Persons: Under 18 years
# D08AB: Persons: 18 years and over
pop_u18 <- read_csv("./data/ipums/nhgis0008_csv/nhgis0008_ts_nominal_county.csv") |>
filter(STATE == "North Carolina",
YEAR %in% years_in_scope) |>
select(GISJOIN, YEAR, COUNTY = NAME,
pop = A00AA, # Persons: Total,
pop_u18 = D08AA, # Persons: Under 18 years
pop_18p = D08AB # Persons: 18 years and over
) |>
clean_names() |>
mutate(county = str_to_title(str_extract(county, "[A-z]+")),
county = case_when(
county == "New" ~ "New Hanover",
county == "Mc" ~ "McDowell", # prob in 1970 data
county == "Mcdowell" ~ "McDowell",
TRUE ~ county
),
pop_pct_u18 = pop_u18 / pop,
pop_pct_18p = pop_18p / pop) |>
left_join(county_region_df,
by = "county") |>
mutate(region = factor(region, levels = region_colors$region))
nc_total <- pop_u18 |>
group_by(year) |>
summarize(
pop = sum(pop),
pop_u18 = sum(pop_u18),
pop_18p = sum(pop_18p)
) |>
ungroup() |>
mutate(
pop_pct_u18 = pop_u18 / pop,
pop_pct_18p = pop_18p / pop,
county = "NC total"
) |>
mutate(change_since_1970 = pop_pct_u18 / first(pop_pct_u18),
region = "NC")
nc_total_long <- nc_total |>
pivot_longer(
cols = starts_with("pop"),
names_to = "variable",
values_to = "value"
) |>
mutate(sign_value = sign(value)) |>
arrange(region, variable, year) |>
group_by(region, variable) |>
# growth_rate for any row refers to the growth between this an the next census
mutate(
pct_growth_naive = abs(value / lag(value)) - 1,
pct_growth_factor = case_when(
sign_value + lag(sign_value) == 2 ~ 1,
sign_value + lag(sign_value) == 0 ~ -1,
sign_value + lag(sign_value) == -2 ~ -1,
TRUE ~ 0 # error
),
pct_growth = pct_growth_naive * pct_growth_factor,
# pct_growth_original = lead(value) / value - 1,
# pct_growth = lead(value_xformed) / value_xformed - 1,
positive_growth = case_when(
pct_growth > 0 ~ "positive",
pct_growth < 0 ~ "negative",
abs(pct_growth) < 1e15 ~ "zero",
is.na(pct_growth) ~ "zz info not available",
TRUE ~ "error in case_when() input"
)
) |>
ungroup()
pop_u18_summary <- bind_rows(
pop_u18,
nc_total) |>
group_by(region, year) |>
summarize(pop = sum(pop),
pop_u18 = sum(pop_u18),
pop_18p = sum(pop_18p),
pop_pct_u18 = pop_u18 / pop,
pop_pct_18p = pop_18p / pop) |>
ungroup() |>
mutate(region = factor(region, levels = region_colors$region))
d_pop_growth_u18_long <- pop_u18 |>
pivot_longer(
cols = starts_with("pop"),
names_to = "variable",
values_to = "value"
) |>
mutate(sign_value = sign(value)) |>
arrange(county, variable, year) |>
group_by(county, variable) |>
# growth_rate for any row refers to the growth between this an the next census
mutate(
pct_growth_naive = abs(value / lag(value)) - 1,
pct_growth_factor = case_when(
sign_value + lag(sign_value) == 2 ~ 1,
sign_value + lag(sign_value) == 0 ~ -1,
sign_value + lag(sign_value) == -2 ~ -1,
TRUE ~ 0 # error
),
pct_growth = pct_growth_naive * pct_growth_factor,
# pct_growth_original = lead(value) / value - 1,
# pct_growth = lead(value_xformed) / value_xformed - 1,
positive_growth = case_when(
pct_growth > 0 ~ "positive",
pct_growth < 0 ~ "negative",
abs(pct_growth) < 1e15 ~ "zero",
is.na(pct_growth) ~ "zz info not available",
TRUE ~ "error in case_when() input"
)
) |>
ungroup() #|>
#filter(year != "1970") # TODO: make sure consuming sections do this if needed
d_pop_growth_u18_summary_long <- pop_u18_summary |>
pivot_longer(
cols = starts_with("pop"),
names_to = "variable",
values_to = "value"
) |>
mutate(sign_value = sign(value)) |>
arrange(region, variable, year) |>
group_by(region, variable) |>
# growth_rate for any row refers to the growth between this an the next census
mutate(
pct_growth_naive = abs(value / lag(value)) - 1,
pct_growth_factor = case_when(
sign_value + lag(sign_value) == 2 ~ 1,
sign_value + lag(sign_value) == 0 ~ -1,
sign_value + lag(sign_value) == -2 ~ -1,
TRUE ~ 0 # error
),
pct_growth = pct_growth_naive * pct_growth_factor,
# pct_growth_original = lead(value) / value - 1,
# pct_growth = lead(value_xformed) / value_xformed - 1,
positive_growth = case_when(
pct_growth > 0 ~ "positive",
pct_growth < 0 ~ "negative",
abs(pct_growth) < 1e15 ~ "zero",
is.na(pct_growth) ~ "zz info not available",
TRUE ~ "error in case_when() input"
)
) |>
ungroup() #|>
#filter(year != "1970") # TODO: make sure consuming sections do this if needed
```
## By region
Using the regions defined earlier, let's look at the total population, 18+ and under 18 for each region. Population is on a linear scale with each row having its own scale to maximize the detail within each row. In all regions the 18+ group has increased, while the under 18 group stayed about the same in regions other than the urban crescent and the coastal counties.
<br>
```{r fig.height=8, fig.width=8}
#| label: fig-summary_population-facet-grid-1
#| fig-cap: "NC regional population (1980 - 2022). Each row provides comparison across the variables for a region"
#| fig-height: 8
#| fig-width: 8
#| column: screen-inset-right
region_levels_new <- rev(region_colors$region)
region_color_levels_new = rev(region_colors$color)
d_pop_growth_u18_summary_long |>
filter(!str_detect(variable, "pct")) |>
mutate(region = factor(region, levels = region_levels_new)) |>
ggplot(aes(year, value, color = region)) +
geom_point(size = 2, alpha = 0.8) +
geom_line() +
facet_grid(region ~ variable, scales = "free_y") +
scale_x_continuous(breaks = c(1970, 1990, 2010)) +
scale_y_continuous(labels = label_number(scale_cut = cut_short_scale())) +
scale_color_manual(values = region_color_levels_new) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.title = element_text(size = rel(2.0), face = "bold"),
legend.position = "none"
) +
labs(title = "NC population (1970 - 2022)",
subtitle = "US decennial census counts",
x = NULL,
y = "Population (scale varies by row)",
caption = my_caption_nhgis)
```
<br>
The plot below allows comparison of total population, 18+ and under 18 across the regions. The uniqueness of the urban crescent stands out.
<br>
```{r fig.height=8, fig.width=8}
#| label: fig-summary_population-facet-grid-2
#| fig-cap: "NC regional population (1980 - 2022). Each row provides comparison across the regions for each variable."
#| fig-height: 8
#| fig-width: 8
#| column: screen-inset-right
d_pop_growth_u18_summary_long |>
filter(!str_detect(variable, "pct")) |>
mutate(region = factor(region, levels = region_levels_new)) |>
ggplot(aes(year, value, color = region)) +
geom_point(size = 2, alpha = 0.8) +
geom_line() +
facet_grid(variable ~ region, scales = "free_y") +
scale_x_continuous(breaks = c(1970, 1990, 2010)) +
scale_y_continuous(labels = label_number(scale_cut = cut_short_scale())) +
scale_color_manual(values = region_color_levels_new) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.title = element_text(size = rel(2.0), face = "bold"),
legend.position = "none"
) +
labs(title = "NC population (1970 - 2022)",
subtitle = "US decennial census counts",
x = NULL,
y = "Population (scale varies by row)",
caption = my_caption_nhgis)
```
<br>
## Ranked by county
We can rank counties by their population. On the left is a simple ranking; on the right population is plotted on the y axis.
<br>
```{r fig.height=14, fig.width=10}
#| label: fig-nc-counties-total-pop-rank
#| fig-cap: "County rank: total population"
#| fig-width: 10
#| fig-height: 14
#| column: screen-inset-right
plot_rank_population(my_title = "NC counties ranked by total population"
)
```
<br>
## Growth rates
The plot below allows comparison of growth rates for total population, 18+ and under 18 *for each region*. In the ten years to 2020 the 18+ population grew in most regions. In contrast, the under 18 population was about net flat; growth in the urban crescent was offset by declines in the other regions.
<br>
```{r fig.height=8, fig.width=8}
#| label: fig-summary-population-growth-rates-facet-grid-1
#| fig-cap: "NC regional population growth rates (1980 - 2020). Each row provides comparison across the variables for a region."
#| fig-height: 8
#| fig-width: 8
#| column: screen-inset-right
region_levels_new <- rev(region_colors$region)
region_color_levels_new = rev(region_colors$color)
d_pop_growth_u18_summary_long |>
filter(!str_detect(variable, "pct"),
year != 1970) |>
mutate(region = factor(region, levels = region_levels_new)) |>
ggplot(aes(year, pct_growth, color = region)) +
geom_hline(yintercept = 0, lty = 2, linewidth = 0.25, alpha = 0.2) +
geom_point(size = 2, alpha = 0.8) +
geom_line() +
scale_x_continuous(breaks = c(1970, 1990, 2010)) +
scale_y_continuous(labels = percent_format()) +
scale_color_manual(values = region_color_levels_new) +
facet_grid(region ~ variable) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.title = element_text(size = rel(2.0), face = "bold"),
legend.position = "none"
) +
labs(title = "NC population growth rate (1980 - 2022)",
subtitle = "US decennial census counts",
x = NULL,
y = "Popluation growth rate",
caption = my_caption_nhgis)
```
<br>
The plot below allows comparison of growth rates for total population, 18+ and under 18 *across the regions*.
<br>
```{r fig.height=8, fig.width=8}
#| label: fig-summary-population-growth-rates-facet-grid-2
#| fig-cap: "NC regional population growth rates (1980 - 2020). Each row provides comparison across the regions for a variable."
#| fig-height: 8
#| fig-width: 8
#| column: screen-inset-right
d_pop_growth_u18_summary_long |>
filter(!str_detect(variable, "pct"),
year != 1970) |>
mutate(region = factor(region, levels = region_levels_new)) |>
ggplot(aes(year, pct_growth, color = region)) +
geom_hline(yintercept = 0, lty = 2, linewidth = 0.25, alpha = 0.2) +
geom_point(size = 2, alpha = 0.8) +
geom_line() +
facet_grid(variable ~ region) +
scale_x_continuous(breaks = c(1970, 1990, 2010)) +
scale_y_continuous(labels = percent_format()) +
scale_color_manual(values = region_color_levels_new) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.title = element_text(size = rel(2.0), face = "bold"),
legend.position = "none"
) +
labs(title = "NC population growth rate (1980 - 2020)",
subtitle = "US decennial census counts",
x = NULL,
y = "Popluation growth rate",
caption = my_caption_nhgis)
```
<br>
In recent censuses it's generally true that the counties with larger population grew while those with smaller, shrank. This wasn't the case in the 10 years leading to the 1980 census. By 2000 it was quite pronounced in the under 18 population, and since then the pattern is visible in all three categories: total population, 18+, and under 18.
The grow rate slowed a lot in the 10 years to 2020.
The regional dynamics are visible when plotting county growth rates on a map. For rural counties in the east and west, the 2000 census offered reason for hope for the future, since the number of children increased; since then the under 18 population has declined again.
<br>
```{r fig.height=6, fig.width=10}
#| label: fig-nc-map-fact-u18-pct
#| fig-cap: "NC county growth rates 1990-2020"
#| fig-height: 8
#| fig-width: 14
#| column: screen-inset-right
data_for_plot <- d_pop_growth_u18_long |>
filter(year != 1970) |>
group_by(variable, county) |>
mutate(order = row_number()) |>
fill(c(pct_growth, positive_growth), .direction = "down") |>
ungroup() |>
mutate(order = factor(as.character(order), levels = c("1", "2", "3", "4")))
nc_county_boundaries |>
left_join(data_for_plot,
by = "county",
multiple = "all") |>
filter(variable %in% c("pop", "pop_18p", "pop_u18")) |> #"pop_pct_18p",
ggplot() +
geom_sf(aes(fill = pct_growth), color = "white", size = 0.1) +
facet_grid(year ~ variable) +
scale_fill_gradient2(low = "firebrick", high = "lightskyblue", mid = "grey80",
midpoint = 0, labels = percent) +
theme_map() +
theme(plot.title = element_text(size = rel(2.0), face = "bold"),
legend.position = "right") +
labs(title = "NC county growth rates",
subtitle = glue("Total, 18+, and under 18 population growth in the 10 years to 1990-2020"),
fill = "Population growth",
caption = my_caption_nhgis)
```
<br>
## Components of population change
Population changes are due to (1) births and deaths ("natural increase"); and (2) migration in and out ("net migration"). In the case of North Carolina as a whole, most of the increase is due to net migration, as seen in @fig-estimates-state-rates-natural-migration and @fig-estimates-five-year-four-vars-state-summary.
In @fig-estimates-state-rates-natural-migration each thin line shows one county's rate of natural increase or rate of net migration. The thick line is the weighted mean.
<br>
```{r}
#| label: define-multi-year-pop-change-components
#| include: false
# state-level
my_vars <- c("BIRTHS", "DEATHS", "NATURALINC",
"RBIRTH", "RDEATH", "RNATURALINC",
"DOMESTICMIG", "INTERNATIONALMIG", "NETMIG",
"RDOMESTICMIG", "RINTERNATIONALMIG", "RNETMIG")
get_nc_pop <- function(my_year) {
get_estimates(
geography = "state",
state = "NC",
variables = "POP",
year = my_year
) |>
mutate(value = if_else(str_detect(variable, "^R"),
value / 1000,
value),
datayear = my_year)
}
multi_year_nc_pop <- map_df(2015:2019, get_nc_pop) |>
mutate(NAME = "North Carolina") |>
rename(state = NAME)
######
get_nc_estimates <- function(my_year) {
get_estimates(
geography = "state",
state = "NC",
variables = my_vars,
year = my_year
) |>
mutate(value = if_else(str_detect(variable, "^R"),
value / 1000,
value),
datayear = my_year,
variable = factor(variable, levels = my_vars))
}
multi_year_nc <- map_df(2015:2019, get_nc_estimates) |>
mutate(NAME = "North Carolina") |>
rename(state = NAME) |>
inner_join(multi_year_nc_pop |>
rename(pop_total = value) |>
select(-c(variable, GEOID, state)),
by = "datayear")
# The Population Estimates API is not available in tidycensus for years prior to 2015
# county-level
get_nc_county_pop <- function(my_year) {
get_estimates(
geography = "county",
state = "NC",
variables = "POP",
year = my_year
) |>
mutate(value = if_else(str_detect(variable, "^R"),
value / 1000,
value),
datayear = my_year)
}
multi_year_nc_county_pop <- map_df(2015:2019, get_nc_county_pop) |>
#mutate(NAME = "North Carolina") |>
rename(county = NAME,
pop_total = value) |>
mutate(county = str_remove(county, " County.*")) |>
select(-variable)
######
get_nc_county_estimates <- function(my_year) {
get_estimates(
geography = "county",
state = "NC",
variables = my_vars,
year = my_year
) |>
mutate(value = if_else(str_detect(variable, "^R"),
value / 1000,
value),
datayear = my_year,
variable = factor(variable, levels = my_vars))
}
multi_year_nc_counties <- map_df(2015:2019, get_nc_county_estimates) |>
mutate(NAME = str_remove(NAME, " County.*")) |>
rename(county = NAME)
# The Population Estimates API is not available in tidycensus for years prior to 2015
```
```{r fig.height=6, fig.width=10}
#| label: fig-estimates-state-rates-natural-migration
#| fig-cap: "Rates of population change in North Carolina (2015-2019)"
#| fig-height: 6
#| fig-width: 10
#| column: screen-inset-right
data_for_mean_plot <- multi_year_nc_counties |>
#filter(str_detect(variable, "^R")) |>
filter(str_detect(variable, "NETMIG|NATURALINC")) |>
pivot_wider(id_cols = c(county, GEOID, datayear), names_from = "variable", values_from = "value") |>
group_by(datayear) |>
reframe(mean_rnaturalinc = weighted.mean(RNATURALINC, w = abs(NATURALINC)),
mean_rnetmig = weighted.mean(RNETMIG, w = abs(NETMIG))) |>
ungroup() |>
#filter(variable %in% c("RNETMIG", "RNATURALINC")) |>
pivot_longer(cols = starts_with("mean_"), names_to = "variable", values_to = "value") |>
mutate(variable = if_else(variable == "mean_rnetmig", "Rate of net migration", "Rate of natural increase"))
multi_year_nc_counties |>
filter(str_detect(variable, "^R")) |>
filter(variable %in% c("RNETMIG", "RNATURALINC")) |>
mutate(variable = if_else(variable == "RNETMIG", "Rate of net migration", "Rate of natural increase")) |>
ggplot(aes(x = datayear, y = value, color = variable)) +
geom_hline(yintercept = 0, lty = 2, linewidth = 1, color = "firebrick", alpha = 0.25) +
geom_line(aes(group = county), alpha = 0.1, show.legend = FALSE) +
#geom_smooth(method = "lm", formula = 'y ~ x', se = FALSE, show.legend = FALSE) +
geom_line(data = data_for_mean_plot,
aes(x = datayear, y = value, color = variable),
linewidth = 1, alpha = 0.8) +
scale_y_continuous(labels = label_percent()) +
scale_color_viridis_d(end = 0.8) +
coord_cartesian(ylim = c(-0.02, 0.02)) +
facet_wrap(~ variable, ncol = 2) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.title = element_text(size = rel(2.0), face = "bold"),
legend.position = "top") +
guides(color = guide_legend(override.aes = list(linewidth = 3))) +
labs(
title = "NC population increase 2015-2019",
subtitle = glue("Roughtly one third was due to natural increase; two thirds due to net migration"),
x = NULL,
y = "Yearly rate",
color = NULL,
caption = my_caption
)
```
<!-- br>< -->
<!-- In this time period the state-level pattern repeats each year: there are more births than deaths, and domestic and international migration are net positive, resulting in a population growth of about one percent each year. -->
<!-- <br> -->
```{r fig.height=6, fig.width=8}
#| label: fig-estimates-five-year-four-vars-state-summary
#| fig-cap: "Population change in NC counties over the five years 2015-2019"
#| fig-height: 6
#| fig-width: 8
#| column: screen-inset-right
#| eval: false
#factor_county_stretch <- 0 # Not needed for state summary
factor_datayear_stretch <- 8
factor_variable_stretch <- 2
data_for_plot_tmp <- multi_year_nc |>
filter(!str_detect(variable, "^R"),
!variable %in% c("NETMIG", "NATURALINC")) |>
mutate(variable = fct_drop(variable),
value = if_else(variable == "DEATHS",
-1 * value,
value),
value_pct_pop = value / pop_total
) |>
arrange(datayear, variable) |>
mutate(
xend = cumsum(value_pct_pop),
xstart = lag(xend, default = 0),
row_id = row_number()
) |>
arrange(datayear, variable)
datayear_levels <- data_for_plot_tmp |>
distinct(datayear) |>
arrange(datayear) |>
mutate(datayear_id = (row_number() - 1) * factor_datayear_stretch)
variable_levels <- data_for_plot_tmp |>
distinct(variable) |>
arrange(variable) |>
mutate(variable_id = (row_number() - 1) * factor_variable_stretch)
data_for_plot <- data_for_plot_tmp |>
inner_join(datayear_levels,
by = "datayear") |>
inner_join(variable_levels,
by = "variable") |>
mutate(
y_level = datayear_id + variable_id,
group = case_when(
variable %in% c("BIRTHS", "DEATHS", "NATURALINC") ~ "natural",
str_detect(variable, "MIG") ~ "migration",
TRUE ~ "ERROR"
)
) |>
mutate(
label_datayear = if_else(variable_id == 2,
as.character(datayear),
"")
)
data_for_plot |>
ggplot() +
geom_vline(xintercept = 0, lty = 2, linewidth = 0.5, color = "firebrick", alpha = 0.25) +
geom_hline(aes(yintercept = if_else(variable_id == 0,
datayear_id - 1,
NA_real_)),
na.rm = TRUE, linewidth = 0.2, alpha = 0.5) +
geom_segment(aes(x = xstart, xend = xend, y = y_level, yend = y_level, color = variable),
linewidth = 3) +
geom_text(aes(x = -0.005, #x_value_label,
y = y_level,
label = label_datayear),
hjust = 1, na.rm = TRUE, size = 4) +
scale_x_continuous(labels = label_percent()) +
scale_y_continuous(expand = expansion(mult = c(0, .025))) +
scale_color_viridis_d(option = "H", end = 0.8) +
coord_cartesian(xlim = c(-0.01, NA)) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
plot.title = element_text(size = rel(2.0), face = "bold"),
legend.position = "top") +
guides(color = guide_legend(override.aes = list(linewidth = 3))) +
labs(
title = "Population change in NC in the five years 2015-2019",
subtitle = "Percent of state population",
x = "Change in state population since 2014",
y = NULL,
color = NULL,
caption = my_caption
)
```
<br>
As is hinted in the "RNETMIG" panel in @fig-estimates-state-rates-natural-migration, county-level dynamics vary considerably. First, let's look at this by region.
Observations
* The coastal counties and those in the urban crescent are the only regions that are all growing
* Domestic in- and out-migration are the biggest contributors to county population growth or decline
<br>
```{r fig.height=10, fig.width=12}
#| label: fig-estimates-five-year-four-vars-6
#| fig-cap: "Population change in NC counties over the five years 2015-2019"
#| fig-height: 10
#| fig-width: 12
#| column: screen-inset-right
factor_county_stretch <- 40
factor_datayear_stretch <- 8
factor_variable_stretch <- 2
data_for_plot_tmp <- multi_year_nc_counties |>
filter(!str_detect(variable, "^R"),
!variable %in% c("NETMIG", "NATURALINC")) |>
inner_join(multi_year_nc_county_pop,
by = c("GEOID", "county", "datayear")) |>
mutate(variable = fct_drop(variable),
value = if_else(variable == "DEATHS",
-1 * value,
value),
value_pct_pop = value / pop_total
) |>
arrange(datayear, variable) |>
inner_join(county_region_df,
by = "county") |>
group_by(GEOID, county, region) |>
mutate(
xend = cumsum(value_pct_pop),
xstart = lag(xend, default = 0),
row_id = row_number()
) |>
ungroup() |>
arrange(GEOID, county, datayear, variable)
county_levels <- data_for_plot_tmp |>
group_by(region, county) |>
mutate(end_xend = xend[max(row_id)]) |>
ungroup() |>
distinct(county, region, end_xend) |>
group_by(region) |>
arrange(desc(end_xend)) |>
mutate(county_id = (row_number() - 1) * factor_county_stretch) |>
ungroup()
datayear_levels <- data_for_plot_tmp |>
distinct(datayear) |>
arrange(datayear) |>
mutate(datayear_id = (row_number() - 1) * factor_datayear_stretch)
variable_levels <- data_for_plot_tmp |>
distinct(variable) |>
arrange(variable) |>
mutate(variable_id = (row_number() - 1) * factor_variable_stretch)
data_for_plot <- data_for_plot_tmp |>
inner_join(county_levels,
by = c("county", "region")) |>
inner_join(datayear_levels,
by = "datayear") |>
inner_join(variable_levels,
by = "variable") |>
mutate(
y_level = county_id + datayear_id + variable_id,
group = case_when(
variable %in% c("BIRTHS", "DEATHS", "NATURALINC") ~ "natural",
str_detect(variable, "MIG") ~ "migration",
TRUE ~ "ERROR"
)
) |>
mutate(
label_county = if_else(datayear_id == 16 & variable_id == 0, # TODO: generalize: 16 is 3rd of 5 datayear values
county,
"")
) |>
group_by(region) |>
mutate(x_value_label = min(pmin(xstart, xend)) - 0.01) |>
ungroup()
data_for_plot |>
ggplot() +
geom_vline(xintercept = 0, lty = 2, linewidth = 0.5, color = "firebrick", alpha = 0.25) +
geom_segment(aes(x = xstart, xend = xend, y = y_level, yend = y_level, color = variable),
linewidth = 0.5) +
geom_text(aes(x = x_value_label,
y = y_level,
label = label_county),
hjust = 1, na.rm = TRUE, size = 2.5) +
facet_wrap( ~ region, nrow = 1) +
scale_x_continuous(labels = label_percent()) +
scale_y_reverse() +
scale_color_viridis_d(option = "H", end = 0.8) +
coord_cartesian(xlim = c(-0.20, 0.17)) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
plot.title = element_text(size = rel(2.0), face = "bold"),
legend.position = "top") +
guides(color = guide_legend(override.aes = list(linewidth = 3))) +
labs(
title = "Population changes in NC counties in the five years 2015-2019",
subtitle = "Percent of county population; sorted within region by net population chage in this period",
x = "Change in county population since 2014",
y = NULL,
color = NULL,
caption = my_caption
)
```
<br>
Here too some patterns are easier to see when the counties are put in approximate geographical position (@fig-estimates-five-year-four-vars-geofacet).
Observations
* Most of agricultural counties growing the fastest border a county containing of the larger cities: (1) Chatham, Franklin, Harnett, and Johnston surround Wake; (2) Iredell (spill-over from Charlotte); (3) Henderson and Haywood (near Asheville / Buncombe county)
* Exceptions: (2) Hoke (spill-over from Fayetteville in Cumberland); (2) Watauga (contains App State University and the town of Boone); (3) Jackson (not obvious why it's growing; perhaps benefiting from Buncombe-Haywood growth?)
* Onslow, Hoke and Cumberland experienced largest net natural increase:
```{r}
data_for_table <- multi_year_nc_counties |>
filter(str_detect(variable, "RNATURALINC")) |>
group_by(GEOID, county) |>
summarize(avg_yearly_rate = mean(value)) |>
ungroup() |>
arrange(desc(avg_yearly_rate)) |>
select(-GEOID) |>
left_join(
county_region_df,
by = "county"
) |>
select(county, region, avg_yearly_rate)
bind_rows(
data_for_table |> head(10),
data_for_table |> tail(10)
) |>
gt() |>
tab_header(md(glue("***Ten counties with greatest positive and negative<br>percent natural increase 2015-2019***",
"<br>Simple average of natural increase rate"))) |>
fmt_percent(columns = avg_yearly_rate,
decimals = 1)
```
<br>
* Brunswick, Chatham, and Pender experienced largest percent increases due net migration:
```{r}
data_for_table <- multi_year_nc_counties |>
filter(str_detect(variable, "^RNETMIG")) |>
group_by(GEOID, county) |>
summarize(avg_yearly_rate = mean(value)) |>
ungroup() |>
arrange(desc(avg_yearly_rate)) |>
select(-GEOID) |>
left_join(
county_region_df,
by = "county"
) |>
select(county, region, avg_yearly_rate)
bind_rows(
data_for_table |> head(10),
data_for_table |> tail(10)
) |>
gt() |>
tab_header(md(glue("***Ten counties with greatest positive and negative<br>percent net migration 2015-2019***",
"<br>Simple average of net migration rate"))) |>
fmt_percent(columns = avg_yearly_rate,
decimals = 1)
```
<br>
```{r fig.height=10, fig.width=24}
#| label: fig-estimates-five-year-four-vars-geofacet
#| fig-cap: "Population change in NC counties over the five years 2015-2019 (approximate geographical position)"
#| fig-height: 10
#| fig-width: 24
#| column: screen-inset-right
factor_county_stretch <- 40
factor_datayear_stretch <- 8
factor_variable_stretch <- 2
data_for_plot_tmp <- multi_year_nc_counties |>
filter(!str_detect(variable, "^R"),
!variable %in% c("NETMIG", "NATURALINC")) |>
inner_join(multi_year_nc_county_pop,
by = c("GEOID", "county", "datayear")) |>
mutate(variable = fct_drop(variable),
value = if_else(variable == "DEATHS",
-1 * value,
value),
value_pct_pop = value / pop_total
) |>
arrange(datayear, variable) |>
inner_join(county_region_df,
by = "county") |>
group_by(GEOID, county, region) |>
mutate(
xend = cumsum(value_pct_pop),
xstart = lag(xend, default = 0),
row_id = row_number()
) |>
ungroup() |>
arrange(GEOID, county, datayear, variable)
county_levels <- data_for_plot_tmp |>
distinct(county) |>
mutate(county_id = 0) # since each county is its own facet
datayear_levels <- data_for_plot_tmp |>
distinct(datayear) |>
arrange(datayear) |>
mutate(datayear_id = (row_number() - 1) * factor_datayear_stretch)
variable_levels <- data_for_plot_tmp |>
distinct(variable) |>
arrange(variable) |>
mutate(variable_id = (row_number() - 1) * factor_variable_stretch)
data_for_plot <- data_for_plot_tmp |>
inner_join(county_levels,
by = c("county")) |>
inner_join(datayear_levels,
by = "datayear") |>
inner_join(variable_levels,
by = "variable") |>
mutate(
y_level = county_id + datayear_id + variable_id,
group = case_when(
variable %in% c("BIRTHS", "DEATHS", "NATURALINC") ~ "natural",
str_detect(variable, "MIG") ~ "migration",
TRUE ~ "ERROR"
)
) |>
mutate(
label_datayear = if_else(variable_id == 2,
as.character(datayear),
"")
) |>
# group_by(region) |>
# mutate(x_value_label = min(pmin(xstart, xend)) - 0.02) |>
# ungroup() |>
mutate(county = if_else(county == "McDowell", "Mcdowell", county))
data_for_plot |>
ggplot() +
geom_vline(xintercept = 0, lty = 2, linewidth = 0.5, color = "firebrick", alpha = 0.25) +
geom_hline(aes(yintercept = if_else(variable_id == 0,
datayear_id - 1,
NA_real_)),
na.rm = TRUE, linewidth = 0.1, alpha = 0.5) +
geom_segment(aes(x = xstart, xend = xend, y = y_level, yend = y_level, color = variable),