PopulationAnalysis 2026

Required Packages

We will use the required packages for data analysis and visualization. Make sure to install them if you haven’t already.

library(tidyverse)
Warning: package 'ggplot2' was built under R version 4.3.3
Warning: package 'tidyr' was built under R version 4.3.1
Warning: package 'readr' was built under R version 4.3.1
Warning: package 'dplyr' was built under R version 4.3.1
Warning: package 'stringr' was built under R version 4.3.1
Warning: package 'lubridate' was built under R version 4.3.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.2     ✔ tibble    3.2.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
library(ggplot2)

Data Preparation

Population These data came from the kaggle dataset

population <- read_csv("https://data-science-with-r.github.io/data/population.csv")
Rows: 217 Columns: 28
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (4): series_name, series_code, country_name, country_code
dbl (24): 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, ...

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
continents <- read_csv("https://data-science-with-r.github.io/data/continents.csv")
Rows: 285 Columns: 4
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (3): entity, code, continent
dbl (1): year

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

View the top values of each dataframe

head(population)
# A tibble: 6 × 28
  series_name  series_code country_name country_code `2000` `2001` `2002` `2003`
  <chr>        <chr>       <chr>        <chr>         <dbl>  <dbl>  <dbl>  <dbl>
1 Population,… SP.POP.TOTL Afghanistan  AFG          1.95e7 1.97e7 2.10e7 2.26e7
2 Population,… SP.POP.TOTL Albania      ALB          3.09e6 3.06e6 3.05e6 3.04e6
3 Population,… SP.POP.TOTL Algeria      DZA          3.08e7 3.12e7 3.16e7 3.21e7
4 Population,… SP.POP.TOTL American Sa… ASM          5.82e4 5.83e4 5.82e4 5.79e4
5 Population,… SP.POP.TOTL Andorra      AND          6.61e4 6.78e4 7.08e4 7.39e4
6 Population,… SP.POP.TOTL Angola       AGO          1.64e7 1.69e7 1.75e7 1.81e7
# ℹ 20 more variables: `2004` <dbl>, `2005` <dbl>, `2006` <dbl>, `2007` <dbl>,
#   `2008` <dbl>, `2009` <dbl>, `2010` <dbl>, `2011` <dbl>, `2012` <dbl>,
#   `2013` <dbl>, `2014` <dbl>, `2015` <dbl>, `2016` <dbl>, `2017` <dbl>,
#   `2018` <dbl>, `2019` <dbl>, `2020` <dbl>, `2021` <dbl>, `2022` <dbl>,
#   `2023` <dbl>
head(continents)
# A tibble: 6 × 4
  entity                code      year continent
  <chr>                 <chr>    <dbl> <chr>    
1 Abkhazia              OWID_ABK  2015 Asia     
2 Afghanistan           AFG       2015 Asia     
3 Akrotiri and Dhekelia OWID_AKD  2015 Asia     
4 Aland Islands         ALA       2015 Europe   
5 Albania               ALB       2015 Europe   
6 Algeria               DZA       2015 Africa   

Filter the populiation by 2023 only

#Label - Population only 2023
population <- population |>
  select(series_name, country_code,'2023') |>
  rename(population = '2023')

Which variable will we use to join the poulation and continents dataframes?

#population country code & conintents code
population_continents <- population |>
  left_join(continents,select(code,continent),
            by = join_by(country_code == code))

###There are some countries in the population dataset that are not in the continents dataset. Let’s find out which ones are missing.

#population country code & conintents code
population_continents |>
  filter(is.na(continent)) 
# A tibble: 2 × 6
  series_name       country_code population entity  year continent
  <chr>             <chr>             <dbl> <chr>  <dbl> <chr>    
1 Population, total CHI              175346 <NA>      NA <NA>     
2 Population, total XKX             1756374 <NA>      NA <NA>     
#population country code & conintents code
population_continents <- population |>
  mutate(
    country_code = case_when(
    country_code == "Kosovo"~ "OWID_KOS",
    country_code == "Channel Islands" ~ "OWID_CIS",
    .default = country_code)
    ) |>
  left_join(continents |> select(code,continent),
            by = join_by(country_code == code)) 
#population country code & conintents code
population_continents |>
  filter(is.na(continent))
# A tibble: 2 × 4
  series_name       country_code population continent
  <chr>             <chr>             <dbl> <chr>    
1 Population, total CHI              175346 <NA>     
2 Population, total XKX             1756374 <NA>     

Which continent has the highest population in 2023? Which do you think has the lowest? Let’s find out.

To find out, let us create new dataframe population_summary

#population country code & conintents code
population_summary <- population_continents |>
  group_by(continent) |>
  summarise(total_population = sum(population, na.rm = TRUE)) |>
  arrange(desc(total_population))

Visulization of barplot

#population country code & conintents code
ggplot(population_summary, aes(x = continent, y = total_population)) +
  geom_bar(stat = "identity", fill = "steelblue") +
  labs(title = "Total Population by Continent in 2023",
       x = "Continent",
       y = "Total Population") +
  theme_minimal()

#population country code & conintents code
ggplot(population_summary, aes(y = continent, x = total_population)) +
  geom_point() +
  geom_segment(aes(x = 0, xend = total_population, y = continent, yend = continent), color = "gray") +
  labs(title = "Total Population by Continent in 2023")