Warning: package 'dplyr' was built under R version 4.3.2
nations <-read_csv('nations.csv')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>
# create new column gdp for GDP in trillionsnations <- nations %>%mutate(gdp = gdp_percap * population /10^12)
First chart
# selected countries are Cameroon, Cote d'Ivoire, Togo and Gabonnations2 <- nations |>filter(country %in%c("Cameroon", "Cote d'Ivoire", "Togo", "Gabon"))# Create the dot-and-line chartchart1 <-ggplot(nations2, aes(x = year, y = gdp, color = country)) +labs(title ="Cote d'Ivoire is almost always the highest",x ="Year",y ="GDP ($ trillion)",color =NULL# Remove legend title for color ) +theme_minimal(base_size =12) +scale_color_brewer(palette ="Set1") +geom_line() +geom_point() chart1
Second chart
#group data and summarizenations3 <- nations |>group_by(region, year) |>summarize(GDP =sum(gdp, na.rm =TRUE))
`summarise()` has grouped output by 'region'. You can override using the
`.groups` argument.
# Create the stacked area chartchart2 <-ggplot(nations3, aes(x = year, y = GDP, fill = region)) +geom_area(color ="white", size =0.2) +labs(title ="GDP by World Bank Region", x ="Year", y ="GDP ($ trillion)") +scale_fill_brewer(palette ="Set2") +theme_minimal()
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.