-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_plot_annotated.R
191 lines (155 loc) · 7.61 KB
/
03_plot_annotated.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
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Load and download (if necessary) required packages ----
# use (and install if necessary) pacman package
if (!require("pacman")) install.packages("pacman")
library(pacman)
# load and install (if necessary) required packages for this course
pacman::p_load(
colorspace, # for defining the fill colors
ggh4x, # for nested facet plot
ggtext, # formatting the axis text
grid, # to add lines to the patchwork plot
here, # enables easy file referencing in project-oriented workflows
patchwork, # for plot composition
tidyverse, # universal toolkit for data wrangling and plotting
scales, # also used for transforming numeric values into percentages
showtext # allows to use of google fonts
)
# clear the environment
rm(list = ls())
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Load data ----
load(here("data", "posted",
"01_cplcash.RData"))
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# add font for plot ----
font_add_google("Roboto Condensed", "Roboto Condensed")
showtext_auto()
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Facet plot for couples' money arrangements ----
# ...for different cohorts and age groups and by partnership status
cash.plot <- facetplotdata |>
ggplot(aes(x, y)) +
geom_tile(aes(fill=cplcash), color="white", linewidth=0.5) +
geom_text(data = facetplot.txt.data,
aes(x = x , y = y, label = label),
fontface ="bold",
family = "Roboto Condensed",
size = 5,
hjust = 0) +
facet_nested(rows = vars(cohort, fct_rev(age)),
cols = vars(pstatus),
switch = "y") +
labs(y = NULL, x = NULL, fill = NULL) +
scale_fill_manual(values = sequential_hcl(3, palette = "OrYel")) +
theme_void(base_family = "Roboto Condensed") +
theme(strip.text.x = element_text(size = 22, face = "bold",
family = "Roboto Condensed",
margin = margin(2,0,2,0)),
strip.text.y = element_blank(),
legend.position = "bottom",
legend.text = element_text(size = 18),
legend.spacing.x = unit(.3, 'in'),
legend.key.size = unit(.3, "in"),
plot.margin = margin(0))
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Waffle plot showing distribution of partnership states ----
# ... for different cohorts and age groups
partner.plot <- pstatus.to.plot |>
ggplot(aes(x, y)) +
geom_tile(aes(fill=pstatus), color="white", linewidth=0.5) +
geom_text(data = pstatus.txt.data,
aes(x = x , y = y, label = label),
fontface ="bold",
family = "Roboto Condensed",
size = 5,
hjust = 0) +
facet_nested(rows = vars(cohort, fct_rev(age)),
cols = vars(colvar),
switch = "y") +
labs(y = NULL, x = NULL, fill = NULL) +
scale_fill_manual(values = sequential_hcl(11,
palette = "Sunset",
rev = F)[c(2,5,8)] |> lighten(.3),
guide = guide_legend(reverse = TRUE)) +
theme_void(base_family = "Roboto Condensed") +
theme(
strip.text.x = element_text(size = 22, face = "bold",
family = "Roboto Condensed",
margin = margin(2,0,2,0)),
strip.text.y.left = element_markdown(size = 22, face = "bold",
family = "Roboto Condensed", angle = 0,
lineheight = 1.2,
margin = margin(2,0,2,0),
hjust = 0),
legend.position = "bottom",
legend.location = "plot",
legend.margin = margin(l=0),
legend.text = element_text(size = 18),
legend.spacing.x = unit(.3, 'in'),
legend.key.size = unit(.3, "in"),
plot.background = element_part_rect(side = "r",
colour = "black",
linewidth = 1,
linetype = "solid"),
plot.margin = margin(0))
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Composing the final plot ----
# Step 1: Put the patches together
ptitle <- paste("Cohort and Age Group Differences in Partnership",
"and Financial Arrangements Among Couples in Germany")
psubtitle <- paste("The plot shows percentages using calibrated design",
"weights adjusting the samples to central characteristics",
"of the German population for the three cohorts 1971/73,",
"1981/83, and 1991/1993. Design-adjusted Rao‐Scott",
"chi-square tests shown in the supplementary material",
"illustrate that the cohort differences in",
"couples' money arrangements of similar-aged respondents",
"are mainly driven by married couples. Similarly, the age",
"group differences within the cohort 1981/83 are only",
"statistically significant (p < .001) among married respondents.")
pcaption <- ("**Source:** German Family Panel (pairfam), release 13.0, waves 1 and 9")
the.plot <- partner.plot + cash.plot + plot_layout(widths = c(1, 4)) +
plot_annotation(
title = ptitle,
subtitle = psubtitle,
caption = pcaption,
theme = theme(
plot.title = element_text(size = 38,
margin = margin(5,0,5,0),
family = "Roboto Condensed",
face = "bold"),
plot.subtitle = element_textbox_simple(size = 18,
margin = margin(10,0,15,0),
family = "Roboto Condensed"),
plot.caption = element_markdown(size = 14,
margin = margin(20,0,5,0),
family = "Roboto Condensed")
)
)
# Step 2: Convert the ggplot object to a grob
p_grob <- patchworkGrob(the.plot)
# Step 3: Create additional elements using grid functions
hline <- linesGrob(x = unit(c(0.01, .99), "npc"),
y = unit(c(0.66, 0.66), "npc"))
hline2 <- linesGrob(x = unit(c(0.01, .99), "npc"),
y = unit(c(0.26, 0.26), "npc"))
# Step 4: Combine the ggplot grob and the additional elements into a single grob
the.plot <- grobTree(p_grob, hline, hline2)
# Step 5: Save the combined grob using ggsave
showtext_opts(dpi = 300)
ggsave(here("plots", "the_plot_annotated.png"),
the.plot, width = 23, height = 16, dpi = 300,
bg = "white")
ggsave(here("plots", "the_plot_annotated.svg"),
the.plot, width = 23, height = 16, dpi = 300,
bg = "white")
showtext_opts(dpi = 96)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~