Rows: 5275 Columns: 10
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (5): iso2c, iso3c, country, region, income
dbl (5): year, gdp_percap, population, birth_rate, neonat_mortal_rate
ℹ 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(nations)
# A tibble: 6 × 10
iso2c iso3c country year gdp_percap population birth_rate neonat_mortal_rate
<chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 AD AND Andorra 1996 NA 64291 10.9 2.8
2 AD AND Andorra 1994 NA 62707 10.9 3.2
3 AD AND Andorra 2003 NA 74783 10.3 2
4 AD AND Andorra 1990 NA 54511 11.9 4.3
5 AD AND Andorra 2009 NA 85474 9.9 1.7
6 AD AND Andorra 2011 NA 82326 NA 1.6
# ℹ 2 more variables: region <chr>, income <chr>
str(nations)
spc_tbl_ [5,275 × 10] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
$ iso2c : chr [1:5275] "AD" "AD" "AD" "AD" ...
$ iso3c : chr [1:5275] "AND" "AND" "AND" "AND" ...
$ country : chr [1:5275] "Andorra" "Andorra" "Andorra" "Andorra" ...
$ year : num [1:5275] 1996 1994 2003 1990 2009 ...
$ gdp_percap : num [1:5275] NA NA NA NA NA NA NA NA NA NA ...
$ population : num [1:5275] 64291 62707 74783 54511 85474 ...
$ birth_rate : num [1:5275] 10.9 10.9 10.3 11.9 9.9 NA 10.9 9.8 11.8 11.2 ...
$ neonat_mortal_rate: num [1:5275] 2.8 3.2 2 4.3 1.7 1.6 2 1.7 2.1 2.1 ...
$ region : chr [1:5275] "Europe & Central Asia" "Europe & Central Asia" "Europe & Central Asia" "Europe & Central Asia" ...
$ income : chr [1:5275] "High income" "High income" "High income" "High income" ...
- attr(*, "spec")=
.. cols(
.. iso2c = col_character(),
.. iso3c = col_character(),
.. country = col_character(),
.. year = col_double(),
.. gdp_percap = col_double(),
.. population = col_double(),
.. birth_rate = col_double(),
.. neonat_mortal_rate = col_double(),
.. region = col_character(),
.. income = col_character()
.. )
- attr(*, "problems")=<pointer: 0x0000023785dfbde0>
Create a new variable in the data
#create a new variable that puts each country's GDP in trillions of dollars using mutate()nations2 <-mutate(nations, gdp_tril=((gdp_percap * population)/10^12))tail(nations2)
The four countries I would like to compare are China, Japan, South Korea (Republic of Korea), and the United States.
#filter to four desired countriesnations3 <-filter(nations2, country %in%c("China", "Japan", "Korea, Rep.", "United States"))nations3
# A tibble: 100 × 11
iso2c iso3c country year gdp_percap population birth_rate neonat_mortal_rate
<chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 CN CHN China 1992 1260. 1164970000 18.3 29.4
2 CN CHN China 2005 5053. 1303720000 12.4 14
3 CN CHN China 2000 2915. 1262645000 14.0 21.2
4 CN CHN China 1991 1091. 1150780000 19.7 29.7
5 CN CHN China 2013 12219. 1357380000 12.1 6.3
6 CN CHN China 1999 2650. 1252735000 14.6 22.2
7 CN CHN China 2014 13255. 1364270000 12.4 5.9
8 CN CHN China 2003 3934. 1288400000 12.4 17.1
9 CN CHN China 2004 4423. 1296075000 12.3 15.5
10 CN CHN China 1993 1453. 1178440000 18.1 28.8
# ℹ 90 more rows
# ℹ 3 more variables: region <chr>, income <chr>, gdp_tril <dbl>
#create a line graph to show selected countries' GDP over timegdp_compare <-ggplot(nations3, aes(x=year, y=gdp_tril, color = country)) +geom_point(size =2) +geom_line() +labs(x ="Year",y ="GDP ($ Trillion)",title ="GDP of East Asian Countries vs United States")+scale_color_brewer(name ="Country", palette ="Set1")+theme_economist()gdp_compare
Plot the second chart
#group the nations by region and year, and exclude empty values from sum()nations4 <- nations2 |>group_by(region, year) |>summarise(GDP =sum(gdp_tril,na.rm =TRUE))
`summarise()` has regrouped the output.
ℹ Summaries were computed grouped by region and year.
ℹ Output is grouped by region.
ℹ Use `summarise(.groups = "drop_last")` to silence this message.
ℹ Use `summarise(.by = c(region, year))` for per-operation grouping
(`?dplyr::dplyr_by`) instead.
#create an area graph by world regions over timeworld_gdp <-ggplot(nations4, aes(x=year, y=GDP, fill = region)) +geom_area(color="white", linewidth = .25) +labs(x ="Year",y ="GDP ($ Trillion)",title ="GDP by Regions around the World") +scale_fill_brewer(name ="Region", palette ="Set2")+theme_bw()world_gdp
The difference between the charts of GDP by individual country versus GDP by World Region is really interesting because in the individual countries you can see that United States and China have really high GDPs (over $15 trillion) whereas in the other chart, North America (which includes the United States) is in the bottom three.
The fill wasn’t showing up when I first ran the code, and then I realized that I didn’t put the value gdp_tril in the sum() function. It showed up when I fixed my code.