Required items to include in your work for grade “B”:
For grade “A”:
The Refugees package includes 8 datasets, but today I will use only
population and demographics.
library(refugees) # dataset
library(dplyr)
library(tidyr)
library(data.table)
library(countrycode) # map countries to continent
library(sf) # shapefiles
library(ggplot2)
library(gganimate)
library(maps)
library(rnaturalearth) # map
library(scales) # scale axis-es
library(ggiraph) # interactive charts
custom_theme <- function() {
theme_minimal() +
theme(
axis.text = element_text(
family = 'Arial',
color = "black",
size = 11),
axis.title = element_text(
family = 'Arial',
color = "black",
size = 13),
panel.grid.major = element_line(color = "gray", size = 0.2),
panel.grid.minor = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(color = "gray", size = 0.2),
panel.background = element_rect(
color = NA,
fill = "white",
linewidth = 0,
linetype = "solid"),
plot.title = element_text(
family = 'Arial',
color = "black",
size = 16,
face = "bold",
hjust = 0.5),
plot.subtitle = element_text(
family = 'Arial',
color = "black",
size = 12,
hjust = 0.5),
legend.position = "right",
legend.title = element_text(
family = 'Arial',
color = "black",
size = 12),
legend.text = element_text(
family = 'Arial',
color = "black",
size = 10))}
First, let’s look at how the number of refugees changed since the 1950s grouped by continent of origin. Note that the country of origin and country of asylum does not have to differ. For many of the refugees these two are the same.
# plot refugees by year and continent
ggplot(grouped_population, aes(year, refugees)) +
geom_col(aes(fill = reorder(continent, -refugees)), position = "stack", width = 1) +
labs(title = "Refugees by Year and Continent",
x = "",
y = "Refugees",
fill = "Continent") +
scale_fill_manual(values = continent_colors) +
custom_theme() +
scale_y_continuous(labels = scales::label_number(scale = 1e-6, suffix = "M")) +
scale_x_continuous(breaks = seq(1960, max(grouped_population$year), by = 10))
As seen from the graph, there was a drastic increase in the number of refugees after 1980 up until the 1990s when a more peaceful time came and the number of refugees started to decrease. However, after around 2005 a never before seen wave of refugees hit Asia and Africa, who has been the clear victims of migration. Another spike is clearly the start of Russia’s invasion of Ukraine in 2022, when Europe experienced a sudden wave of refugees looking for asylums.
Now, let’s look at the same graph using animation.
## ANIMATED BARCHART
ggplot(agg_population, aes(continent, refugees)) +
geom_col(aes(fill = continent)) +
labs(title = "Refugees in {closest_state} \n by Asylum Continent",
x = "",
y = "Refugees",
fill = "Continent") +
scale_fill_manual(values = continent_colors, limits = names(continent_colors))+
custom_theme() +
transition_states(year, transition_length = 0.5, state_length = 1) +
enter_fade() +
exit_fade() +
scale_y_continuous(labels = scales::label_number(scale = 1e-6, suffix = "M"))
For the next graph, let’s dive into the age groups of refugees who were seeking asylum in the following continents. The graph is interactive, so hovering over the bars will display the exact number of refugees for the corresponding age and gender group.
Oceania is left out of the data as there are little to no observations from that continent.
p <- ggplot(demo_2022[demo_2022$to_continent != 'Oceania',])+
geom_col_interactive(aes(-male_p,ages,fill = 'Male',tooltip = paste("Refugees: ", comma(male))),width = 0.8)+
geom_col_interactive(aes(female_p,ages,fill = 'Female',tooltip = paste("Refugees: ", comma(female))),width = 0.8)+
geom_text(aes(-male_p, ages, label = percent(round(abs(male_p),3))),hjust = 1.1,size = 9/ .pt)+
geom_text(aes(female_p, ages, label = percent(round(abs(female_p),3))),hjust = -0.15,size = 9/ .pt)+
facet_wrap(~to_continent,ncol = 1)+
scale_fill_manual(values = c('#762a83', '#5aae61'))+
labs(title = 'Age Group Distribution of Refugees',
subtitle = 'Grouped by Asylum Continent',
y = '',
x = '',
fill = 'Gender')+
custom_theme()+
theme(panel.grid.major.y = element_blank(),
legend.position = 'right',
strip.text = element_text(size = 11),
axis.text.x = element_blank(),
#axis.text.y = element_text(face = "bold")
)+
xlim(min(demo_2022$male_p)-0.55, max(demo_2022$male_p)-0.13)
girafe(ggobj = p)
There is not much difference between genders for Africa, the Americas, and Asia. Maybe the first noticable difference is in Europe. In the above 60 age group, 60% is female which is the biggest difference in our dataset.
On the next graph, I visualize Asylum seekers in Europe, and compare how different decades looked like.
Note: if there is either no data, or the number of refugees is 0 then the country is not shown on the map.
# map with geom_sf
ggplot() +
geom_sf(data = merged_europe, aes(geometry = geometry, fill = V1), color = "black") +
scale_fill_gradient(
low = "lightblue",
high = "darkblue",
na.value = "white",
labels = custom_labels,
breaks = custom_breaks,
limits = c(0, 1.2e6)
) +
theme_void() +
theme(
legend.position = "right",
legend.title = element_text(size = 8), # Adjust legend title size
legend.text = element_text(size = 6),
plot.title = element_text(size = 12, hjust = 0.5),
plot.subtitle = element_text(size = 8,hjust = 0.5)# Centered and increased plot title size
) +
labs(
title = "Refugees In Europe 1950s-2020s",
fill = "Refugees \n (in thousands)",
x = "",
y = "",
subtitle = "Values are yearly averages in the decade" # Add a subtitle if needed
) +
coord_sf(xlim = c(-20, 40), ylim = c(35, 65)) +
facet_wrap(~decade, nrow = 4)
It is interesting to see how Eastern Europe is not shown until the 1990s, which is probably because there are no observations from the time and place of the Soviet Union. Up until then, only the UK, France, and Germany had darker colors. The darkest blue color is Germany in the 1990s. The reason might be because of the collapse of East-Germany, when almost a million people moved to the West side of the country.
On the next graph, we can see which countries refugees fleed from in 2023. The graph is also interactive, so hovering over the bars will display the full name of the country as opposed to the ISO code on the X axis, and the corresponding number of Refugees.
# Create the plot
p <- ggplot(from_to, aes(x = reorder(country, refugees), y = refugees/1e6, fill = factor(sign(refugees)))) +
geom_col_interactive(aes(tooltip = paste("Country: ", coo_name, "<br>Refugees: ", comma(refugees))),
fill_opacity = 0.7) +
scale_fill_manual(values = c("#d53e4f", "#5e4fa2", "grey"), name = "") +
labs(title = 'Refugee Situation in Europe as of 2023',
subtitle = 'Which Countries do Refugees come from \n which countries offer Asylum',
x = '',
y = 'Refugees (in Millions)') +
guides(fill = guide_legend(title = "Refugees Sign")) +
custom_theme() +
theme(axis.text.x = element_text(angle = 90, hjust = 1),
legend.position = "None")
girafe(ggobj = p)
By far the most people come from Ukraine, followed by countries in the Middle-East, Syria, Afghanistan, and Iraq.
On the other side, Germany, Russia, and Poland offers the most asylums.
On the last graph, I am comparing the refugee politics of Germany and Poland.
Germany has been known for its relatively open stance, especially during the European migration crisis in 2015 when it accepted a significant number of refugees.
Poland, on the other, has been more cautious in accepting refugees and has advocated for stricter immigration policies within the EU. However, Poland played and plays a crucial role for helping Ukrainian Asylum seekers and offer perhaps the most shelter for Ukrainians.
ggplot(europe_aggregated, aes(x = year, y = total_refugees, color = highlight, size = highlight, group = coa_name)) +
geom_line(alpha = 0.7) +
scale_color_manual(values = highlight_colors) +
labs(
title = "Comparing Refugee Politics",
subtitle = "Germany vs Poland",
y = 'Refugees (in thousands)',
x = '') +
scale_size_manual(values = c("Germany" = 1.5, "Poland" = 1.5, "Other" = 0.2)) +
custom_theme() +
theme(legend.position = "none") +
geom_text(data = europe_aggregated %>% filter(year == 2023 & (highlight == "Germany" | highlight == "Poland")),
aes(label = highlight),
vjust = 0.4,
hjust = -0.1,
size = 6) +
xlim(min(europe_aggregated$year), max(europe_aggregated$year) + 5.6) +
scale_y_continuous(labels = scales::label_number(scale = 1e-3))+
geom_label(x=2022, y=1150000, label="Russia's invasion of Ukraine in 2022", size=4, color="#5e4fa2")
As anticipated, while Germany was always very open towards refugees, Poland only opened its borders to Ukrainians after the Russian invasion of their country.