Choose a dataset and Load additional library packages to explore it
I have chosen the gapminder dataset. This dataset includes health and income outcomes for 184 countries from 1960 to 2016. It also includes two character vectors, OECD and OPEC, with the names of OECD and OPEC countries from 2016.
OPEC, the Organization of the Petroleum Exporting Countries, was formed in 1960 and consists of 13 member states, including Saudi Arabia, Iraq, and Venezuela. These countries collectively hold over 80% of the world’s proven oil reserves and produce about 40% of the world’s crude oil, with their exports accounting for around 60% of global petroleum trade. OPEC aims to coordinate petroleum policies among its members to stabilize oil prices, ensure supply, and provide a fair return on investment.
The OECD (Organization for Economic Co-operation and Development) is made up of industrialized countries, such as the United States and much of Europe. While these economies consume less oil than non-OECD countries, they still have a significant impact on global oil demand.
data("gapminder")library(tidyverse)
── 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.1 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ 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(leaflet)
Warning: package 'leaflet' was built under R version 4.4.1
library(leaflet.extras)
Warning: package 'leaflet.extras' was built under R version 4.4.1
Upload a dataset that has countries longitude and latitude
setwd("C:/Users/User/Downloads/Data 110 Projects and Assignments")countries_coordinates <-read_csv("world_country_and_usa_states_latitude_and_longitude_values.csv")
Rows: 245 Columns: 8
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (4): country_code, country, usa_state_code, usa_state
dbl (4): latitude, longitude, usa_state_latitude, usa_state_longitude
ℹ 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.
head(countries_coordinates)
# A tibble: 6 × 8
country_code latitude longitude country usa_state_code usa_state_latitude
<chr> <dbl> <dbl> <chr> <chr> <dbl>
1 AD 42.5 1.60 Andorra AK 63.6
2 AE 23.4 53.8 United Arab… AL 32.3
3 AF 33.9 67.7 Afghanistan AR 35.2
4 AG 17.1 -61.8 Antigua and… AZ 34.0
5 AI 18.2 -63.1 Anguilla CA 36.8
6 AL 41.2 20.2 Albania CO 39.6
# ℹ 2 more variables: usa_state_longitude <dbl>, usa_state <chr>
Merge the two datasets
I will join this gapminder and some variables of countries_coordinates based on the “country” column
world <-left_join(gapminder,# Select relevant columns select(countries_coordinates,"latitude","longitude","country"),by ="country") head(world)
country year infant_mortality life_expectancy fertility
1 Albania 1960 115.40 62.87 6.19
2 Algeria 1960 148.20 47.50 7.65
3 Angola 1960 208.00 35.98 7.32
4 Antigua and Barbuda 1960 NA 62.97 4.43
5 Argentina 1960 59.87 65.39 3.11
6 Armenia 1960 NA 66.86 4.55
population gdp continent region latitude longitude
1 1636054 NA Europe Southern Europe 41.15333 20.168331
2 11124892 13828152297 Africa Northern Africa 28.03389 1.659626
3 5270844 NA Africa Middle Africa -11.20269 17.873887
4 54681 NA Americas Caribbean 17.06082 -61.796428
5 20619075 108322326649 Americas South America -38.41610 -63.616672
6 1867396 NA Asia Western Asia 40.06910 45.038189
Create a map with leaflet
I will create an interactive map with Leaflet for one year (2010), and add circle markers representing various metrics for each country. The popup will only show up when we click the marker on the map.
# Filter for the year 2010world_filtered <- world %>%filter(year ==2010) # Create the leaflet mapp1 <-leaflet()|>addTiles() |># Center the map at specified coordinates and zoom levelsetView(lng =12.43, lat =42.98, zoom =5) %>%# Add circle markers to the map, with popups displaying various metricsaddCircleMarkers(data = world_filtered, color ="darkblue",lat =~latitude , lng =~longitude ,popup =~paste("gdp:",gdp, "<br>Life Expectancy:",life_expectancy,"<br>Year:", year, "<br>Fertility:",fertility,"<br>Population:", population,"<br>Infant Mortality:", infant_mortality))|># Add a toolbar to the map for drawing shapesaddDrawToolbar()
Warning in validateCoords(lng, lat, funcName): Data contains 13 rows with
either missing or invalid lat/lon values and will be ignored
p1
When mousing over this map, we can view various observations for each country. The goal was to display the geographical distribution of GDP, life expectancy, fertility rate, and infant mortality.
However, this method isn’t ideal for visualizing relationships between variables; scatterplots or line charts would be more effective for that.