```{r}
#| label: plot
### |- first plot ----
# Pyramid style chart
p1 <- ggplot(gender_by_country, aes(x = reorder(country, total_contestants), y = count, fill = gender)) +
geom_bar(stat = "identity", width = 0.75, alpha = 0.85) +
# Geoms
# Adding labels outside the bars
geom_text(aes(label = comma(abs(count))),
position = position_nudge(y = ifelse(gender_by_country$gender == "female", -50, 50)),
size = 3.6, hjust = ifelse(gender_by_country$gender == "female", 1, 0), color = text_col
) +
# Adding a single country label next to the bars
geom_text(aes(y = -900, label = country), # Position countries next to the bars
size = 3.6, hjust = 0.5, vjust = 0, color = text_col
) +
# Scales
scale_y_continuous(
breaks = seq(-1000, 1000, by = 500),
labels = scales::comma_format(),
limits = c(-1200, 1600)
) +
scale_fill_manual(values = col_palette) +
coord_flip(clip = "off") +
# labs
labs(
x = NULL,
y = "Number of Contestants",
fill = "Gender"
) +
# Theme
theme(
axis.text.y = element_blank(),
axis.title.y = element_blank(),
axis.ticks.y = element_blank(),
panel.grid.major.y = element_blank(),
)
### |- second plot ----
# Create the plot, including the ribbon and the textlines
p2 <- ggplot() +
# Geoms
# Add ribbon to fill the area between male and female percentages
geom_ribbon(aes(x = male_data$year, ymin = female_data$percentage, ymax = male_data$percentage),
fill = "lightblue", alpha = 0.5
) +
# Add the geom_textline for male and female percentages
geom_textline(aes(x = year, y = percentage, color = gender, label = gender),
data = gender_representation_normalized_long,
linewidth = 1,
family = "text",
size = 5,
fontface = "bold",
hjust = 0.5, # move labels to the right
offset = unit(0.3, "cm"), # move labels up
text_smoothing = 30 # smooth text (more legible)
) +
# Adding geom_point and geom_text for the start and end percentages for male and female
geom_point(
data = filter(gender_representation_normalized_long, year == min(year) | year == max(year)),
aes(x = year, y = percentage, color = gender), size = 4
) +
# Female
geom_text(
data = filter(
gender_representation_normalized_long,
(year == min(year) | year == max(year)) & gender == "Female"
),
aes(
x = year, y = percentage, label = scales::percent(percentage / 100, accuracy = 1),
color = gender
), size = 5, nudge_x = -0.005, vjust = -1.3, fontface = "bold", family = "text"
) +
# Male
geom_text(
data = filter(
gender_representation_normalized_long,
(year == min(year) | year == max(year)) & gender == "Male"
),
aes(
x = year, y = percentage, label = scales::percent(percentage / 100, accuracy = 1),
color = gender
), size = 5, nudge_x = 0.005, vjust = 1.9, fontface = "bold", family = "text"
) +
# Labs
labs(
x = "Year",
y = "Percentage of Contestants",
color = "Gender"
) +
# Scales
scale_x_continuous() +
scale_y_continuous(labels = scales::label_percent(scale = 1)) +
scale_color_manual(values = col_palette) +
coord_cartesian(clip = "off")
#### |- combined plot ----
# Annotation and aspect ratio of p2
p2 <- p2 +
annotate(
"text",
x = 1962,
y = 50,
label = "Even though the overall contestant count has increased,\n the gender gap has remained the same.",
size = 4,
fontface = "italic",
family = "text",
color = 'gray40',
hjust = 0
) +
theme(aspect.ratio = 0.85)
# Combine plots
combined_plot <- (p1 | p2) +
patchwork::plot_layout(
ncol = 2,
widths = c(1, 1.25), # Adjusting relative widths
guides = 'collect' # Collect legends
) +
# Labs
plot_annotation(
title = title_text,
subtitle = subtitle_text,
caption = caption_text
) &
# Theme
theme(
plot.margin = margin(10, 20, 10, 20),
plot.title = element_markdown(
size = rel(1.7),
family = "title",
face = "bold",
color = title_col,
lineheight = 1.1,
margin = margin(t = 5, b = 5)
),
plot.subtitle = element_markdown(
size = rel(0.88),
family = 'subtitle',
color = subtitle_col,
lineheight = 1.1,
margin = margin(t = 5, b = 5)
),
plot.caption = element_markdown(
size = rel(0.65),
family = "caption",
color = caption_col,
lineheight = 1.1,
hjust = 0.5,
halign = 1,
margin = margin(t = 5, b = 5)
)
)
# Show the combined plot
combined_plot
```