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()
nd <- 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.
withGDP <- nd %>% mutate(GDP = gdp_percap * population / 1000000000000 ) %>% select(country, GDP, year) %>% filter(country == c("United States", "Mexico", "Panama", "France"))
## Warning: There was 1 warning in `filter()`.
## ℹ In argument: `country == c("United States", "Mexico", "Panama", "France")`.
## Caused by warning in `country == c("United States", "Mexico", "Panama", "France")`:
## ! longer object length is not a multiple of shorter object length
glimpse(withGDP)
## Rows: 24
## Columns: 3
## $ country <chr> "France", "France", "France", "France", "France", "France", "M…
## $ GDP <dbl> 1.76167055, 2.26225547, 1.82020918, 1.27204185, 1.58203157, 1.…
## $ year <dbl> 2002, 2008, 2004, 1996, 2000, 2003, 1994, 2000, 1992, 2007, 19…
GDPld <- ggplot(withGDP, aes(x = year, y= GDP, color = country)) +
xlab("Year") +
ylab("GDP ($trillion)") +
scale_color_brewer(palette = "Set1") +
geom_point() +
geom_line() +
ggtitle("GDP Comparison of Four Personal Countries")
GDPld
GDP2 <- nd %>% mutate(GDP = gdp_percap * population / 1000000000000) %>%
select(year, country, GDP, region) %>% group_by(region, year) %>%
summarise(GDP = sum(GDP, na.rm = TRUE))
## `summarise()` has grouped output by 'region'. You can override using the
## `.groups` argument.
GDP2
## # A tibble: 175 × 3
## # Groups: region [7]
## region year GDP
## <chr> <dbl> <dbl>
## 1 East Asia & Pacific 1990 5.52
## 2 East Asia & Pacific 1991 6.03
## 3 East Asia & Pacific 1992 6.50
## 4 East Asia & Pacific 1993 7.04
## 5 East Asia & Pacific 1994 7.64
## 6 East Asia & Pacific 1995 8.29
## 7 East Asia & Pacific 1996 8.96
## 8 East Asia & Pacific 1997 9.55
## 9 East Asia & Pacific 1998 9.60
## 10 East Asia & Pacific 1999 10.1
## # … with 165 more rows
GDParea <- ggplot(GDP2, aes(x = year, y = GDP, area = region, fill = region)) +
ggtitle("GDP by World Bank Region") +
xlab("Year") +
ylab("GDP($trillions)") +
scale_fill_brewer(palette = "Set2") +
geom_area()
GDParea
In the mood to read more about how to make beautiful graphs in R Studio? Check out R Graphics Cookbook by Winston Chang! https://r-graphics.org/