-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPHC-2002-2006-2010.Rmd
1828 lines (1465 loc) · 81.7 KB
/
SPHC-2002-2006-2010.Rmd
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
---
title: "Fluidity of self-reported sexual identity in Stockholm County from 2010 to 2021"
author: Guoqiang Zhang, Maya Mathur, Matteo Quartagno
email: guoqiang.zhang@ki.se
output: html_notebook
editor_options:
chunk_output_type: console
---
Notes:
This R Notebook presents the analyses among the participants in the pooled cohort across SPHC 2002, 2006, and 2010, who provided data on sexual identity in at least two of the three follow-up survey years (2010, 2014, and 2021). The analyses include:
- Visual presentation of change in sexual identity in 2010-2021.
- Relationship between change in sexual identity and key demographic factors.
The analyses of relationship between change in sexual identity and key demographic factors in SPHC 2010 and 2014 are available, respectively, in the R Notebooks "SPHC-2010.Rmd" and "SPHC-2014.Rmd".
### 1. Load Packages
```{r}
library(haven)
library(naniar)
library(dplyr)
library(tidyverse)
library(finalfit)
library(ggplot2)
library(ggalluvial)
library(ggbreak)
library(ggrepel)
library(grDevices)
library(sandwich)
library(lmtest)
source("/Users/guoqiang.zhang/Documents/Karolinska Institutet/Research Projects/Trends and Fluidity of Sexual Identity/Sexual_identity_in_Stockholm_County/Helper_functions.R") # helper function
```
### 2. The Stockholm Public Health Cohort
#### 2.1. SPHC 2002
```{r}
d_2002 <- read_sas("/Volumes/LGBT Project data/sphc02_07_10_14_21.sas7bdat")
# sexual identity in 2010
table( d_2002$F10F103, useNA = "always" )
d_2002$F10F103[ d_2002$F10F103 == 9 ] <- NA
d_2002$sexual_identity_2010 <- factor( ifelse( d_2002$F10F103 == 1, "Heterosexual",
ifelse( d_2002$F10F103 == 2, "Homosexual",
ifelse( d_2002$F10F103 == 3, "Bisexual", "Other" ) ) ),
levels = c( "Heterosexual", "Homosexual", "Bisexual", "Other" ) )
table( d_2002$sexual_identity_2010, useNA = "always" )
# sexual identity in 2014
table( d_2002$F14F103, useNA = "always" )
d_2002$sexual_identity_2014 <- factor( ifelse( d_2002$F14F103 == 1, "Heterosexual",
ifelse( d_2002$F14F103 == 2, "Homosexual",
ifelse( d_2002$F14F103 == 3, "Bisexual", "Other" ) ) ),
levels = c( "Heterosexual", "Homosexual", "Bisexual", "Other" ) )
table( d_2002$sexual_identity_2014, useNA = "always" )
# sexual identity in 2021
table( d_2002$F21F91, useNA = "always" )
d_2002$sexual_identity_2021 <- factor( ifelse( d_2002$F21F91 == 1, "Heterosexual",
ifelse( d_2002$F21F91 == 2, "Homosexual",
ifelse( d_2002$F21F91 == 3, "Bisexual", "Other" ) ) ),
levels = c( "Heterosexual", "Homosexual", "Bisexual", "Other" ) )
table( d_2002$sexual_identity_2021, useNA = "always" )
# sex
table( d_2002$kon, useNA = "always" )
d_2002$sex <- factor( ifelse( d_2002$kon == 1, "Male", "Female" ),
levels = c( "Male", "Female" ) )
table( d_2002$sex, useNA = "always" )
# age
summary( d_2002$F2alder )
d_2002$age_2002 <- d_2002$F2alder
d_2002$age_2010 <- d_2002$age_2002 + 8
d_2002$age_2010_cat <- factor( ifelse( d_2002$age_2010 <= 29, "16-29",
ifelse( d_2002$age_2010 >=30 & d_2002$age_2010 <= 44, "30-44",
ifelse( d_2002$age_2010 >= 45 & d_2002$age_2010 <= 59, "45-59", ">=60" ) ) ),
levels = c( "16-29", "30-44", "45-59", ">=60" ) )
table( d_2002$age_2010_cat, useNA = "always" )
# country of birth
table( d_2002$fodelseland, useNA = "always" )
d_2002$fodelseland[ d_2002$fodelseland == "" ] <- NA
d_2002$country_of_birth <- factor( ifelse( d_2002$fodelseland == "Afrika", "Africa",
ifelse( d_2002$fodelseland == "Asien", "Asia",
ifelse( d_2002$fodelseland == "Europa", "Europe",
ifelse( d_2002$fodelseland == "Sverige", "Sweden", "Americas" ) ) ) ),
levels = c( "Africa", "Americas", "Asia", "Europe", "Sweden" ) )
table( d_2002$country_of_birth, useNA = "always" )
# sampling strata
n_miss( d_2002$stratum )
d_2002$sampling_strata <- as.factor( d_2002$stratum )
length( unique( d_2002$sampling_strata ) )
strata_region_name <- readxl::read_xlsx("/Users/guoqiang.zhang/Documents/Karolinska Institutet/Research Projects/SPHC/Data for LGB projects/Stratum.xlsx")
strata_region_name$region_2002 <- as.factor( strata_region_name$region_2002 )
d_2002 <- d_2002 %>%
left_join( strata_region_name[ , c( 1, 5 ) ], by = c( "sampling_strata" = "region_2002" ) )
d_2002$sampling_strata_region <- d_2002$sdk_2002
d_2002$sampling_strata_region <- ifelse( d_2002$sampling_strata == "998" | d_2002$sampling_strata == "999", "Unknown",
d_2002$sampling_strata_region )
##### design weights #####
summary( d_2002$F2dvikt )
d_2002$design_weight <- d_2002$F2dvikt
##### non-response #####
# unit non-response
d_2002$design_weight_unit_nonresponse <- d_2002$F2dbvikt # weights calculated assuming Missing Completely At Random (MCAR) within each stratum
summary( d_2002$design_weight_unit_nonresponse )
sum( d_2002$design_weight_unit_nonresponse ) # No. of source population = 1,402,160
##### summary of stratified sampling #####
# because stratified random sampling was done by region and sex,
# the following analyses were conducted separately for males and females
# among males
d_2002_males <- d_2002 %>% filter( sex == "Male" )
unitresponse_prob <- d_2002_males %>%
group_by( sampling_strata_region ) %>%
summarise( unitresponse_prob = unique( design_weight ) / unique( design_weight_unit_nonresponse ),
no.of.population = sum( design_weight_unit_nonresponse ),
sample_size = unique( no.of.population )/unique( design_weight ) ) # calculate overall unit response rate, and no. of population and sample size within each stratum
d_2002_males <- d_2002_males %>%
left_join( unitresponse_prob, by = "sampling_strata_region" )
itemresponse_prob <- d_2002_males %>%
group_by( sampling_strata_region ) %>%
summarise( itemresponse_prob = sum( !is.na( sexual_identity_2010 ) ) / n() ) # calculate item response rate
d_2002_males <- d_2002_males %>%
left_join( itemresponse_prob, by = "sampling_strata_region" ) %>%
mutate( itemresponse_prob = ifelse( is.na( sexual_identity_2010 ), 0, itemresponse_prob ) )
sampling_frame_2002_males <- as.data.frame( d_2002_males %>%
group_by( sampling_strata_region ) %>%
reframe( no.of.population = unique( no.of.population ),
sample_size = unique( no.of.population/design_weight ),
unitresponse = n(),
itemresponse = sum( itemresponse_prob != 0 ) ) )
sampling_frame_2002_males$unitresponse_rate <- sampling_frame_2002_males$unitresponse/sampling_frame_2002_males$sample_size
sampling_frame_2002_males$itemresponse_rate <- sampling_frame_2002_males$itemresponse/sampling_frame_2002_males$unitresponse
sampling_frame_2002_males$overallresponse_rate <- sampling_frame_2002_males$itemresponse/sampling_frame_2002_males$sample_size
sampling_frame_2002_males$unitresponse_label <- paste0( sampling_frame_2002_males$unitresponse, " (",
sprintf( "%.1f", sampling_frame_2002_males$unitresponse_rate*100 ), "%)" )
sampling_frame_2002_males$overallresponse_label <- paste0( sampling_frame_2002_males$itemresponse, " (",
sprintf( "%.1f", sampling_frame_2002_males$overallresponse_rate*100 ), "%)" )
round( sum( sampling_frame_2002_males$unitresponse )/( sum( sampling_frame_2002_males$sample_size ) ), 3 ) # overall unit response rate
round( sum( sampling_frame_2002_males$itemresponse )/( sum( sampling_frame_2002_males$unitresponse ) ), 3 ) # overall item response rate
round( sum( sampling_frame_2002_males$itemresponse )/( sum( sampling_frame_2002_males$sample_size ) ), 3 ) # overall response rate
writexl::write_xlsx( sampling_frame_2002_males, "sampling_frame_2002_males.xlsx" )
# among females
d_2002_females <- d_2002 %>% filter( sex == "Female" )
unitresponse_prob <- d_2002_females %>%
group_by( sampling_strata_region ) %>%
summarise( unitresponse_prob = unique( design_weight ) / unique( design_weight_unit_nonresponse ),
no.of.population = sum( design_weight_unit_nonresponse ),
sample_size = unique( no.of.population )/unique( design_weight ) ) # calculate overall unit response rate, and no. of population and sample size within each stratum
d_2002_females <- d_2002_females %>%
left_join( unitresponse_prob, by = "sampling_strata_region" )
itemresponse_prob <- d_2002_females %>%
group_by( sampling_strata_region ) %>%
summarise( itemresponse_prob = sum( !is.na( sexual_identity_2010 ) ) / n() ) # calculate item response rate
d_2002_females <- d_2002_females %>%
left_join( itemresponse_prob, by = "sampling_strata_region" ) %>%
mutate( itemresponse_prob = ifelse( is.na( sexual_identity_2010 ), 0, itemresponse_prob ) )
sampling_frame_2002_females <- as.data.frame( d_2002_females %>%
group_by( sampling_strata_region ) %>%
reframe( no.of.population = unique( no.of.population ),
sample_size = unique( no.of.population/design_weight ),
unitresponse = n(),
itemresponse = sum( itemresponse_prob != 0 ) ) )
sampling_frame_2002_females$unitresponse_rate <- sampling_frame_2002_females$unitresponse/sampling_frame_2002_females$sample_size
sampling_frame_2002_females$itemresponse_rate <- sampling_frame_2002_females$itemresponse/sampling_frame_2002_females$unitresponse
sampling_frame_2002_females$overallresponse_rate <- sampling_frame_2002_females$itemresponse/sampling_frame_2002_females$sample_size
sampling_frame_2002_females$unitresponse_label <- paste0( sampling_frame_2002_females$unitresponse, " (",
sprintf( "%.1f", sampling_frame_2002_females$unitresponse_rate*100 ), "%)" )
sampling_frame_2002_females$overallresponse_label <- paste0( sampling_frame_2002_females$itemresponse, " (",
sprintf( "%.1f", sampling_frame_2002_females$overallresponse_rate*100 ), "%)" )
round( sum( sampling_frame_2002_females$unitresponse )/( sum( sampling_frame_2002_females$sample_size ) ), 3 ) # overall unit response rate
round( sum( sampling_frame_2002_females$itemresponse )/( sum( sampling_frame_2002_females$unitresponse ) ), 3 ) # overall item response rate
round( sum( sampling_frame_2002_females$itemresponse )/( sum( sampling_frame_2002_females$sample_size ) ), 3 ) # overall response rate
writexl::write_xlsx( sampling_frame_2002_females, "sampling_frame_2002_females.xlsx" )
```
#### 2.2. SPHC 2006
```{r}
d_2006 <- read_sas("/Volumes/LGBT Project data/sphc06_10_14_21.sas7bdat")
# sexual identity in 2010
table( d_2006$F10F103, useNA = "always" )
d_2006$F10F103[ d_2006$F10F103 == 9 ] <- NA
d_2006$sexual_identity_2010 <- factor( ifelse( d_2006$F10F103 == 1, "Heterosexual",
ifelse( d_2006$F10F103 == 2, "Homosexual",
ifelse( d_2006$F10F103 == 3, "Bisexual", "Other" ) ) ),
levels = c( "Heterosexual", "Homosexual", "Bisexual", "Other" ) )
table( d_2006$sexual_identity_2010, useNA = "always" )
# sexual identity in 2014
table( d_2006$F14F103, useNA = "always" )
d_2006$sexual_identity_2014 <- factor( ifelse( d_2006$F14F103 == 1, "Heterosexual",
ifelse( d_2006$F14F103 == 2, "Homosexual",
ifelse( d_2006$F14F103 == 3, "Bisexual", "Other" ) ) ),
levels = c( "Heterosexual", "Homosexual", "Bisexual", "Other" ) )
table( d_2006$sexual_identity_2014, useNA = "always" )
# sexual identity in 2021
table( d_2006$F21F91, useNA = "always" )
d_2006$sexual_identity_2021 <- factor( ifelse( d_2006$F21F91 == 1, "Heterosexual",
ifelse( d_2006$F21F91 == 2, "Homosexual",
ifelse( d_2006$F21F91 == 3, "Bisexual", "Other" ) ) ),
levels = c( "Heterosexual", "Homosexual", "Bisexual", "Other" ) )
table( d_2006$sexual_identity_2021, useNA = "always" )
# sex
table( d_2006$kon, useNA = "always" )
d_2006$sex <- factor( ifelse( d_2006$kon == 1, "Male", "Female" ),
levels = c( "Male", "Female" ) )
table( d_2006$sex, useNA = "always" )
# age
summary( d_2006$F6alder )
d_2006$age_2006 <- d_2006$F6alder
d_2006$age_2010 <- d_2006$age_2006 + 4
d_2006$age_2010_cat <- factor( ifelse( d_2006$age_2010 <= 29, "16-29",
ifelse( d_2006$age_2010 >=30 & d_2006$age_2010 <= 44, "30-44",
ifelse( d_2006$age_2010 >= 45 & d_2006$age_2010 <= 59, "45-59", ">=60" ) ) ),
levels = c( "16-29", "30-44", "45-59", ">=60" ) )
table( d_2006$age_2010_cat, useNA = "always" )
# country of birth
table( d_2006$fodelseland, useNA = "always" )
d_2006$country_of_birth <- factor( ifelse( d_2006$fodelseland == "Afrika", "Africa",
ifelse( d_2006$fodelseland == "Asien", "Asia",
ifelse( d_2006$fodelseland == "Europa", "Europe",
ifelse( d_2006$fodelseland == "Sverige", "Sweden", "Americas" ) ) ) ),
levels = c( "Africa", "Americas", "Asia", "Europe", "Sweden" ) )
table( d_2006$country_of_birth, useNA = "always" )
# sampling strata
n_miss( d_2006$stratum )
d_2006$sampling_strata <- as.factor( d_2006$stratum )
length( unique( d_2006$sampling_strata ) ) # 43 strata
strata_region_name <- readxl::read_xlsx("/Users/guoqiang.zhang/Documents/Karolinska Institutet/Research Projects/SPHC/Data for LGB projects/Stratum.xlsx")
strata_region_name$region_2006 <- as.factor( strata_region_name$region_2006 )
d_2006 <- d_2006 %>%
left_join( strata_region_name[ , c( 2, 6 ) ], by = c( "sampling_strata" = "region_2006" ) )
d_2006$sampling_strata_region <- d_2006$sdk_2006
##### design weights #####
summary( d_2006$F6dvikt )
d_2006$design_weight <- d_2006$F6dvikt
##### non-response #####
# unit non-response
d_2006$design_weight_unit_nonresponse <- d_2006$F6dbvikt # weights calculated assuming Missing Completely At Random (MCAR) within each stratum
summary( d_2006$design_weight_unit_nonresponse )
sum( d_2006$design_weight_unit_nonresponse ) # No. of source population = 1,450,501
unitresponse_prob <- d_2006 %>%
group_by( sampling_strata_region ) %>%
summarise( unitresponse_prob = unique( design_weight ) / unique( design_weight_unit_nonresponse ),
no.of.population = sum( design_weight_unit_nonresponse ),
sample_size = unique( no.of.population )/unique( design_weight ) ) # calculate overall unit response rate, and no. of population and sample size within each stratum
d_2006 <- d_2006 %>%
left_join( unitresponse_prob, by = "sampling_strata_region" )
# item non-response
itemresponse_prob <- d_2006 %>%
group_by( sampling_strata_region ) %>%
summarise( itemresponse_prob = sum( !is.na( sexual_identity_2010 ) ) / n() ) # calculate item response rate
d_2006 <- d_2006 %>%
left_join( itemresponse_prob, by = "sampling_strata_region" ) %>%
mutate( itemresponse_prob = ifelse( is.na( sexual_identity_2010 ), 0, itemresponse_prob ) )
##### summary of stratified sampling #####
sampling_frame_2006 <- as.data.frame( d_2006 %>%
group_by( sampling_strata_region ) %>%
reframe( no.of.population = unique( no.of.population ),
sample_size = unique( no.of.population/design_weight ),
unitresponse = n(),
itemresponse = sum( itemresponse_prob != 0 ) ) )
sampling_frame_2006 <- sampling_frame_2006 %>%
left_join( strata_region_name[ , c( 2, 6 ) ], by = c( "sampling_strata_region" = "sdk_2006" ) ) %>%
arrange( region_2006 )
sampling_frame_2006$unitresponse_rate <- sampling_frame_2006$unitresponse/sampling_frame_2006$sample_size
sampling_frame_2006$itemresponse_rate <- sampling_frame_2006$itemresponse/sampling_frame_2006$unitresponse
sampling_frame_2006$overallresponse_rate <- sampling_frame_2006$itemresponse/sampling_frame_2006$sample_size
sampling_frame_2006$unitresponse_label <- paste0( sampling_frame_2006$unitresponse, " (",
sprintf( "%.1f", sampling_frame_2006$unitresponse_rate*100 ), "%)" )
sampling_frame_2006$overallresponse_label <- paste0( sampling_frame_2006$itemresponse, " (",
sprintf( "%.1f", sampling_frame_2006$overallresponse_rate*100 ), "%)" )
round( sum( sampling_frame_2006$unitresponse )/( sum( sampling_frame_2006$sample_size ) ), 3 ) # overall unit response rate
round( sum( sampling_frame_2006$itemresponse )/( sum( sampling_frame_2006$unitresponse ) ), 3 ) # overall item response rate
round( sum( sampling_frame_2006$itemresponse )/( sum( sampling_frame_2006$sample_size ) ), 3 ) # overall response rate
writexl::write_xlsx( sampling_frame_2006, "sampling_frame_2006.xlsx" )
```
#### 2.3. SPHC 2010
```{r}
load("/Volumes/LGBT Project data/d_2010.RData")
# sexual identity in 2010
table( d_2010$F10U87G78, useNA = "always" )
d_2010$F10U87G78[ d_2010$F10U87G78 == 9 ] <- NA
d_2010$sexual_identity_2010 <- factor( ifelse( d_2010$F10U87G78 == 1, "Heterosexual",
ifelse( d_2010$F10U87G78 == 2, "Homosexual",
ifelse( d_2010$F10U87G78 == 3, "Bisexual", "Other" ) ) ),
levels = c( "Heterosexual", "Homosexual", "Bisexual", "Other" ) )
table( d_2010$sexual_identity_2010, useNA = "always" )
# sexual identity in 2014
table( d_2010$F14F103, useNA = "always" )
d_2010$sexual_identity_2014 <- factor( ifelse( d_2010$F14F103 == 1, "Heterosexual",
ifelse( d_2010$F14F103 == 2, "Homosexual",
ifelse( d_2010$F14F103 == 3, "Bisexual", "Other" ) ) ),
levels = c( "Heterosexual", "Homosexual", "Bisexual", "Other" ) )
table( d_2010$sexual_identity_2014, useNA = "always" )
# sexual identity in 2021
table( d_2010$F21F91, useNA = "always" )
d_2010$sexual_identity_2021 <- factor( ifelse( d_2010$F21F91 == 1, "Heterosexual",
ifelse( d_2010$F21F91 == 2, "Homosexual",
ifelse( d_2010$F21F91 == 3, "Bisexual", "Other" ) ) ),
levels = c( "Heterosexual", "Homosexual", "Bisexual", "Other" ) )
table( d_2010$sexual_identity_2021, useNA = "always" )
# sex
table( d_2010$kon, useNA = "always" )
d_2010$sex <- factor( ifelse( d_2010$kon == 1, "Male", "Female" ),
levels = c( "Male", "Female" ) )
table( d_2010$sex, useNA = "always" )
# age
summary( d_2010$F10alder )
d_2010$age_2010 <- d_2010$F10alder
d_2010$age_2010_cat <- factor( ifelse( d_2010$age_2010 <= 29, "16-29",
ifelse( d_2010$age_2010 >=30 & d_2010$age_2010 <= 44, "30-44",
ifelse( d_2010$age_2010 >= 45 & d_2010$age_2010 <= 59, "45-59", ">=60" ) ) ),
levels = c( "16-29", "30-44", "45-59", ">=60" ) )
table( d_2010$age_2010_cat, useNA = "always" )
# country of birth
table( d_2010$fodelseland, useNA = "always" )
d_2010$country_of_birth <- factor( ifelse( d_2010$fodelseland == "Afrika", "Africa",
ifelse( d_2010$fodelseland == "Asien", "Asia",
ifelse( d_2010$fodelseland == "Europa", "Europe",
ifelse( d_2010$fodelseland == "Sverige", "Sweden", "Americas" ) ) ) ),
levels = c( "Africa", "Americas", "Asia", "Europe", "Sweden" ) )
table( d_2010$country_of_birth, useNA = "always" )
# education
table( d_2010$utbniva2010, useNA = "always" )
d_2010$education <- factor( ifelse( d_2010$utbniva2010 <= 2, "<=9 years",
ifelse( d_2010$utbniva2010 <= 4, "10-12 years", ">=13 years" ) ),
levels = c( "<=9 years", "10-12 years", ">=13 years" ) )
table( d_2010$education, useNA = "always" )
# disposable income
summary( d_2010$dispink2010, useNA = "always" )
d_2010$dispink2010 <- d_2010$dispink2010*( 1960/1733 ) # Consumer Price Index (CPI)-adjusted income
d_2010$income <- factor( ifelse( d_2010$dispink2010 <= 2500, "<=2,500",
ifelse( d_2010$dispink2010 > 2500 & d_2010$dispink2010 <= 3500, "(2,500, 3,500]",
ifelse( d_2010$dispink2010 > 3500 & d_2010$dispink2010 <= 4500, "(3,500, 4,500]", ">4,500" ) ) ),
levels = c( "<=2,500", "(2,500, 3,500]", "(3,500, 4,500]", ">4,500" ) )
table( d_2010$income, useNA = "always" )
# marital status
table( d_2010$civil2010, useNA = "always" )
d_2010$marital_status <- factor( ifelse( d_2010$civil2010 == "G" | d_2010$civil2010 == "RP", "Currently married",
ifelse( d_2010$civil2010 == "OG", "Never married", "Other" ) ),
levels = c( "Never married", "Currently married", "Other" ) )
table( d_2010$marital_status, useNA = "always" )
# living alone
table( d_2010$F10U53aG57a, useNA = "always" )
d_2010$F10U53aG57a[ d_2010$F10U53aG57a == 9 ] <- NA
d_2010$living_alone <- factor( ifelse( d_2010$F10U53aG57a == 1, "no", "yes" ),
levels = c( "yes", "no" ) )
table( d_2010$living_alone, useNA = "always" )
# personal support
table( d_2010$F10U57G62, useNA = "always" )
d_2010$F10U57G62[ d_2010$F10U57G62 == 9 ] <- NA
d_2010$personal_support <- factor( ifelse( d_2010$F10U57G62 <= 2, "yes", "no" ),
levels = c( "yes", "no" ) )
table( d_2010$personal_support, useNA = "always" )
# sampling strata
n_miss( d_2010$stratum )
d_2010$sampling_strata <- as.factor( d_2010$stratum )
length( unique( d_2010$sampling_strata ) ) # 39 strata
strata_region_name$region_2010 <- as.factor( strata_region_name$region_2010 )
d_2010 <- d_2010 %>%
left_join( strata_region_name[ , c( 3, 7 ) ], by = c( "sampling_strata" = "region_2010" ) )
d_2010$sampling_strata_region <- d_2010$sdk_2010
```
#### 2.4. Merge data across surveys
```{r}
##### obtain follow-up data in 2010-2021 for SPHC-B 2002 and 2006 #####
# there are 999 individuals who participated in both SPHC-B 2002 and 2006
# their follow-up data are included only once
variable_list <- c( "lopnr", "sexual_identity_2010", "sexual_identity_2014", "sexual_identity_2021", "age_2010", "sex", "country_of_birth", "sampling_strata_region" )
m1 <- d_2002[ , variable_list ] %>%
mutate( year = rep( "SPHC 2002" ) )
m2 <- d_2006[ , variable_list ] %>%
mutate( year = rep( "SPHC 2006" ) )
m12 <- rbind( m1, m2 ) %>%
group_by( lopnr ) %>%
filter( !( year == "SPHC 2002" & any( year == "SPHC 2006" ) ) ) %>%
ungroup()
sphcf10 <- read_sas( "/Volumes/LGBT Project data/sphc-f10.sas7bdat",
col_select = c( "lopnr", "utbniva2010", "dispink2010", "civil2010", "F10F80a", "F10F84" ) )
m12_updated <- sphcf10 %>%
left_join( m12, by = "lopnr" )
# age
summary( m12_updated$age_2010 )
m12_updated$age_conf <- m12_updated$age_2010
m12_updated$age_exp <- factor( ifelse( m12_updated$age_2010 <= 29, "<=29",
ifelse( m12_updated$age_2010 >=30 & m12_updated$age_2010 <= 44, "30-44",
ifelse( m12_updated$age_2010 >= 45 & m12_updated$age_2010 <= 59, "45-59", ">=60" ) ) ),
levels = c( "45-59", "<=29", "30-44", ">=60" ) )
table( m12_updated$age_exp, useNA = "always" )
# sex
summary( m12_updated$sex )
m12_updated$sex_exp <-relevel( m12_updated$sex, ref = "Male" )
m12_updated$sex_conf <- m12_updated$sex
# country of birth
summary( m12_updated$country_of_birth )
m12_updated$country_of_birth <- factor( ifelse( m12_updated$country_of_birth == "Sweden", "Sweden",
ifelse( m12_updated$country_of_birth == "Europe", "Europe", "Outside Europe" ) ),
levels = c( "Sweden", "Europe", "Outside Europe" ) )
m12_updated$country_of_birth_exp <- m12_updated$country_of_birth
m12_updated$country_of_birth_conf <- m12_updated$country_of_birth
table( m12_updated$country_of_birth_exp, useNA = "always" )
# education
table( m12_updated$utbniva2010, useNA = "always" )
m12_updated$education <- factor( ifelse( m12_updated$utbniva2010 <= 2, "<=9 years",
ifelse( m12_updated$utbniva2010 <= 4, "10-12 years", ">=13 years" ) ),
levels = c( "<=9 years", "10-12 years", ">=13 years" ) )
m12_updated$education_exp <- relevel( m12_updated$education, ref = ">=13 years" )
m12_updated$education_conf <- m12_updated$education
table( m12_updated$education_exp, useNA = "always" )
# disposable income
summary( m12_updated$dispink2010, useNA = "always" )
m12_updated$dispink2010 <- m12_updated$dispink2010*( 1960/1733 ) # Consumer Price Index (CPI)-adjusted income
m12_updated$income_conf <- m12_updated$dispink2010
m12_updated$income_exp <- factor( ifelse( m12_updated$dispink2010 <= 2500, "<=2,500",
ifelse( m12_updated$dispink2010 > 2500 & m12_updated$dispink2010 <= 3500, "(2,500, 3,500]",
ifelse( m12_updated$dispink2010 > 3500 & m12_updated$dispink2010 <= 4500, "(3,500, 4,500]", ">4,500" ) ) ),
levels = c( ">4,500", "<=2,500", "(2,500, 3,500]", "(3,500, 4,500]" ) )
table( m12_updated$income_exp, useNA = "always" )
# marital status
table( m12_updated$civil2010, useNA = "always" )
m12_updated$civil2010[ m12_updated$civil2010 == "" ] <- NA
m12_updated$marital_status_exp <- factor( ifelse( m12_updated$civil2010 == "G" | m12_updated$civil2010 == "RP", "Currently married",
ifelse( m12_updated$civil2010 == "OG", "Never married", "Other" ) ),
levels = c( "Currently married", "Never married", "Other" ) )
m12_updated$marital_status_conf <- m12_updated$marital_status_exp
table( m12_updated$marital_status_exp, useNA = "always" )
# living alone
table( m12_updated$F10F80a, useNA = "always" )
m12_updated$living_status_conf <- as.factor( m12_updated$F10F80a )
m12_updated$living_status_exp <- factor( ifelse( m12_updated$F10F80a == 1, "no", "yes" ),
levels = c( "no", "yes" ) )
table( m12_updated$living_status_exp, useNA = "always" )
# personal support
table( m12_updated$F10F84, useNA = "always" )
m12_updated$personal_support <- factor( ifelse( m12_updated$F10F84 <= 2, "yes", "no" ),
levels = c( "yes", "no" ) )
m12_updated$personal_support_exp <- m12_updated$personal_support
m12_updated$personal_support_conf <- m12_updated$personal_support
table( m12_updated$personal_support_exp, useNA = "always" )
# year
table( m12_updated$year, useNA = "always" )
m12_updated$year_conf <- as.factor( m12_updated$year )
m12_updated$year_exp <- factor( m12_updated$year, levels = c( "SPHC 2002", "SPHC 2006" ) )
summary( m12_updated )
d_2002_2006_follow_up <- m12_updated %>%
select( "sexual_identity_2010", "sexual_identity_2014", "sexual_identity_2021", "age_exp", "age_conf", "sex_exp", "sex_conf", "country_of_birth_exp", "country_of_birth_conf", "education_exp", "education_conf", "income_exp", "income_conf", "marital_status_exp", "marital_status_conf", "living_status_exp", "living_status_conf", "personal_support_exp", "personal_support_conf", "year_exp", "year_conf" )
##### obtain follow-up data in 2010-2021 for SPHC-B 2010 #####
d_2010 <- d_2010 %>% mutate( year = rep( "SPHC 2010" ) )
# age
summary( d_2010$age_2010 )
d_2010$age_conf <- d_2010$age_2010
d_2010$age_exp <- factor( ifelse( d_2010$age_2010 <= 29, "<=29",
ifelse( d_2010$age_2010 >=30 & d_2010$age_2010 <= 44, "30-44",
ifelse( d_2010$age_2010 >= 45 & d_2010$age_2010 <= 59, "45-59", ">=60" ) ) ),
levels = c( "45-59", "<=29", "30-44", ">=60" ) )
table( d_2010$age_exp, useNA = "always" )
# sex
summary( d_2010$sex )
d_2010$sex_conf <- d_2010$sex
d_2010$sex_exp <- relevel( d_2010$sex, ref = "Male" )
# country of birth
summary( d_2010$country_of_birth )
d_2010$country_of_birth <- factor( ifelse( d_2010$country_of_birth == "Sweden", "Sweden",
ifelse( d_2010$country_of_birth == "Europe", "Europe", "Outside Europe" ) ),
levels = c( "Sweden", "Europe", "Outside Europe" ) )
d_2010$country_of_birth_exp <- d_2010$country_of_birth
d_2010$country_of_birth_conf <- d_2010$country_of_birth
# education
summary( d_2010$education )
d_2010$education_conf <- d_2010$education
d_2010$education_exp <- relevel( d_2010$education, ref = ">=13 years" )
# disposable income
summary( d_2010$dispink2010 )
summary( d_2010$income )
d_2010$income_conf <- d_2010$dispink2010
d_2010$income_exp <- relevel( d_2010$income, ref = ">4,500" )
# marital status
summary( d_2010$marital_status )
d_2010$marital_status_conf <- d_2010$marital_status
d_2010$marital_status_exp <- relevel( d_2010$marital_status, ref = "Currently married" )
# living alone
table( d_2010$F10U53aG57a, useNA = "always" )
d_2010$F10U53aG57a[ d_2010$F10U53aG57a == 9 ] <- NA
summary( d_2010$living_alone )
d_2010$living_status_conf <- as.factor( d_2010$F10U53aG57a )
d_2010$living_status_exp <- relevel( d_2010$living_alone, ref = "no" )
# personal support
summary( d_2010$personal_support )
d_2010$personal_support_conf <- d_2010$personal_support
d_2010$personal_support_exp <- relevel( d_2010$personal_support, ref = "yes" )
# year
table( d_2010$year, useNA = "always" )
d_2010$year_conf <- as.factor( d_2010$year )
d_2010$year_exp <- as.factor( d_2010$year )
summary( d_2010 )
d_2010_follow_up <- d_2010 %>%
select( "sexual_identity_2010", "sexual_identity_2014", "sexual_identity_2021", "age_exp", "age_conf", "sex_exp", "sex_conf", "country_of_birth_exp", "country_of_birth_conf", "education_exp", "education_conf", "income_exp", "income_conf", "marital_status_exp", "marital_status_conf", "living_status_exp", "living_status_conf", "personal_support_exp", "personal_support_conf", "year_exp", "year_conf" )
##### merge follow-up data in 2010-2021 for SPHC-B 2002, 2006, and 2010 #####
d_2002_2006_2010_follow_up <- rbind( d_2002_2006_follow_up, d_2010_follow_up )
# define outcome
d_2002_2006_2010_follow_up$sexual_identity_fluidity <- ifelse(
d_2002_2006_2010_follow_up$sexual_identity_2010 != d_2002_2006_2010_follow_up$sexual_identity_2014 |
d_2002_2006_2010_follow_up$sexual_identity_2010 != d_2002_2006_2010_follow_up$sexual_identity_2021 |
d_2002_2006_2010_follow_up$sexual_identity_2014 != d_2002_2006_2010_follow_up$sexual_identity_2021,
1, 0 ) # any change of sexual identity in 2010-2021
d_sexual_identity_fluidity <- d_2002_2006_2010_follow_up[
!is.na( d_2002_2006_2010_follow_up$sexual_identity_2010 ) &
!is.na( d_2002_2006_2010_follow_up$sexual_identity_2014 ) &
!is.na( d_2002_2006_2010_follow_up$sexual_identity_2021 ), c( "sexual_identity_2010", "sexual_identity_2014", "sexual_identity_2021", "age_exp", "sex_exp" ) ]
nrow( d_sexual_identity_fluidity ) # 34,815 provided data on sexual identity in 2010-2021
# calculate the number of people who changed their sexual identity at least once
changed_once <- sum(
d_sexual_identity_fluidity$sexual_identity_2010 != d_sexual_identity_fluidity$sexual_identity_2014 |
d_sexual_identity_fluidity$sexual_identity_2010 != d_sexual_identity_fluidity$sexual_identity_2021 |
d_sexual_identity_fluidity$sexual_identity_2014 != d_sexual_identity_fluidity$sexual_identity_2021
)
# calculate the number of people who changed their sexual identity twice
changed_twice <- sum(
d_sexual_identity_fluidity$sexual_identity_2010 != d_sexual_identity_fluidity$sexual_identity_2014 &
d_sexual_identity_fluidity$sexual_identity_2014 != d_sexual_identity_fluidity$sexual_identity_2021
)
d_fluidity_model <- subset( d_2002_2006_2010_follow_up,
select = -c( sexual_identity_2014, sexual_identity_2021 ) ) %>%
filter( !is.na( sexual_identity_fluidity ) )
miss_case_summary( d_fluidity_model ) # no cases had missing data on all variables
nrow( d_fluidity_model ) # 36,398 participants with (incomplete) follow-up data on demographic variables
# sexual identity in 2010
summary( d_fluidity_model$sexual_identity_2010 )
d_fluidity_model$sexual_identity_2010_exp <- relevel( d_fluidity_model$sexual_identity_2010, ref = "Heterosexual" )
d_fluidity_model$sexual_identity_2010_conf <- d_fluidity_model$sexual_identity_2010
# make characteristics table by change in sexual identity
explanatory = c( "year_exp", "sex_exp", "age_exp", "country_of_birth_exp", "education_exp", "income_exp", "marital_status_exp", "living_status_exp", "personal_support_exp", "sexual_identity_2010" )
dependent = "sexual_identity_fluidity"
d_follow_up_table <- d_fluidity_model %>%
mutate( sexual_identity_fluidity = factor( sexual_identity_fluidity, levels = c( 0, 1 ), labels = c( "no change", "change" ) ) ) %>%
summary_factorlist( dependent,
explanatory,
na_include = TRUE,
total_col = TRUE,
add_col_totals = TRUE,
column = FALSE )
# Fisher's test
x1 <- table( d_fluidity_model$year_exp, d_fluidity_model$sexual_identity_fluidity )
x1
format( round( fisher.test( x1, simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
x2 <- table( d_fluidity_model$sex_exp, d_fluidity_model$sexual_identity_fluidity )
x2
format( round( fisher.test( x2 )$p.value, 3 ), nsmall = 3 )
x3 <- table( d_fluidity_model$age_exp, d_fluidity_model$sexual_identity_fluidity )
x3
format( round( fisher.test( x3, simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
x4 <- table( d_fluidity_model$country_of_birth_exp, d_fluidity_model$sexual_identity_fluidity )
x4
format( round( fisher.test( x4, simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
x5 <- table( d_fluidity_model$education_exp, d_fluidity_model$sexual_identity_fluidity )
x5
format( round( fisher.test( x5, simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
x6 <- table( d_fluidity_model$income_exp, d_fluidity_model$sexual_identity_fluidity )
x6
format( round( fisher.test( x6, simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
x7 <- table( d_fluidity_model$marital_status_exp, d_fluidity_model$sexual_identity_fluidity )
x7
format( round( fisher.test( x7, simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
x8 <- table( d_fluidity_model$living_status_exp, d_fluidity_model$sexual_identity_fluidity )
x8
format( round( fisher.test( x8, simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
x9 <- table( d_fluidity_model$personal_support_exp, d_fluidity_model$sexual_identity_fluidity )
x9
format( round( fisher.test( x9, simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
x10 <- table( d_fluidity_model$sexual_identity_2010, d_fluidity_model$sexual_identity_fluidity )
x10
format( round( fisher.test( x10, simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
```
### 3. Make Fluidity Plots
#### 3.1. Prepare dataset for plotting
##### 3.1.1. Among all participants
```{r}
# change in sexual identity from 2010 to 2021
m123_all <- d_sexual_identity_fluidity %>%
count( sexual_identity_2010, sexual_identity_2014, sexual_identity_2021, name = "number" ) %>%
mutate( percent = number / sum( number ) )
m123_all <- m123_all %>%
group_by( sexual_identity_2010 ) %>%
mutate( percent_for_label = number / sum( number ) ) %>%
ungroup()
m123_all$label <- paste0( prettyNum( m123_all$number, big.mark = "," , preserve.width = "none" ),
" (", sprintf( "%.1f", m123_all$percent_for_label*100 ), "%)"
)
for ( col in 1:3 ) {
m123_all[[col]] <- factor( m123_all[[col]], levels = c( "Homosexual", "Bisexual", "Other", "Heterosexual" ) )
}
m123_all_long <- to_lodes_form( m123_all, axes = 1:3, key = "year", value = "sexual_identity", id = "Cohort", diffuse = sexual_identity_2010 ) # convert to long format
# change in sexual identity from 2010-2014 to 2021
x123_all <- d_sexual_identity_fluidity %>%
count( sexual_identity_2010, sexual_identity_2014, sexual_identity_2021, name = "number" ) %>%
mutate( sexual_identity_2010_2014 = paste( sexual_identity_2010,
sexual_identity_2014,
sep = "_"
) )
x123_all$percent <- x123_all$number/sum( x123_all$number )
ordered_levels <- x123_all %>%
group_by( sexual_identity_2010_2014 ) %>%
summarize( total = sum( number ) ) %>%
arrange( desc( total ) ) %>%
pull( sexual_identity_2010_2014 )
ordered_levels <- as.character( ordered_levels[ ordered_levels != "Heterosexual_Heterosexual" &
ordered_levels != "Heterosexual_Other" ] )
ordered_levels <- c( ordered_levels, "Heterosexual_Other", "Heterosexual_Heterosexual" )
x123_all$sexual_identity_2010_2014 <- factor( x123_all$sexual_identity_2010_2014,
levels = ordered_levels )
x123_all$sexual_identity_2021 <- factor( x123_all$sexual_identity_2021,
levels = c( "Homosexual", "Bisexual", "Other", "Heterosexual" )
)
x123_all <- x123_all %>%
group_by( sexual_identity_2010_2014 ) %>%
mutate( total = sum( number ) ) %>%
mutate( percent_for_label = number / total ) %>%
ungroup()
x123_all$label <- paste0( prettyNum( x123_all$number, big.mark = "," , preserve.width = "none" ),
" (",
sprintf( "%.1f", x123_all$percent_for_label*100 ),
"%)" )
x123_all_long <- to_lodes_form( x123_all, axes = c( 5, 3 ), key = "year", value = "sexual_identity", id = "Cohort", diffuse = sexual_identity_2010_2014 )
```
##### 3.1.2. By sex
```{r}
m123_sex <- d_sexual_identity_fluidity %>%
count( sexual_identity_2010, sexual_identity_2014, sexual_identity_2021, sex_exp ) %>%
rename( number = n )
m123_sex <- m123_sex %>%
group_by( sex_exp ) %>%
mutate( percent = number / sum( number ) ) %>%
ungroup()
for ( col in 1:3 ) {
m123_sex[[col]] <- factor( m123_sex[[col]], levels = c( "Homosexual", "Bisexual", "Other", "Heterosexual" ) )
}
# among males
m123_male <- m123_sex %>%
filter( sex_exp == "Male" ) %>%
group_by( sexual_identity_2010 ) %>%
mutate( percent_for_label = number / sum( number ) ) %>%
ungroup()
m123_male$label <- paste0( prettyNum( m123_male$number, big.mark = "," , preserve.width = "none" ), " (",
sprintf( "%.1f", m123_male$percent_for_label*100 ),
"%)" )
m123_male_long <- to_lodes_form( m123_male, axes = 1:3, key = "year", value = "sexual_identity", id = "Cohort", diffuse = sexual_identity_2010 )
# among females
m123_female <- m123_sex %>%
filter( sex_exp == "Female" ) %>%
group_by( sexual_identity_2010 ) %>%
mutate( percent_for_label = number / sum( number ) ) %>%
ungroup()
m123_female$label <- paste0( prettyNum( m123_female$number, big.mark = "," , preserve.width = "none" ), " (",
sprintf( "%.1f", m123_female$percent_for_label*100 ),
"%)" )
m123_female_long <- to_lodes_form( m123_female, axes = 1:3, key = "year", value = "sexual_identity", id = "Cohort", diffuse = sexual_identity_2010 )
```
##### 3.1.3. By age
```{r}
summary( d_sexual_identity_fluidity$age_exp )
m123_age <- d_sexual_identity_fluidity %>%
count( sexual_identity_2010, sexual_identity_2014, sexual_identity_2021, age_exp ) %>%
rename( number = n ) %>%
group_by( age_exp ) %>%
mutate( percent = number / sum( number ) ) %>%
ungroup()
for ( col in 1:3 ) {
m123_age[[col]] <- factor( m123_age[[col]], levels = c( "Homosexual", "Bisexual", "Other", "Heterosexual" ) )
}
# 18-29 yrs
m123_age_18 <- m123_age %>%
filter( age_exp == "<=29" ) %>%
group_by( sexual_identity_2010 ) %>%
mutate( percent_for_label = number / sum( number ) ) %>%
ungroup()
m123_age_18$label <- paste0( prettyNum( m123_age_18$number, big.mark = "," , preserve.width = "none" ), " (",
sprintf( "%.1f", m123_age_18$percent_for_label*100 ),
"%)" )
m123_age_18_long <- to_lodes_form( m123_age_18, axes = 1:3, key = "year", value = "sexual_identity", id = "Cohort", diffuse = sexual_identity_2010 )
# 30-44 yrs
m123_age_30 <- m123_age %>%
filter( age_exp == "30-44" ) %>%
group_by( sexual_identity_2010 ) %>%
mutate( percent_for_label = number / sum( number ) ) %>%
ungroup()
m123_age_30$label <- paste0( prettyNum( m123_age_30$number, big.mark = "," , preserve.width = "none" ), " (",
sprintf( "%.1f", m123_age_30$percent_for_label*100 ),
"%)" )
m123_age_30_long <- to_lodes_form( m123_age_30, axes = 1:3, key = "year", value = "sexual_identity", id = "Cohort", diffuse = sexual_identity_2010 )
# 45-59 yrs
m123_age_45 <- m123_age %>%
filter( age_exp == "45-59" ) %>%
group_by( sexual_identity_2010 ) %>%
mutate( percent_for_label = number / sum( number ) ) %>%
ungroup()
m123_age_45$label <- paste0( prettyNum( m123_age_45$number, big.mark = "," , preserve.width = "none" ), " (",
sprintf( "%.1f", m123_age_45$percent_for_label*100 ),
"%)" )
m123_age_45_long <- to_lodes_form( m123_age_45, axes = 1:3, key = "year", value = "sexual_identity", id = "Cohort", diffuse = sexual_identity_2010 )
# >=60 yrs
m123_age_60 <- m123_age %>%
filter( age_exp == ">=60" ) %>%
group_by( sexual_identity_2010 ) %>%
mutate( percent_for_label = number / sum( number ) ) %>%
ungroup()
m123_age_60$label <- paste0( prettyNum( m123_age_60$number, big.mark = "," , preserve.width = "none" ), " (",
sprintf( "%.1f", m123_age_60$percent_for_label*100 ),
"%)" )
m123_age_60_long <- to_lodes_form( m123_age_60, axes = 1:3, key = "year", value = "sexual_identity", id = "Cohort", diffuse = sexual_identity_2010 )
```
#### 3.2. Plotting
##### 3.2.1. Among all participants
###### 3.2.1.1. Change in sexual identity from 2010 to 2021
```{r}
ggplot( m123_all_long,
aes( x = year,
stratum = sexual_identity,
alluvium = Cohort,
y = percent ) ) +
geom_alluvium( aes( fill = sexual_identity_2010 ), curve_type = "cubic" ) +
geom_stratum( aes( fill = sexual_identity_2010 ) ) +
geom_text( stat = "stratum", aes( label = after_stat( stratum ) ), family = "Arial", size = 12 / .pt ) +
scale_fill_manual( values = c( "#C77CFF", "#00BFC4", "#7CAE00", "#F8766D" ),
breaks = c( "Heterosexual", "Homosexual", "Bisexual", "Other" ),
na.value = NA ) +
scale_x_discrete( limits = c( "sexual_identity_2010", "sexual_identity_2014", "sexual_identity_2021" ), labels = c( "2010", "2014", "2021" ), expand = c( 0, 0 ) ) +
theme_classic() +
scale_y_break( c( 0.12, 0.9 ), scales = 10, space = 0.3 ) +
scale_y_continuous( labels = scales::percent, breaks = c( 0, 0.1, 0.9, 0.95, 1 ) ) +
labs( x = NULL, y = NULL) +
theme( axis.text.x = element_text( family = "Arial", size = 11 ),
axis.text.y = element_text( family = "Arial", size = 11 ),
legend.title = element_blank(),
legend.text = element_text( family = "Arial", size = 10 ),
legend.key = element_rect( colour = "white" ),
legend.position = "bottom",
axis.ticks.x.bottom = element_blank(),
axis.line.x.bottom = element_blank(),
axis.text.y.right = element_blank(),
axis.ticks.y.right = element_blank(),
axis.line.y.right = element_blank()
) +
geom_text_repel( stat = "alluvium",
aes( x = as.integer( year ) + 0.15,
color = factor( sexual_identity_2010,
levels = c( "Other", "Bisexual", "Homosexual", "Heterosexual" ) ),
label = ifelse( year == "sexual_identity_2021" &
( percent_for_label >= 0.1 | number >= 50 ), label, NA ) ),
size = 4.5,
family = "Arial",
segment.linetype = 3,
segment.size = 0.55,
direction = "y",
nudge_x = 0.45,
box.padding = 0.5,
point.padding = 0,
show.legend = FALSE,
seed = 123
)
```
###### 3.2.1.2. Change in sexual identity from 2014 to 2021
```{r}
all_colors <- c( "#00BFC4", "#7CAE00", "#FFA500", "#F8766D",
"yellow2", "#0092FF", "#00FF00", "#4B0082",
"#0000FF", "#DEB887", "#00A86B", "#FF0000",
"#FF00DB", "#E6E6FA", "#87CEEB", "#C77CFF" )
legend_labels <- setNames(
c( "Homo to Homo", "Bisexual to bisexual", "Hetero to bisexual", "Uncertain to other",
"Homo to Hetero", "Bisexual to Hetero", "Uncertain to Hetero", "Bisexual to other",
"Hetero to Homo", "Uncertain to bisexual", "Bisexual to Homo", "Homo to bisexual",
"Homo to other", "Uncertain to Homo", "Hetero to other", "Hetero to Hetero"),
ordered_levels )
ggplot( x123_all_long,
aes( x = year,
stratum = sexual_identity,
alluvium = Cohort,
y = percent ) ) +
geom_alluvium( aes( fill = sexual_identity_2010_2014 ), curve_type = "cubic" ) +
geom_stratum( aes( fill = sexual_identity_2010_2014 ) ) +
geom_text( data = subset( x123_all_long, year == "sexual_identity_2021"),
stat = "stratum",
aes( label = after_stat( stratum ) ),
family = "Arial",
size = 12 / .pt ) +
scale_fill_manual( values = all_colors,
breaks = ordered_levels,
labels = legend_labels,
na.value = NA ) +
scale_x_discrete( limits = c( "sexual_identity_2010_2014", "sexual_identity_2021" ), labels = c( "2010–2014", "2021" ), expand = c( 0, 0 ) ) +
theme_classic() +
scale_y_break( c( 0.12, 0.9 ), scales = 10, space = 0.3 ) +
scale_y_continuous( labels = scales::percent, breaks = c( 0, 0.1, 0.9, 0.95, 1 ) ) +
labs( x = NULL, y = NULL) +
theme( axis.text.x = element_text( family = "Arial", size = 11 ),
axis.text.y = element_text( family = "Arial", size = 11 ),
legend.title = element_blank(),
legend.text = element_text( family = "Arial", size = 10 ),
legend.key = element_rect( colour = "white" ),
legend.position = "bottom",
axis.ticks.x.bottom = element_blank(),
axis.line.x.bottom = element_blank(),
axis.text.y.right = element_blank(),
axis.ticks.y.right = element_blank(),
axis.line.y.right = element_blank()
) +
geom_text_repel( stat = "alluvium",
aes( x = as.integer( year ) + 0.15,
color = sexual_identity_2010_2014,
label = ifelse( year == "sexual_identity_2021" &
( percent_for_label >= 0.05 & number >= 25 ), label, NA ) ),
size = 4.5,