── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.0 ✔ readr 2.2.0
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.2 ✔ tibble 3.3.1
✔ lubridate 1.9.5 ✔ tidyr 1.3.2
✔ purrr 1.2.1
── 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
library(knitr)# Set working directorysetwd("/Users/rahwahagos/Downloads/Hate_Crimes")# Load the nations datasetnations <-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.
nations <- nations |>mutate(gdp_trillion = (gdp_percap * population) /1e12)# Check the new variablesummary(nations$gdp_trillion)
Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
0.00001 0.00774 0.03244 0.32594 0.18490 18.08291 766
Warning: There were 21 warnings in `summarize()`.
The first warning was:
ℹ In argument: `max_gdp = max(gdp_trillion, na.rm = TRUE)`.
ℹ In group 4: `country = "American Samoa"`.
Caused by warning in `max()`:
! no non-missing arguments to max; returning -Inf
ℹ Run `dplyr::last_dplyr_warnings()` to see the 20 remaining warnings.
# A tibble: 10 × 2
country max_gdp
<chr> <dbl>
1 China 18.1
2 United States 17.3
3 India 7.35
4 Japan 4.66
5 Germany 3.76
6 Russian Federation 3.63
7 Brazil 3.29
8 Indonesia 2.69
9 France 2.60
10 United Kingdom 2.60
chart1 <- nations |>filter(country %in%c("China", "United States", "India", "Japan")) |>ggplot(aes(x = year, y = gdp_trillion, color = country)) +geom_line(size =1.2) +geom_point(size =2, alpha =0.7) +scale_color_brewer(palette ="Set1") +labs(title ="China's Rise to Become the Largest Economy",subtitle ="GDP in trillions of US dollars (constant)",x ="Year",y ="GDP (trillion $)",color ="Country",caption ="Data source: Nations dataset (gapminder)" ) +theme_minimal() +theme(legend.position ="bottom",plot.title =element_text(hjust =0.5),plot.subtitle =element_text(hjust =0.5) )
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
print(chart1)
chart2 <- nations |>group_by(region, year) |>summarize(gdp_trillion =sum(gdp_trillion, na.rm =TRUE), .groups ="drop") |>ggplot(aes(x = year, y = gdp_trillion, fill = region)) +geom_area(alpha =0.8, color ="white", size =0.2) +scale_fill_brewer(palette ="Set2") +labs(title ="GDP by World Bank Region",subtitle ="GDP in trillions of US dollars (constant)",x ="Year",y ="GDP (trillion $)",fill ="Region",caption ="Data source: Nations dataset (gapminder)" ) +theme_minimal() +theme(legend.position ="right",plot.title =element_text(hjust =0.5),plot.subtitle =element_text(hjust =0.5) )print(chart2)
Chart 1: Major Economies
This line chart with points shows the GDP growth of the four largest economies over time. China's remarkable rise is evident, surpassing the United States to become the world's largest economy. India also shows strong growth, particularly in recent decades.
Chart 2: Regional GDP
The area chart displays total GDP by World Bank region over time. North America and Europe & Central Asia have historically dominated global GDP, while East Asia & Pacific has shown dramatic growth, particularly since the 1990s, reflecting China's economic expansion. Sub-Saharan Africa and South Asia remain smaller economies, though they show consistent growth.
Quarto
Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.
Running Code
When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:
1+1
[1] 2
You can add options to executable code like this
[1] 4
The echo: false option disables the printing of code (only output is displayed).