Load in the Libraries

library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(readr)
library(scales)
## 
## Attaching package: 'scales'
## The following object is masked from 'package:readr':
## 
##     col_factor

Load in the Dataset

gdp <- read.csv(file = "nations.csv")

Create New Variable

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.

gdp1 <- mutate(gdp, GDP = ((gdp_percap * population)/1000000000000))

Chart One

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”).

gdp2 <- filter(gdp1, country == "Brazil" | country == "Mexico" | country == "China" | country == "United States")
ggplot (gdp2, aes(x = year, y = GDP, color = country)) +
  ylab("GDP($ trillion)") +
  theme_minimal(base_size = 12) +
  ggtitle("National Economic Performance as Reflected in Per Capita GDP for Four Countries") +
  geom_point() +
  geom_line() +
  scale_color_brewer(palette = 'Set1')

Chart Two

For the second chart, using dplyr you will need to group_by region and year, and then summarize on your mutated value for gdp using 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”)

Think about the difference between fill and color when making the chart, and where the above fill command needs to go in order for the regions to fill with the different colors when making the chart, and put a very thin white line around each area.

gdp3 <- gdp1 %>% group_by(region, year) %>% summarise(GDP = sum(GDP, na.rm = TRUE))
## `summarise()` has grouped output by 'region'. You can override using the `.groups` argument.
ggplot(gdp3, aes(year, GDP)) +
         xlab("year") + ylab("GDP($ trillion)") +
         theme_minimal(base_size = 12) +
         scale_fill_brewer(palette = 'Set2') +
         ggtitle("GDP by Region") +
         geom_area(colour = "white", aes(fill = region))