Hw Week 6

Author

Andreas Sanches

China’s Rise to Become the Largest Economy

We starting downloading and opening tidyverse and our nations dataset.

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.2     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.3     ✔ tibble    3.2.1
✔ lubridate 1.9.2     ✔ tidyr     1.3.0
✔ 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
setwd("/Users/andreassanches/Documents")
nations <- read_csv("nations.csv")
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.

Select, filter and mutate

Now, we select the variables we will work with, we filter to get 4 countries (in this case I chose US, China, Japan and Germany). Then, I used the general GDP formula and created the “gdp_countries” variable.

countries_selected <- nations %>%
  select(gdp_percap, population, country, year) %>%
  filter(country %in% c("United States", "China", "Japan", "Germany")) %>% mutate(gdp_countries = gdp_percap * population / 1000000000000)
countries_selected
# A tibble: 100 × 5
   gdp_percap population country  year gdp_countries
        <dbl>      <dbl> <chr>   <dbl>         <dbl>
 1      1260. 1164970000 China    1992          1.47
 2      5053. 1303720000 China    2005          6.59
 3      2915. 1262645000 China    2000          3.68
 4      1091. 1150780000 China    1991          1.26
 5     12219. 1357380000 China    2013         16.6 
 6      2650. 1252735000 China    1999          3.32
 7     13255. 1364270000 China    2014         18.1 
 8      3934. 1288400000 China    2003          5.07
 9      4423. 1296075000 China    2004          5.73
10      1453. 1178440000 China    1993          1.71
# ℹ 90 more rows

Create your visualization

Now, it’s time to label and define the type of graph you will create with what you have. In this case I created a line graph using the “geom_line” function.

countries_selected_chart <- ggplot(countries_selected, aes(x = year, y = gdp_countries, color = country)) +
  labs(title = "China's Rise to Become the Largest Economy") +
  geom_line() +
  xlab("Year") +
  ylab("GDP ($ trillion)")
print(countries_selected_chart)

Adding points to your graph

In order make my graph better looking, I added some points to it using the “geom_point” function.

countries_selected_chart + geom_point() + scale_color_brewer(palette = "Set1")