I’m using this data set from World Bank’s Gender Data Portal.
library(readr)
library(tidyverse)
library(dplyr)
library(ggplot2)
mean_age_at_first_marriage <- read_csv("mean age at first marriage.csv")
#take a look at variables
colnames(mean_age_at_first_marriage)
## [1] "Indicator Name" "Indicator Code" "Country Name" "Country Code"
## [5] "Year" "Value" "Disaggregation"
#check for NAs
sum(is.na(mean_age_at_first_marriage))
## [1] 0
# use group_by and summarize
gender_summary <- mean_age_at_first_marriage %>%
group_by(Disaggregation) %>%
summarize(avg_age = round(mean(Value), 0), median_age = round(median(Value), 0), min_age = min(Value), max_age = max(Value))
gender_summary
## # A tibble: 2 × 5
## Disaggregation avg_age median_age min_age max_age
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 female 24 24 14.4 34.1
## 2 male 28 28 20.8 36.9
# use count and order by highest reporter at top
country_counts <- mean_age_at_first_marriage %>%
count(`Country Name`, name = "total_records") %>%
arrange(desc(total_records))
# taking a look at the difference from top and lowest reporting countries
range(country_counts$total_records)
## [1] 1 96
# taking a look at the distribution of reponse counts
table(country_counts$total_records)
##
## 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
## 1 3 2 8 4 9 7 19 8 14 9 13 10 8 13 5 6 9 6 5 3 3 5 3 3 1
## 27 28 29 30 31 32 34 37 38 41 44 46 56 60 61 63 68 78 84 86 88 96
## 1 3 1 3 1 3 3 1 2 1 1 2 1 1 1 2 1 1 2 2 1 1
# using head here as it's a big data set
head(country_counts, 10)
## # A tibble: 10 × 2
## `Country Name` total_records
## <chr> <int>
## 1 Hungary 96
## 2 Denmark 88
## 3 Norway 86
## 4 Sweden 86
## 5 Finland 84
## 6 Netherlands 84
## 7 Iceland 78
## 8 Canada 68
## 9 Greenland 63
## 10 San Marino 63
These insights indicate that across countries, women tend to be younger at their first marriage, but also that there’s inconsistency in reporting across countries over the years, with more reporting from countries in Europe than anywhere else (this last point deduced just from looking at the country counts data frame, not from performing analysis on it; with more time I’d have added a column to assign each observation a continent so I could do that kind of analysis, and which also may have provided more plot opportunities for that data).
I decided to try a density plot, comparing the density of the mean age responses for each gender. I don’t think it necessarily makes the most sense as a way to visualize this information (maybe actual number of reports on the y would be better) but I wanted to try it out.
ggplot(mean_age_at_first_marriage, aes(x = Value, fill = Disaggregation)) +
geom_density(alpha = 0.5) +
labs(
title = "Marriage Age by Gender Across Countries",
x = "Mean Age at First Marriage (Years)",
y = "Density",
fill = "Gender"
) +
theme_minimal()
I also wanted to visualize the country count, for which a histogram seemed to make the most sense, giving us a sense of how many data points countries typically contribute.
ggplot(country_counts, aes(x = total_records)) +
geom_histogram(binwidth = 2, fill = "darkgreen", color = "white") +
labs(
title = "Distribution of Responses Across All Countries",
x = "Number of Survey Responses per Country",
y = "Number of Countries"
) +
theme_minimal()