-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSummary_plots_imp.Rmd
176 lines (141 loc) · 8.43 KB
/
Summary_plots_imp.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
---
title: "Summary plots of imputation analyses"
author: Guoqiang Zhang
email: [email protected]
output: html_notebook
editor_options:
chunk_output_type: console
---
### 1. Load Packages
```{r}
library(readxl)
library(dplyr)
library(tidyr)
library(ggplot2)
```
### 2. Plots for Proportion of Sexual Identity
#### 2.1. By generation
```{r}
# datasets for proportion of sexual identity
prop_imp_summary_2010_generation <- read_excel("/Users/guoqiang.zhang/Library/CloudStorage/OneDrive-KarolinskaInstitutet/Karolinska Institutet/Research Projects/Population Trends and Individual Fluidity of Sexual Identity in Stockholm County/Research Letter/Population_Trends_and_Individual_Fluidity_of_Sexual_Identity_in_Stockholm_County_2010_to_2021/prop_imp_summary_2010.xlsx")
prop_imp_summary_2014_generation <- read_excel("/Users/guoqiang.zhang/Library/CloudStorage/OneDrive-KarolinskaInstitutet/Karolinska Institutet/Research Projects/Population Trends and Individual Fluidity of Sexual Identity in Stockholm County/Research Letter/Population_Trends_and_Individual_Fluidity_of_Sexual_Identity_in_Stockholm_County_2010_to_2021/prop_imp_summary_2014.xlsx")
prop_imp_summary_2021_generation <- read_excel("/Users/guoqiang.zhang/Library/CloudStorage/OneDrive-KarolinskaInstitutet/Karolinska Institutet/Research Projects/Population Trends and Individual Fluidity of Sexual Identity in Stockholm County/Research Letter/Population_Trends_and_Individual_Fluidity_of_Sexual_Identity_in_Stockholm_County_2010_to_2021/prop_imp_summary_2021.xlsx")
prop_imp_summary_2010_generation <- prop_imp_summary_2010_generation %>% slice( 1:5 )
prop_imp_summary_2014_generation <- prop_imp_summary_2014_generation %>% slice( 1:6 )
prop_imp_summary_2021_generation <- prop_imp_summary_2021_generation %>% slice( 1:6 )
# merge datasets
prop_imp_summary_generation <- prop_imp_summary_2010_generation %>%
full_join( prop_imp_summary_2014_generation, by = "subgroup" ) %>%
full_join( prop_imp_summary_2021_generation, by = "subgroup" )
# convert to long format
long_data_generation <- prop_imp_summary_generation %>%
select( subgroup, contains( "point_estimate" ) ) %>%
pivot_longer(
cols = -subgroup,
names_to = "year_identity",
values_to = "proportion"
) %>%
separate( year_identity, into = c( "identity", "year" ), sep = "_point_estimate_" )
long_data_generation_filtered <- long_data_generation %>%
filter( identity %in% c( "Bisexual" ) ) %>%
drop_na()
# plot
ggplot( long_data_generation_filtered,
aes( x = year, y = proportion, color = subgroup, group = subgroup ) ) +
geom_line( linewidth = 0.5 ) +
geom_point( size = 2 ) +
scale_x_discrete( expand = c( 0.05, 0 ) ) +
scale_y_continuous( labels = scales::percent,
breaks = c( 0, 0.02, 0.04, 0.06, 0.08, 0.10 ),
limits = c( 0, 0.10 )
) +
scale_color_manual( values = c( "#C77CFF", "#00BFC4", "#7CAE00", "#F8766D", "orange", "brown" ),
breaks = c( "Generation Z (1997–2012)", "Generation X (1965–1980)", "Silent Generation (1928–1945)", "Millennials (1981–1996)", "Baby Boomers (1946–1964)", "Greatest Generation (1901–1927)" ),
labels = c( "Generation Z (1997–2012)ᵃ ", "Generation X (1965–1980) ", "Silent Generation (1928–1945) ", "Millennials (1981–1996)", "Baby Boomers (1946–1964)", "Greatest Generation (1901–1927)" ),
guide = guide_legend( nrow = 3 ) ) +
labs(
x = "Survey Year",
y = "Proportion of Bisexual Identity" ) +
theme_classic() +
theme( axis.title.x = element_text( family = "Arial", size = 11 ),
axis.text.x = element_text( family = "Arial", size = 11 ),
axis.text.y = element_text( family = "Arial", size = 11 ),
axis.title.y = element_text( family = "Arial", size = 11 ),
legend.text = element_text( family = "Arial", size = 10 ),
legend.title = element_blank(),
legend.position = "bottom" )
```
#### 2.2. By age group
##### 2.2.1. Prepare dataset for plotting
```{r}
prop_imp_age_group_2010 <- read_xlsx("/Users/guoqiang.zhang/Library/CloudStorage/OneDrive-KarolinskaInstitutet/Karolinska Institutet/Research Projects/Population Trends and Individual Fluidity of Sexual Identity in Stockholm County/Research Letter/Population_Trends_and_Individual_Fluidity_of_Sexual_Identity_in_Stockholm_County_2010_to_2021/prop_imp_age_group_2010.xlsx") %>%
select( "subgroup", "Bisexual_point_estimate_2010" )
prop_imp_age_group_2014 <- read_xlsx("/Users/guoqiang.zhang/Library/CloudStorage/OneDrive-KarolinskaInstitutet/Karolinska Institutet/Research Projects/Population Trends and Individual Fluidity of Sexual Identity in Stockholm County/Research Letter/Population_Trends_and_Individual_Fluidity_of_Sexual_Identity_in_Stockholm_County_2010_to_2021/prop_imp_age_group_2014.xlsx") %>%
select( "subgroup", "Bisexual_point_estimate_2014" )
prop_imp_age_group_2021 <- read_xlsx("/Users/guoqiang.zhang/Library/CloudStorage/OneDrive-KarolinskaInstitutet/Karolinska Institutet/Research Projects/Population Trends and Individual Fluidity of Sexual Identity in Stockholm County/Research Letter/Population_Trends_and_Individual_Fluidity_of_Sexual_Identity_in_Stockholm_County_2010_to_2021/prop_imp_age_group_2021.xlsx") %>%
select( "subgroup", "Bisexual_point_estimate_2021" )
prop_imp_age_group_2010$subgroup[ prop_imp_age_group_2010$subgroup == "[17,19]" ] <- "(17,19]"
prop_imp_age_group_2014$subgroup[ prop_imp_age_group_2014$subgroup == "[15,17]" ] <- "[16,17]"
prop_imp_age_group_2021$subgroup[ prop_imp_age_group_2021$subgroup == "[15,17]" ] <- "[16,17]"
prop_imp_age_group_merged <- prop_imp_age_group_2010 %>%
full_join( prop_imp_age_group_2014, by = "subgroup" ) %>%
full_join( prop_imp_age_group_2021, by = "subgroup" )
new_subgroup_label <- function( label ) {
if ( startsWith( label, "(" ) ) {
nums <- as.numeric( unlist( strsplit( gsub( "[^0-9,]", "", label ), "," ) ) )
new_label <- sprintf( "[%d,%d]", nums[1] + 1, nums[2] )
return( new_label )
} else {
return( label )
}
}
prop_imp_age_group_merged$new_subgroup <- sapply( prop_imp_age_group_merged$subgroup, new_subgroup_label )
prop_imp_age_group_merged <- prop_imp_age_group_merged %>%
filter( !( new_subgroup %in% c( "[80,99]", "[80,100]", "[80,104]" ) ) ) %>%
relocate( new_subgroup, .after = subgroup )
age_group_order <- c( "[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]" )
prop_imp_age_group_merged$new_subgroup <- factor( prop_imp_age_group_merged$new_subgroup, levels = age_group_order )
long_data_age_group <- prop_imp_age_group_merged %>%
pivot_longer(
cols = - c( subgroup, new_subgroup ),
names_to = c( "Sexuality", "Estimate_Type", "Year" ),
names_pattern = "([A-Za-z]+)_(\\w+)_(\\d+)",
values_to = "Value"
)
```
##### 2.2.2. Plotting
```{r}
ggplot( long_data_age_group,
aes( x = new_subgroup, y = Value, color = Year, group = Year ) ) +
geom_line( linewidth = 0.5 ) +
scale_color_manual( values = c( "#00BFC4", "#C77CFF", "#F8766D" ),
breaks = c( "2010", "2014", "2021" ),
labels = c( "SPHC-B 2010", "SPHC-B 2014", "SPHC-B 2021" ),
guide = guide_legend( nrow = 1 )
) +
scale_y_continuous( labels = scales::percent,
breaks = c( 0, 0.02, 0.04, 0.06, 0.08, 0.10, 0.12 ),
limits = c( 0, 0.12 )
) +
labs( x = "Age Group (Year)ᵃ",
y = "Proportion of Bisexual Identity"
) +
theme_classic() +
theme( axis.text.y = element_text( family = "Arial", size = 11 ),
axis.title.y = element_text( family = "Arial", size = 11 ),
axis.title.x = element_text( family = "Arial", size = 11 ),
axis.text.x = element_text( family = "Arial", size = 11, angle = 45, vjust = 0.5, hjust = 0.5 ),
legend.title = element_blank(),
legend.text = element_text( family = "Arial", size = 10 ),
legend.position = "bottom"
)
```
```{r}
# check status
renv::status()
# record R package versions
renv::snapshot()
```