install.packages("bea.R", repos = "https://cloud.r-project.org")
## 
## The downloaded binary packages are in
##  /var/folders/5_/389qrkvs1sd7nkp792bslx5r0000gn/T//RtmpHny58n/downloaded_packages
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.1     ✔ purrr   1.0.1
## ✔ tibble  3.1.8     ✔ dplyr   1.1.0
## ✔ tidyr   1.3.0     ✔ stringr 1.5.0
## ✔ readr   2.1.3     ✔ forcats 1.0.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(bea.R)
## Loading required package: data.table
## 
## Attaching package: 'data.table'
## 
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
## 
## The following object is masked from 'package:purrr':
## 
##     transpose
## 
## Creating a generic function for 'toJSON' from package 'jsonlite' in package 'googleVis'
## Note: As of February 2018, beaGet() requires 'TableName' for NIPA and NIUnderlyingDetail data instead of 'TableID.' See https://github.us-bea/bea.R for details.
# Setup API key
beaKey <- "0568977E-7B8B-42CB-A6F9-AA92F36A7AC8"
beaSets(beaKey = beaKey)
## No encoding supplied: defaulting to UTF-8.
## $Dataset
##                DatasetName                   DatasetDescription
## 1                     NIPA                 Standard NIPA tables
## 2       NIUnderlyingDetail Standard NI underlying detail tables
## 3                      MNE            Multinational Enterprises
## 4              FixedAssets         Standard Fixed Assets tables
## 5                      ITA  International Transactions Accounts
## 6                      IIP    International Investment Position
## 7              InputOutput                    Input-Output Data
## 8            IntlServTrade         International Services Trade
## 9            GDPbyIndustry                      GDP by Industry
## 10                Regional                   Regional data sets
## 11 UnderlyingGDPbyIndustry           Underlying GDP by Industry
## 12      APIDatasetMetaData    Metadata about other API datasets
## 
## attr(,"params")
##   ParameterName                       ParameterValue
## 1        USERID 0568977E-7B8B-42CB-A6F9-AA92F36A7AC8
## 2        METHOD                       GETDATASETLIST
## 3  RESULTFORMAT                                 JSON
#beaParams(beaKey = beaKey, "Regional")
beaSearch(searchTerm = 'GDP',
          beaKey     =  beaKey, 
          asHtml     = TRUE
          )
## Creating first-time local copy of metadata for all datasets - only done once.
## Datasets will be updated only if timestamps indicate metadata obsolete in future searches,
## and only obsolete metadata sets will be updated (it's faster this way).
## 
## No encoding supplied: defaulting to UTF-8.
## Warning in beaSearch(searchTerm = "GDP", beaKey = beaKey, asHtml = TRUE):
## Regional metadata is missing from
## /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/beaR/data
## and may be locked for updating on the BEA API; searching national metadata
## only.
beaSpecs10 <- list(
  "UserID" = beaKey, # Set up API key
  "Method" = "GetData", # Method
  "datasetname" = "NIPA", # Specify dataset
  "TableName" = "T10705", # Specify table within the dataset
  "frequency" = "A", # A = annual, Q = quartely 
#  "LineCode" = 10, # Specify the line code
# "GeoFips" = "National", # Specify the geographical level
  "Year" = "x"# Specify the year
)
beaPayload <- beaGet(beaSpecs10)
## No encoding supplied: defaulting to UTF-8.
head(beaPayload)
##    TableName SeriesCode LineNumber
## 1:    T10705     A191RC          1
## 2:    T10705     B645RC          2
## 3:    T10705     A655RC          3
## 4:    T10705     A001RC          4
## 5:    T10705     A262RC          5
## 6:    T10705     A024RC          6
##                                     LineDescription     METRIC_NAME CL_UNIT
## 1:                     Gross domestic product (GDP) Current Dollars   Level
## 2: Plus: Income receipts from the rest of the world Current Dollars   Level
## 3:   Less: Income payments to the rest of the world Current Dollars   Level
## 4:                   Equals: Gross national product Current Dollars   Level
## 5:               Less: Consumption of fixed capital Current Dollars   Level
## 6:                                          Private Current Dollars   Level
##    UNIT_MULT DataValue_1929 DataValue_1930 DataValue_1931 DataValue_1932
## 1:         6         104556          92160          77391          59522
## 2:         6           1140           1041            767            528
## 3:         6            374            332            252            164
## 4:         6         105322          92869          77906          59886
## 5:         6          10409          10217           9514           8338
## 6:         6           9411           9221           8550           7465
##    DataValue_1933 DataValue_1934 DataValue_1935 DataValue_1936 DataValue_1937
## 1:          57154          66800          74241          84830          93003
## 2:            438            438            522            570            728
## 3:            138            159            181            308            359
## 4:          57454          67079          74582          85092          93372
## 5:           8012           8430           8480           8801           9767
## 6:           7042           7288           7324           7509           8371
##    DataValue_1938 DataValue_1939 DataValue_1940 DataValue_1941 DataValue_1942
## 1:          87352          93437         102899         129309         165952
## 2:            640            683            610            731            708
## 3:            261            310            330            315            297
## 4:          87731          93810         103179         129725         166363
## 5:          10042          10103          10577          12062          14911
## 6:           8580           8590           8976           9950          11246
##    DataValue_1943 DataValue_1944 DataValue_1945 DataValue_1946 DataValue_1947
## 1:         203084         224447         228007         227535         249616
## 2:            699            758            756           1092           1597
## 3:            368            434            491            440            487
## 4:         203415         224771         228272         228187         250726
## 5:          18031          21341          23107          25690          29121
## 6:          11539          12038          12507          14247          17735
##    DataValue_1948 DataValue_1949 DataValue_1950 DataValue_1951 DataValue_1952
## 1:         274468         272475         299827         346914         367341
## 2:           2031           1944           2186           2756           2857
## 3:            575            664            746            864            872
## 4:         275924         273755         301267         348806         369326
## 5:          31326          32284          33394          37726          40606
## 6:          20820          22590          24334          27746          29544
##    DataValue_1953 DataValue_1954 DataValue_1955 DataValue_1956 DataValue_1957
## 1:         389218         390549         425478         449353         474039
## 2:           2843           3039           3507           3933           4268
## 3:            944            944           1058           1137           1213
## 4:         391117         392644         427927         452149         477093
## 5:          43488          45981          48893          54127          58919
## 6:          31344          32958          34995          38811          42259
##    DataValue_1958 DataValue_1959 DataValue_1960 DataValue_1961 DataValue_1962
## 1:         481229         521654         542382         562209         603922
## 2:           3869           4269           4888           5307           5940
## 3:           1241           1508           1775           1788           1847
## 4:         483858         524415         545494         565728         608015
## 5:          62454          65445          67901          70604          74100
## 6:          44910          46819          48209          49781          51795
##    DataValue_1963 DataValue_1964 DataValue_1965 DataValue_1966 DataValue_1967
## 1:         637450         684460         742289         813414         859959
## 2:           6530           7239           7881           8071           8682
## 3:           2053           2319           2602           3001           3289
## 4:         641927         689380         747568         818484         865352
## 5:          78018          82390          88008          95311         103557
## 6:          54158          57276          61568          67172          73327
##    DataValue_1968 DataValue_1969 DataValue_1970 DataValue_1971 DataValue_1972
## 1:         940651        1017615        1073303        1164850        1279110
## 2:          10102          11779          12833          14011          16278
## 3:           4017           5664           6440           6417           7720
## 4:         946736        1023730        1079696        1172444        1287668
## 5:         113357         124896         136839         148926         161011
## 6:          80603          89433          98260         107635         117493
##    DataValue_1973 DataValue_1974 DataValue_1975 DataValue_1976 DataValue_1977
## 1:        1425376        1545243        1684904        1873412        2081826
## 2:          23541          29820          28024          32363          37200
## 3:          10922          14300          15020          15516          16908
## 4:        1437994        1560763        1697908        1890259        2102118
## 5:         178686         206894         238510         260226         289832
## 6:         131492         153159         178790         196512         221127
##    DataValue_1978 DataValue_1979 DataValue_1980 DataValue_1981 DataValue_1982
## 1:        2351599        2627333        2857307        3207041        3343789
## 2:          46252          68321          79092          92024         100993
## 3:          24671          36390          44907          59089          64483
## 4:        2373180        2659264        2891492        3239976        3380299
## 5:         327196         373882         428432         487231         536963
## 6:         252115         290733         334977         381932         420392
##    DataValue_1983 DataValue_1984 DataValue_1985 DataValue_1986 DataValue_1987
## 1:        3634038        4037613        4338979        4579631        4855215
## 2:         101882         121865         112662         111324         123286
## 3:          64797          85575          87297          94367         105798
## 4:        3671122        4073903        4364344        4596588        4872702
## 5:         562624         598394         640137         685295         730385
## 6:         438788         463516         496410         531572         566309
##    DataValue_1988 DataValue_1989 DataValue_1990 DataValue_1991 DataValue_1992
## 1:        5236438        5641580        5963144        6158129        6520327
## 2:         152116         177699         188847         168363         152052
## 3:         129459         152909         154155         136777         120975
## 4:        5259095        5666369        5997836        6189716        6551404
## 5:         784496         838258         888532         932393         960247
## 6:         607913         649619         688396         721456         742886
##    DataValue_1993 DataValue_1994 DataValue_1995 DataValue_1996 DataValue_1997
## 1:        6858559        7287236        7639749        8073122        8577552
## 2:         155600         184543         229833         246404         280071
## 3:         124442         161577         201881         215535         256771
## 4:        6889717        7310202        7667701        8103991        8600853
## 5:        1003498        1055610        1122381        1175306        1239325
## 6:         778210         822507         880728         929111         987753
##    DataValue_1998 DataValue_1999 DataValue_2000 DataValue_2001 DataValue_2002
## 1:        9062817        9631172       10250952       10581929       10929108
## 2:         286779         324646         390638         339642         335811
## 3:         269368         293742         352157         289278         290017
## 4:        9080228        9662075       10289432       10632293       10974902
## 5:        1309737        1398934        1511225        1599511        1657976
## 6:        1052165        1132208        1231511        1311709        1361815
##    DataValue_2003 DataValue_2004 DataValue_2005 DataValue_2006 DataValue_2007
## 1:       11456450       12217196       13039197       13815583       14474228
## 2:         377405         464696         569269         702621         850184
## 3:         318909         387983         494469         656161         754520
## 4:       11514946       12293909       13113997       13862043       14569892
## 5:        1719081        1821828        1971024        2124124        2252806
## 6:        1411949        1497111        1622603        1751800        1852499
##    DataValue_2008 DataValue_2009 DataValue_2010 DataValue_2011 DataValue_2012
## 1:       14769862       14478067       15048970       15599731       16253970
## 2:         855223         689256         759969         827868         827438
## 3:         710009         539013         554336         589936         594681
## 4:       14915076       14628309       15254603       15837664       16486727
## 5:        2358842        2371476        2390926        2474467        2575995
## 6:        1931823        1928709        1933775        1997313        2082378
##    DataValue_2013 DataValue_2014 DataValue_2015 DataValue_2016 DataValue_2017
## 1:       16843196       17550687       18206023       18695106       19477337
## 2:         847233         881582         860759         893475        1031102
## 3:         616935         646356         640376         661531         738153
## 4:       17073493       17785914       18426407       18927050       19770286
## 5:        2681218        2815026        2911385        2987071        3118724
## 6:        2176569        2298472        2388479        2459936        2576755
##    DataValue_2018 DataValue_2019 DataValue_2020 DataValue_2021 DataValue_2022
## 1:       20533058       21380976       21060474       23315081       25461339
## 2:        1138659        1172200         971295        1086987             NA
## 3:         848352         894150         774325         913897             NA
## 4:       20823364       21659027       21257444       23488172             NA
## 5:        3275618        3436609        3577770        3831587        4284265
## 6:        2710537        2850090        2971829        3184502        3567807
bea_10 <- beaGet(beaSpecs10, asWide = FALSE)
## No encoding supplied: defaulting to UTF-8.
knitr::kable(head(bea_10))
TableName SeriesCode LineNumber LineDescription TimePeriod METRIC_NAME CL_UNIT UNIT_MULT DataValue NoteRef
T10705 A191RC 1 Gross domestic product (GDP) 1929 Current Dollars Level 6 104556 T10705
T10705 A191RC 1 Gross domestic product (GDP) 1930 Current Dollars Level 6 92160 T10705
T10705 A191RC 1 Gross domestic product (GDP) 1931 Current Dollars Level 6 77391 T10705
T10705 A191RC 1 Gross domestic product (GDP) 1932 Current Dollars Level 6 59522 T10705
T10705 A191RC 1 Gross domestic product (GDP) 1933 Current Dollars Level 6 57154 T10705
T10705 A191RC 1 Gross domestic product (GDP) 1934 Current Dollars Level 6 66800 T10705
install.packages("ggplot2", repos = "https://cloud.r-project.org")
## 
## The downloaded binary packages are in
##  /var/folders/5_/389qrkvs1sd7nkp792bslx5r0000gn/T//RtmpHny58n/downloaded_packages
library(ggplot2)
#filtering the data 

# delete rows by range
df2 <- beaPayload[-(2:34),]

head(df2)
##    TableName SeriesCode LineNumber              LineDescription     METRIC_NAME
## 1:    T10705     A191RC          1 Gross domestic product (GDP) Current Dollars
##    CL_UNIT UNIT_MULT DataValue_1929 DataValue_1930 DataValue_1931
## 1:   Level         6         104556          92160          77391
##    DataValue_1932 DataValue_1933 DataValue_1934 DataValue_1935 DataValue_1936
## 1:          59522          57154          66800          74241          84830
##    DataValue_1937 DataValue_1938 DataValue_1939 DataValue_1940 DataValue_1941
## 1:          93003          87352          93437         102899         129309
##    DataValue_1942 DataValue_1943 DataValue_1944 DataValue_1945 DataValue_1946
## 1:         165952         203084         224447         228007         227535
##    DataValue_1947 DataValue_1948 DataValue_1949 DataValue_1950 DataValue_1951
## 1:         249616         274468         272475         299827         346914
##    DataValue_1952 DataValue_1953 DataValue_1954 DataValue_1955 DataValue_1956
## 1:         367341         389218         390549         425478         449353
##    DataValue_1957 DataValue_1958 DataValue_1959 DataValue_1960 DataValue_1961
## 1:         474039         481229         521654         542382         562209
##    DataValue_1962 DataValue_1963 DataValue_1964 DataValue_1965 DataValue_1966
## 1:         603922         637450         684460         742289         813414
##    DataValue_1967 DataValue_1968 DataValue_1969 DataValue_1970 DataValue_1971
## 1:         859959         940651        1017615        1073303        1164850
##    DataValue_1972 DataValue_1973 DataValue_1974 DataValue_1975 DataValue_1976
## 1:        1279110        1425376        1545243        1684904        1873412
##    DataValue_1977 DataValue_1978 DataValue_1979 DataValue_1980 DataValue_1981
## 1:        2081826        2351599        2627333        2857307        3207041
##    DataValue_1982 DataValue_1983 DataValue_1984 DataValue_1985 DataValue_1986
## 1:        3343789        3634038        4037613        4338979        4579631
##    DataValue_1987 DataValue_1988 DataValue_1989 DataValue_1990 DataValue_1991
## 1:        4855215        5236438        5641580        5963144        6158129
##    DataValue_1992 DataValue_1993 DataValue_1994 DataValue_1995 DataValue_1996
## 1:        6520327        6858559        7287236        7639749        8073122
##    DataValue_1997 DataValue_1998 DataValue_1999 DataValue_2000 DataValue_2001
## 1:        8577552        9062817        9631172       10250952       10581929
##    DataValue_2002 DataValue_2003 DataValue_2004 DataValue_2005 DataValue_2006
## 1:       10929108       11456450       12217196       13039197       13815583
##    DataValue_2007 DataValue_2008 DataValue_2009 DataValue_2010 DataValue_2011
## 1:       14474228       14769862       14478067       15048970       15599731
##    DataValue_2012 DataValue_2013 DataValue_2014 DataValue_2015 DataValue_2016
## 1:       16253970       16843196       17550687       18206023       18695106
##    DataValue_2017 DataValue_2018 DataValue_2019 DataValue_2020 DataValue_2021
## 1:       19477337       20533058       21380976       21060474       23315081
##    DataValue_2022
## 1:       25461339
# Edit column names
colnames(df2) <- sub("DataValue_", "", colnames(df2))

# View data
head(df2)
##    TableName SeriesCode LineNumber              LineDescription     METRIC_NAME
## 1:    T10705     A191RC          1 Gross domestic product (GDP) Current Dollars
##    CL_UNIT UNIT_MULT   1929  1930  1931  1932  1933  1934  1935  1936  1937
## 1:   Level         6 104556 92160 77391 59522 57154 66800 74241 84830 93003
##     1938  1939   1940   1941   1942   1943   1944   1945   1946   1947   1948
## 1: 87352 93437 102899 129309 165952 203084 224447 228007 227535 249616 274468
##      1949   1950   1951   1952   1953   1954   1955   1956   1957   1958   1959
## 1: 272475 299827 346914 367341 389218 390549 425478 449353 474039 481229 521654
##      1960   1961   1962   1963   1964   1965   1966   1967   1968    1969
## 1: 542382 562209 603922 637450 684460 742289 813414 859959 940651 1017615
##       1970    1971    1972    1973    1974    1975    1976    1977    1978
## 1: 1073303 1164850 1279110 1425376 1545243 1684904 1873412 2081826 2351599
##       1979    1980    1981    1982    1983    1984    1985    1986    1987
## 1: 2627333 2857307 3207041 3343789 3634038 4037613 4338979 4579631 4855215
##       1988    1989    1990    1991    1992    1993    1994    1995    1996
## 1: 5236438 5641580 5963144 6158129 6520327 6858559 7287236 7639749 8073122
##       1997    1998    1999     2000     2001     2002     2003     2004
## 1: 8577552 9062817 9631172 10250952 10581929 10929108 11456450 12217196
##        2005     2006     2007     2008     2009     2010     2011     2012
## 1: 13039197 13815583 14474228 14769862 14478067 15048970 15599731 16253970
##        2013     2014     2015     2016     2017     2018     2019     2020
## 1: 16843196 17550687 18206023 18695106 19477337 20533058 21380976 21060474
##        2021     2022
## 1: 23315081 25461339
# Create a long format table
?pivot_longer
df2_long <- pivot_longer(df2, cols =8:101,                names_to = "year", values_to = "value"
            )
df2_long
## # A tibble: 94 × 9
##    TableName SeriesCode LineNumber LineDe…¹ METRI…² CL_UNIT UNIT_…³ year   value
##    <chr>     <chr>      <chr>      <chr>    <chr>   <chr>   <chr>   <chr>  <dbl>
##  1 T10705    A191RC     1          Gross d… Curren… Level   6       1929  104556
##  2 T10705    A191RC     1          Gross d… Curren… Level   6       1930   92160
##  3 T10705    A191RC     1          Gross d… Curren… Level   6       1931   77391
##  4 T10705    A191RC     1          Gross d… Curren… Level   6       1932   59522
##  5 T10705    A191RC     1          Gross d… Curren… Level   6       1933   57154
##  6 T10705    A191RC     1          Gross d… Curren… Level   6       1934   66800
##  7 T10705    A191RC     1          Gross d… Curren… Level   6       1935   74241
##  8 T10705    A191RC     1          Gross d… Curren… Level   6       1936   84830
##  9 T10705    A191RC     1          Gross d… Curren… Level   6       1937   93003
## 10 T10705    A191RC     1          Gross d… Curren… Level   6       1938   87352
## # … with 84 more rows, and abbreviated variable names ¹​LineDescription,
## #   ²​METRIC_NAME, ³​UNIT_MULT
?ggplot
fplot <- ggplot(df2_long, aes(year,     value),na.rm = TRUE) + 
  geom_point(color = "green", size = 2)
fplot

Adjusting the table to clearly see x labels.

?ggplot
fplot2 <- ggplot(df2_long, aes(year, value), na.rm = TRUE) + 
  geom_point(color = "green", size = 2) +  theme(axis.text.x = element_text(angle = 30, hjust = 0.5, vjust = 0.5))
fplot2

ggplot(df2_long,
       aes(year,value), na.rm = TRUE) +
  geom_point(color = "pink" , size = 5) +
  labs(x = "YEAR", y = "VALUES") +
  theme(axis.text.x = element_text(angle = 30, hjust = 0.5, vjust = 0.5))

GDP dropped in 2008 due to the financial crisis. One of the actions that led to this event was the collapse of the subprime credit mortgage, leading to defaults on housing loans and creating a credit crunch in the banking system, reducing significant amounts of bank lending. On the other hand, GDP dropped in 2020 due to the global crisis of the Covid-19 pandemic, which resulted in an incredibly slow supply chain, people being unable to work, and a reduction in consumer spending.