── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.1 ✔ readr 2.2.0
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.3 ✔ tibble 3.3.1
✔ lubridate 1.9.5 ✔ tidyr 1.3.2
✔ purrr 1.2.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
nations_data <-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.
head(nations_data)
# 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>
Add GDP
For both charts, you will first need to create a new variable in the data, using mutate from dplyr, giving the GDP of each country in trillions of dollars, by multiplying gdp_percap by population and dividing by a trillion.
For the first chart, you will need to filter the data with dplyr for the four desired countries. When making the chart with ggplot2 you will need to add both geom_point and geom_line layers, and use the Set1 ColorBrewer palette using: scale_color_brewer(palette = “Set1”).
Warning: There was 1 warning in `filter()`.
ℹ In argument: `country == c("China", "Germany", "Japan", "United States")`.
Caused by warning in `country == c("China", "Germany", "Japan", "United States")`:
! longer object length is not a multiple of shorter object length
chart_1 <-ggplot(nations_1, aes(x = year, y = gdp, color = country)) +labs(x="year",y="GDP ($ trillion)",title="China's Rise to Become the Largest Economy") +theme_minimal(base_family ="mono") +geom_point() +geom_smooth(linewidth =0.3) +scale_color_brewer(palette ="Set1")chart_1
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Warning in sqrt(sum.squares/one.delta): NaNs produced
Warning in stats::qt(level/2 + 0.5, pred$df): NaNs produced
Warning in max(ids, na.rm = TRUE): no non-missing arguments to max; returning
-Inf
Warning in max(ids, na.rm = TRUE): no non-missing arguments to max; returning
-Inf
Warning in max(ids, na.rm = TRUE): no non-missing arguments to max; returning
-Inf
Chart 2
For the second chart, using dplyr you will need to group_by region and year, and then summarize on your mutated value for gdpusing
summarise(GDP = sum(gdp, na.rm = TRUE)). (There will be null values, or NAs, in this data, so you will need to use na.rm = TRUE).
Each region’s area will be generated by the command geom_area ()
When drawing the chart with ggplot2, you will need to use the Set2 ColorBrewer palette using scale_fill_brewer(palette = “Set2”)
regions <- nations_data |>group_by(year,region) |>summarize(gdp =sum(gdp, na.rm =TRUE))
`summarise()` has regrouped the output.
ℹ Summaries were computed grouped by year and region.
ℹ Output is grouped by year.
ℹ Use `summarise(.groups = "drop_last")` to silence this message.
ℹ Use `summarise(.by = c(year, region))` for per-operation grouping
(`?dplyr::dplyr_by`) instead.
chart_2 <-ggplot(regions, aes(x = year, y = gdp, fill = region)) +labs(x="year",y="GDP ($ trillion)",title="GDP by World Bank Region") +theme_minimal(base_family ="mono") +geom_area() +scale_fill_brewer(palette ="Set2")chart_2