Transform the wide-format GDP dataset into a tidy (long) format using the tidyr and dplyr packages in R. Once the dataset has been cleaned up, examine GDP patterns across nations and historical periods to see how national economies have evolved.
Approach
Dataset description
I’ll be using a dataset of countries’ GDP figures from 1960 to 2022 for this project. The dataset is available here:
GDP figures for numerous nations across several decades are included in the dataset. The dataset is currently stored in a wide format where a country is represented by each row and a year by each column.
For instance, GDP values for those particular years are shown in columns like 1960, 1961, 1962, and so on.
Because the year variable is contained within column names rather than being expressed as its own column, this structure does not adhere to tidy data principles. Every variable should have its own column and every observation should have its own row in a neat dataset.
The following important variables are present in the dataset:
Country Name: Name of the country
Country Code: Country identifier
1960–2022 columns: GDP values for each year
The year columns must be transformed into a single column that represents the year and stores GDP figures in a separate column in order for the dataset to be analyzed.
Anticipated Challenges
Managing the numerous year columns that span more than 60 years is one expected problem. During the cleaning process, these columns must be transformed into a single variable that represents the year.
Dealing with missing values for specific nations and years presents another possible difficulty. Calculations and visualizations must be done carefully because certain countries might not have GDP statistics available for the full time period.
Furthermore, GDP figures can be extremely high, therefore in order to make trend visualizations easier to read, the scale may need to be changed.
Implementation of Data Import
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.2.0
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.1 ✔ tibble 3.3.1
✔ lubridate 1.9.4 ✔ tidyr 1.3.2
✔ purrr 1.2.1
── 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(tidyverse)library(gt)# URL to GDP CSVurl <-"https://raw.githubusercontent.com/NafeesKhandker/Project-2-Tidy-Format-Transformation/refs/heads/main/GDP_by_Country.csv"# Read CSV into Rdf <-read_csv(file = url, show_col_types =FALSE)# Preview first 5 rowsdf |>head(5) |>gt() |>tab_header(title ="Preview of Raw GDP Data",subtitle ="Wide-format dataset with countries and years" )
Preview of Raw GDP Data
Wide-format dataset with countries and years
Country
Country Code
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
Aruba
ABW
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
405586592
487709497
596648045
695530726
764804469
872067039
958659218
1083240223
1245810056
1320670391
1379888268
1531843575
1665363128
1722905028
1873184358
1896648045
1962011173
2044134078
2254748603
2359776536
2469832402
2677653631
2843016760
2553631285
2453631285
2637988827
2615083799
2727932961
2.791061e+09
2963128492
2983798883
3.092179e+09
3.276188e+09
3.395794e+09
2610038939
3.126019e+09
NA
Africa Eastern and Southern
AFE
21125015452
21616228139
23506279900
28048360188
25920665260
29472103270
32014368121
33269509510
36327785495
41638967621
44629891649
49173371529
53123459912
69482723444
85380645042
90835426418
90212747243
102240575583
116084638702
134256827127
171217790781
175859256874
168095657215
175564912386
160646748724
136759437910
153050335916
186658478814
204765985926
218241607366
254062093242
276856728336
246088124936
242926405780
239610677917
270327154575
269490833465
283446224788
266652333831
263024788890
284759318603
259643121973
266529432166
354176768091
440481795991
513941625354
577586931667
662868036074
710536227817
721901168421
863519541418
967824566959
975354777362
985987130727
1.006526e+12
927348469903
885176429224
1.021043e+12
1.007196e+12
1.000834e+12
927593321648
1.081998e+12
1.169484e+12
Afghanistan
AFG
537777811
548888896
546666678
751111191
800000044
1006666638
1399999967
1673333418
1373333367
1408888922
1748886596
1831108971
1595555476
1733333264
2155555498
2366666616
2555555567
2953333418
3300000109
3697940410
3641723322
3478787909
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
3854235264
4539496563
5220825049
6226198935
6971383339
9715765105
10249770319
12154835708
15633843662
18190413832
20203572960
20564485419
2.055058e+10
19998143636
18019554403
1.889635e+10
1.841886e+10
1.890450e+10
20143451706
1.458314e+10
NA
Africa Western and Central
AFW
10447637853
11173212080
11990534018
12727688165
13898109284
14929792388
15910837742
14510579889
14968235782
16979315745
23596163865
20936358634
25386169423
31975594565
44416677335
51667190242
62351622300
65595122956
71496496574
88948338390
112439126385
211338060015
187448724920
138384182007
114516348921
116776995133
107886511309
110728825942
109438851254
102254998563
122387353859
118039698016
118893094122
99272180411
86636400266
108690885030
126287285163
127602388366
130678128885
138085971820
140945759314
148529518712
177201164643
205214466071
254264799899
310889578636
396921040009
465485534969
567791156729
508362731367
598521555566
682015858792
737589473577
833948054767
8.943225e+11
768644740597
691363412188
6.848988e+11
7.670257e+11
8.225384e+11
786460035395
8.444597e+11
8.778633e+11
Angola
AGO
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
5930503401
5550483036
5550483036
5784341596
6131475065
7554065410
7072536109
8084412414
8769836769
10201780977
11229515599
12704558517
15114352005
11051939102
3390500000
5561222222
7526963964
7648377413
6506229607
6152922943
9129594819
8936079253
15285592487
17812704626
23552057820
36970901025
52381025105
65266415707
88538664888
70307196477
81699526881
109436566876
124998210652
133401582620
1.372444e+11
87219300385
49840491178
6.897277e+10
7.779294e+10
6.930911e+10
50241368244
6.568544e+10
1.067136e+11
Reshape from Wide to Long (Tidy) Format
Convert columns 1960–2022 into a single Year column and a GDP value column. This makes the dataset tidy for analysis.
# Reshape from wide to long (tidy) formatgdp_long <- df |>pivot_longer(cols =3:last_col(), # all year columnsnames_to ="Year",values_to ="GDP" )# Convert Year to integer and GDP to numericgdp_long <- gdp_long |>mutate(Year =as.integer(Year),GDP =as.numeric(GDP) )# Preview first 5 rowsgdp_long |>head(5) |>gt() |>tab_header(title ="Tidy GDP Data (Country-Year Format)",subtitle ="Each row is one country in one year" )
Tidy GDP Data (Country-Year Format)
Each row is one country in one year
Country
Country Code
Year
GDP
Aruba
ABW
1960
NA
Aruba
ABW
1961
NA
Aruba
ABW
1962
NA
Aruba
ABW
1963
NA
Aruba
ABW
1964
NA
Normalize Variable Structure
Ensure the data types are consistent. Convert Year to integer and GDP to numeric.
# FILTER ONLY REAL COUNTRY ECONOMIES — remove groupings/regionsgdp_long <- gdp_long |>filter(!grepl(pattern =paste(# common grouping keywords to exclude"World|income|Africa|Asia|Europe|Latin America|North America|Middle East|Pacific|Caribbean|Sub-Saharan|regional|area|aggregate|total|blend|only|Not classified|OECD|Post|Late|Early|Fragile|Least|Pre|Heavily|Small",sep ="|" ),x = Country,ignore.case =TRUE ) )# Preview first 10 rowsgdp_long |>filter(!is.na(GDP)) |># show rows with actual GDPhead(5) |>gt() |>tab_header(title ="Normalized GDP Data for Actual Countries",subtitle ="Excludes regions/aggregates; non-missing GDP only" )
Normalized GDP Data for Actual Countries
Excludes regions/aggregates; non-missing GDP only
Country
Country Code
Year
GDP
Aruba
ABW
1986
405586592
Aruba
ABW
1987
487709497
Aruba
ABW
1988
596648045
Aruba
ABW
1989
695530726
Aruba
ABW
1990
764804469
Handle Missing or Inconsistent Values
Address NA values. For this dataset, we will keep NA as-is but note that these indicate missing GDP data for that year/country.
# Count missing GDP valuesgdp_long |>summarise(Missing_GDP =sum(is.na(GDP)))
# A tibble: 1 × 1
Missing_GDP
<int>
1 3123
# Remove rows with missing GDP for analysisgdp_long <- gdp_long |>drop_na(GDP)# Confirm no missing values remaingdp_long |>summarise(Missing_GDP =sum(is.na(GDP)))
# A tibble: 1 × 1
Missing_GDP
<int>
1 0
Summary Statistics by Country
The table shows the range and average GDP for each country. Larger economies like the US or China have very high mean GDP, while smaller countries or regions have lower GDP. This helps identify countries with consistent growth versus volatile economies.
library(scales) # for number formatting
Attaching package: 'scales'
The following object is masked from 'package:purrr':
discard
The following object is masked from 'package:readr':
col_factor
# Helper function to format numbersformat_gdp <-function(x) {ifelse(x >=1e12, paste0(round(x/1e12, 2), " T"),ifelse(x >=1e9, paste0(round(x/1e9, 2), " B"),ifelse(x >=1e6, paste0(round(x/1e6, 2), " M"),paste0(round(x/1e3, 2), " K"))))}# Summary statistics by countrygdp_summary <- gdp_long |>group_by(Country) |>summarise(Min_GDP =min(GDP),Max_GDP =max(GDP),Mean_GDP =mean(GDP),Median_GDP =median(GDP),.groups ="drop" ) |>arrange(desc(Mean_GDP))# Display top 10 countriesgdp_summary |>gt() |>fmt(columns =vars(Min_GDP, Max_GDP, Mean_GDP, Median_GDP),fns = format_gdp ) |>tab_header(title ="Summary Statistics of GDP by Country",subtitle ="Values in USD (formatted in K, M, B)" ) |>cols_label(Country ="Country",Min_GDP ="Min GDP",Max_GDP ="Max GDP",Mean_GDP ="Mean GDP",Median_GDP ="Median GDP" )
Warning: Since gt v0.3.0, `columns = vars(...)` has been deprecated.
• Please use `columns = c(...)` instead.
Summary Statistics of GDP by Country
Values in USD (formatted in K, M, B)
Country
Min GDP
Max GDP
Mean GDP
Median GDP
United States
543.3 B
25.46 T
8.21 T
6.16 T
China
47.21 B
17.96 T
3.06 T
383.37 B
Japan
44.31 B
6.27 T
2.9 T
3.58 T
Germany
215.84 B
4.26 T
2.13 T
2.13 T
United Kingdom
73.23 B
3.12 T
1.31 T
1.09 T
France
62.23 B
2.96 T
1.3 T
1.27 T
Italy
40.39 B
2.41 T
1.03 T
1.1 T
Russian Federation
195.91 B
2.29 T
1.03 T
764.02 B
Brazil
17.03 B
2.62 T
741.79 B
412.99 B
India
37.03 B
3.39 T
741.43 B
296.04 B
Canada
40.46 B
2.14 T
732.64 B
579.94 B
Spain
12.07 B
1.63 T
607.23 B
530.56 B
Korea, Rep.
2.42 B
1.81 T
539.66 B
330.65 B
Australia
18.61 B
1.68 T
502.19 B
312.14 B
Mexico
13.04 B
1.41 T
501.07 B
313.14 B
Netherlands
12.28 B
1.01 T
399.68 B
327.5 B
Poland
65.98 B
688.18 B
349.13 B
344.63 B
Switzerland
9.52 B
807.71 B
339.86 B
294.79 B
Indonesia
5.67 B
1.32 T
334.97 B
149 B
Turkiye
7.55 B
957.8 B
297.17 B
150.68 B
Saudi Arabia
4.19 B
1.11 T
293.32 B
158.66 B
Sweden
15.82 B
636.86 B
254.93 B
229.03 B
Belgium
11.66 B
594.35 B
236.48 B
210.51 B
Argentina
18.27 B
643.63 B
219.44 B
141.35 B
Austria
6.59 B
480.37 B
192.12 B
173.79 B
Norway
5.16 B
579.27 B
181.3 B
120.58 B
Israel
2.6 B
522.03 B
178.16 B
138 B
Iran, Islamic Rep.
4.2 B
644.04 B
177.21 B
120.5 B
Denmark
11.93 B
398.3 B
173.41 B
156.16 B
United Arab Emirates
14.72 B
507.53 B
168.38 B
81.64 B
Thailand
2.76 B
543.98 B
153.55 B
98.23 B
Czechia
29.86 B
290.92 B
147.49 B
156.26 B
Nigeria
4.2 B
574.18 B
147.15 B
54.6 B
Hong Kong SAR, China
1.32 B
368.91 B
124.16 B
88.96 B
Finland
5.22 B
296.39 B
122.57 B
112.53 B
Romania
25.12 B
301.26 B
120.36 B
86.71 B
Greece
4.34 B
355.91 B
119.41 B
105.14 B
Ireland
1.94 B
529.24 B
116.64 B
49.79 B
Vietnam
6.29 B
408.8 B
115.5 B
42.49 B
Colombia
4.03 B
382.09 B
112.46 B
49.64 B
Malaysia
1.9 B
406.31 B
109.77 B
49.14 B
Egypt, Arab Rep.
4.95 B
476.75 B
108.71 B
49.24 B
Singapore
704.75 M
466.79 B
106.6 B
45.47 B
Philippines
4.95 B
404.28 B
105.76 B
51.78 B
Portugal
3.19 B
263.42 B
104.16 B
89.23 B
Hungary
34.75 B
182.28 B
104.09 B
120.44 B
Kazakhstan
16.87 B
236.63 B
99.99 B
81 B
Ukraine
31.58 B
199.77 B
99.96 B
85.97 B
Venezuela, RB
7.66 B
482.36 B
96.92 B
60.04 B
Pakistan
3.75 B
376.53 B
94.51 B
45.63 B
Chile
4.11 B
316.71 B
91.12 B
37.84 B
Bangladesh
4.27 B
460.2 B
77.22 B
31.6 B
New Zealand
5.18 B
255.55 B
74.43 B
45.34 B
Algeria
2 B
213.81 B
71.79 B
48.8 B
Iraq
407.8 M
264.18 B
70.14 B
37.82 B
Peru
2.57 B
242.63 B
67.42 B
34.83 B
Slovak Republic
12.75 B
118.66 B
64.98 B
70.77 B
Qatar
301.79 M
237.3 B
55.96 B
9.06 B
Kuwait
2.1 B
184.56 B
54.9 B
25.5 B
Libya
20.48 B
92.54 B
47.56 B
45.75 B
Croatia
22.13 B
70.96 B
47.5 B
54.16 B
Morocco
2.03 B
142.87 B
46.48 B
31.66 B
Puerto Rico
1.69 B
113.43 B
45.9 B
32.29 B
Syrian Arab Republic
857.7 M
252.52 B
43.21 B
19.2 B
Slovenia
20.29 B
62.12 B
40.52 B
45.67 B
Angola
3.39 B
137.24 B
39.73 B
15.11 B
Belarus
12.14 B
78.81 B
39.06 B
36.96 B
Cuba
5.69 B
107.35 B
39.03 B
27.46 B
Serbia
6.88 B
63.5 B
36.64 B
41.26 B
Uzbekistan
9.69 B
86.2 B
36.47 B
17.33 B
Lithuania
7.87 B
70.33 B
34.37 B
38.54 B
Ecuador
1.52 B
115.05 B
34.25 B
18.33 B
Bulgaria
9.7 B
89.04 B
34 B
21.99 B
Ethiopia
6.93 B
126.78 B
30.5 B
11.19 B
Azerbaijan
444.66 M
78.72 B
29.42 B
20.98 B
Oman
63.28 M
114.67 B
28.69 B
12.71 B
Tanzania
4.26 B
75.71 B
27.09 B
18.4 B
Lebanon
2.72 B
54.9 B
25.7 B
21.33 B
Dominican Republic
654.1 M
113.64 B
25.31 B
11.59 B
Sudan
1.13 B
129.72 B
24.61 B
11.25 B
Luxembourg
703.93 M
85.51 B
24.47 B
13.83 B
Sri Lanka
1.24 B
94.49 B
24.41 B
9 B
Kenya
791.27 M
113.42 B
23.18 B
8.21 B
Latvia
5.79 B
41.15 B
22.44 B
26.84 B
Guatemala
1.04 B
95 B
22.21 B
9.72 B
Tunisia
991.05 M
50.27 B
21.04 B
15.56 B
Uruguay
1.24 B
71.18 B
20.32 B
11.21 B
Cote d'Ivoire
546.2 M
71.81 B
18.84 B
10.49 B
Estonia
4.5 B
38.1 B
18.73 B
21.04 B
Yemen, Rep.
4.17 B
43.23 B
18.23 B
13.87 B
Macao SAR, China
1.13 B
55.28 B
18.13 B
7.37 B
Costa Rica
479.18 M
68.38 B
17.71 B
7.2 B
Panama
537.15 M
76.52 B
17.01 B
7.07 B
Ghana
1.22 B
79.16 B
16.77 B
5.45 B
Bahrain
3.05 B
44.39 B
16.1 B
9.06 B
Myanmar
317.82 M
78.93 B
15.99 B
2.18 B
Turkmenistan
2.33 B
45.61 B
15.9 B
6.41 B
Congo, Dem. Rep.
2.88 B
58.07 B
15.32 B
9.6 B
Cameroon
614.21 M
45.34 B
14.33 B
11.01 B
South Sudan
11.93 B
18.43 B
14.08 B
14.27 B
Jordan
561.12 M
47.45 B
13.39 B
6.56 B
Bosnia and Herzegovina
1.26 B
24.53 B
13.2 B
16.4 B
Paraguay
443.59 M
41.72 B
13.04 B
7.22 B
Cyprus
489.91 M
28.44 B
12.88 B
10.13 B
Bolivia
377.02 M
43.07 B
11.05 B
5.73 B
El Salvador
877.72 M
32.49 B
10.49 B
7.18 B
Uganda
423.01 M
45.56 B
10.21 B
4.3 B
Trinidad and Tobago
535.67 M
29.49 B
9.99 B
5.76 B
Georgia
2.51 B
24.61 B
9.83 B
7.75 B
Mozambique
2.64 B
17.85 B
9.79 B
9.81 B
West Bank and Gaza
2.84 B
19.11 B
9.17 B
7.31 B
Channel Islands
5.95 B
11.74 B
8.68 B
9.17 B
Iceland
248.43 M
27.84 B
8.54 B
6.39 B
Nepal
496.1 M
40.83 B
8.47 B
3.63 B
Senegal
1 B
27.68 B
8.47 B
6.37 B
Zimbabwe
1.05 B
34.16 B
8.33 B
6.56 B
Honduras
335.65 M
31.72 B
8.16 B
5.28 B
Cambodia
505.55 M
29.96 B
8.08 B
3.65 B
Zambia
693.14 M
29.78 B
8.04 B
3.6 B
Afghanistan
537.78 M
20.56 B
7.72 B
3.67 B
North Macedonia
2.44 B
13.83 B
7.61 B
6.86 B
Albania
652.17 M
18.88 B
7.48 B
5.61 B
Papua New Guinea
230.5 M
30.63 B
7.06 B
3.48 B
Kosovo
5.02 B
9.43 B
6.96 B
6.74 B
Armenia
1.2 B
19.5 B
6.9 B
6.38 B
Jamaica
699.06 M
17.1 B
6.78 B
4.4 B
Gabon
141.47 M
21.07 B
6.59 B
4.38 B
Namibia
1.61 B
13.68 B
6.51 B
3.99 B
Brunei Darussalam
114.04 M
19.05 B
6.44 B
4.32 B
Lao PDR
598.96 M
18.98 B
6.23 B
2.37 B
Guinea
2 B
21.23 B
6.18 B
3.78 B
Moldova
1.17 B
14.42 B
6.09 B
5.75 B
Mauritius
704.03 M
14.74 B
6.04 B
4.42 B
Botswana
30.41 M
20.35 B
5.61 B
3.94 B
Mali
275.49 M
19.31 B
5.56 B
2.75 B
Madagascar
673.08 M
14.95 B
5.44 B
4.26 B
Mongolia
768.4 M
16.81 B
5.43 B
2.81 B
Malawi
1.65 B
13.16 B
5.4 B
3.88 B
Malta
250.72 M
17.77 B
5.37 B
3.59 B
Haiti
271.07 M
20.88 B
5.32 B
2.61 B
Guam
3.39 B
6.37 B
5.08 B
5.12 B
Bahamas, The
169.8 M
13.06 B
5.04 B
3.11 B
Burkina Faso
330.44 M
19.74 B
4.92 B
2.59 B
Cayman Islands
4.16 B
6.03 B
4.82 B
4.57 B
Nicaragua
227.22 M
15.67 B
4.78 B
2.89 B
Equatorial Guinea
9.12 M
22.39 B
4.75 B
134.71 M
Congo, Rep.
131.73 M
17.96 B
4.7 B
2.3 B
Benin
226.2 M
17.69 B
4.47 B
1.7 B
Isle of Man
914.76 M
7.71 B
4.42 B
4.98 B
Tajikistan
860.54 M
10.49 B
4.41 B
2.83 B
Kyrgyz Republic
1.25 B
10.93 B
4.4 B
2.83 B
Niger
449.53 M
14.92 B
4.06 B
2.29 B
New Caledonia
159.59 M
10.64 B
4.01 B
2.92 B
Virgin Islands (U.S.)
3.26 B
4.78 B
4 B
4.09 B
Chad
313.58 M
13.94 B
3.87 B
1.45 B
Montenegro
984.29 M
6.1 B
3.8 B
4.16 B
Monaco
293.07 M
8.6 B
3.29 B
2.73 B
French Polynesia
176.53 M
7.14 B
3.29 B
3.57 B
Rwanda
119 M
13.31 B
3.04 B
1.94 B
Curacao
2.5 B
3.05 B
2.94 B
3.01 B
Barbados
311.8 M
5.64 B
2.89 B
2.82 B
Bermuda
84.47 M
7.55 B
2.83 B
1.63 B
Liechtenstein
90.1 M
7.19 B
2.72 B
2.36 B
Mauritania
159.21 M
10.38 B
2.7 B
1.76 B
Togo
164.6 M
8.33 B
2.51 B
1.84 B
Faroe Islands
1.07 B
3.65 B
2.27 B
2.38 B
Liberia
748 M
4 B
2.27 B
2.4 B
Aruba
405.59 M
3.4 B
2.01 B
2.15 B
Fiji
112.33 M
5.58 B
1.94 B
1.38 B
Somalia
180.46 M
8.13 B
1.91 B
710.85 M
Maldives
42.46 M
6.19 B
1.71 B
870.03 M
Eswatini
35.08 M
4.89 B
1.71 B
1.16 B
Andorra
78.62 M
4.09 B
1.66 B
1.21 B
Guyana
170.22 M
15.36 B
1.65 B
530.44 M
San Marino
1.01 B
2.39 B
1.64 B
1.66 B
Suriname
99.65 M
5.24 B
1.53 B
873 M
Sierra Leone
321.99 M
5.02 B
1.51 B
932.98 M
Greenland
69.52 M
3.24 B
1.35 B
1.07 B
Timor-Leste
367.09 M
3.62 B
1.23 B
1.04 B
Djibouti
340.99 M
3.52 B
1.23 B
666.07 M
Sint Maarten (Dutch part)
936.09 M
1.57 B
1.23 B
1.24 B
Burundi
158.99 M
3.1 B
1.14 B
969.05 M
St. Lucia
170.37 M
2.09 B
1.04 B
921.85 M
Northern Mariana Islands
729 M
1.56 B
1.02 B
939 M
Cabo Verde
132.02 M
2.31 B
1.01 B
592.42 M
Eritrea
467.87 M
2.07 B
977.77 M
748.95 M
Lesotho
34.58 M
2.58 B
941.68 M
704.33 M
Bhutan
128.67 M
2.54 B
905.91 M
442.96 M
Belize
28.07 M
2.82 B
863.68 M
599 M
Antigua and Barbuda
77.5 M
1.76 B
809.74 M
783.34 M
Turks and Caicos Islands
358.74 M
1.2 B
791.56 M
763.86 M
St. Martin (French part)
772.92 M
775.88 M
774.4 M
774.4 M
Gambia, The
41.16 M
2.27 B
730.61 M
690.31 M
Comoros
184.01 M
1.3 B
632.48 M
452.88 M
American Samoa
493 M
716 M
601.1 M
625 M
Grenada
71.49 M
1.26 B
546.31 M
501.03 M
Seychelles
11.59 M
1.65 B
542.91 M
398.31 M
Solomon Islands
25.2 M
1.62 B
542.26 M
402.84 M
Guinea-Bissau
78.54 M
1.64 B
509.89 M
253.97 M
Samoa
95.57 M
912.95 M
438.5 M
285.48 M
Vanuatu
113.78 M
983.58 M
422.75 M
270.01 M
St. Kitts and Nevis
12.37 M
1.11 B
359.54 M
220.54 M
St. Vincent and the Grenadines
13.07 M
948.56 M
341.87 M
254.83 M
Dominica
45.87 M
612.05 M
323.44 M
332.48 M
Sao Tome and Principe
75.95 M
546.68 M
269.58 M
241.09 M
Micronesia, Fed. Sts.
106.5 M
427.09 M
255.8 M
243.98 M
Tonga
30.04 M
512.05 M
225.3 M
199.21 M
Palau
146.3 M
298.3 M
214.04 M
198.59 M
Marshall Islands
31.02 M
279.67 M
132.37 M
127.11 M
Nauru
47.56 M
150.92 M
105.87 M
101.06 M
Kiribati
14.3 M
227.61 M
90.51 M
65.33 M
Tuvalu
8.82 M
60.35 M
27.9 M
24.1 M
GDP Trend Over Time (Line Plot)
The line chart highlights GDP growth over time. Economies of the top countries show upward trends, with occasional dips corresponding to economic recessions or regional crises. Normalizing to Trillions of USD makes the chart readable.
library(ggplot2)# Select top 10 countries by mean GDPtop10_countries <- gdp_summary |>slice_head(n =10) |>pull(Country)# Filter tidy data for top 10 countriesgdp_top10 <- gdp_long |>filter(Country %in% top10_countries)# Plot GDP trendsggplot(gdp_top10, aes(x = Year, y = GDP, color = Country)) +geom_line(size =1) +scale_y_continuous(labels =function(x) {ifelse(x >=1e12, paste0(round(x/1e12, 1), " T"),ifelse(x >=1e9, paste0(round(x/1e9, 1), " B"),ifelse(x >=1e6, paste0(round(x/1e6, 1), " M"),paste0(round(x/1e3, 1), " K")))) }) +labs(title ="GDP Trend Over Time for Top 10 Countries",x ="Year",y ="GDP (USD)",color ="Country" ) +theme_minimal() +theme(legend.position ="bottom",axis.text.x =element_text(angle =45, hjust =1) )
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
Percentage Contribution of Top 10 Countries to Total GDP
This horizontal bar chart displays the share of total GDP contributed by the top 10 countries in the dataset. Each bar represents a country, with the length proportional to its percentage of the combined GDP of all countries. The values are labeled on the bars for clarity, allowing easy comparison of economic size among the leading economies. The chart highlights which countries dominate global GDP and their relative contributions within this group.
library(dplyr)library(ggplot2)library(scales)# Calculate total GDP across all countriestotal_gdp_all <- gdp_long %>%group_by(Country) %>%summarise(Total_GDP =sum(GDP, na.rm =TRUE)) %>%ungroup() %>%summarise(Global_GDP =sum(Total_GDP, na.rm =TRUE)) %>%pull(Global_GDP)# Top 10 countries by total GDPgdp_top10_summary <- gdp_long %>%group_by(Country) %>%summarise(Total_GDP =sum(GDP, na.rm =TRUE)) %>%ungroup() %>%arrange(desc(Total_GDP)) %>%slice_head(n =10) %>%mutate(Percent_of_Total = Total_GDP / total_gdp_all *100)# Bar chartggplot(gdp_top10_summary, aes(x =reorder(Country, Percent_of_Total), y = Percent_of_Total, fill = Country)) +geom_col(show.legend =FALSE) +geom_text(aes(label =paste0(round(Percent_of_Total, 1), "%")), hjust =-0.1, size =4) +coord_flip() +# horizontal bars for readabilityscale_y_continuous(expand =expansion(mult =c(0, 0.1))) +labs(title ="Percentage Contribution of Top 10 Countries to Global GDP",x ="Country",y ="Percentage of Global GDP (%)" ) +theme_minimal() +theme(axis.text =element_text(size =12),axis.title =element_text(size =14),plot.title =element_text(size =16, face ="bold", hjust =0.5) )
Conclusion
The bar chart highlights that the United States, China, and Japan dominate the global economy among the top 10 countries, contributing a substantial share of total GDP. While the U.S. and China account for the largest portions, smaller economies like Mexico and the Netherlands have comparatively minor contributions. This visualization makes it easy to compare the relative economic weight of these leading countries and understand their impact on global economic trends. Overall, it underscores the concentration of global economic power in a few major nations.
References
OpenAI. (2026, March 8). ChatGPT conversation with K. M. Qaiduzzaman on data analysis, GDP visualization, and R programming. Retrieved from https://chat.openai.com/