##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.
##When drawing both charts with ggplot2 you should also manually set the X axis tick mark labels (otherwise the default will be to include a label for 2015, for which there is no data.)
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(ggplot2)
## Registered S3 methods overwritten by 'ggplot2':
## method from
## [.quosures rlang
## c.quosures rlang
## print.quosures rlang
library(readr)
library(scales)
##
## Attaching package: 'scales'
## The following object is masked from 'package:readr':
##
## col_factor
##Chart 1
###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”).
gdp <- read.csv(file = "nations.csv")
gdp1 <- mutate(gdp, GDP = ((gdp_percap * population)/1000000000000))
gdp2 <- filter(gdp1, country == "China" | country == "Germany" | country == "Japan" | country == "United States")
ggplot (gdp2, aes(x = year, y = GDP, color = country)) +
ylab("GDP($ trillion)") +
theme_minimal(base_size = 12) +
ggtitle("China's Rise to Become the Largest Economy") +
geom_point() +
geom_line() +
scale_color_brewer(palette = 'Set1')
##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 gdp_percap using summarise(sum = sum(gdp_percap, 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))
ggplot(gdp3, aes(year, GDP)) +
xlab("year") + ylab("GDP($ trillion)") +
theme_minimal(base_size = 12) +
scale_fill_brewer(palette = 'Set2') +
ggtitle("GDP by Word Bank Region") +
geom_area(colour = "white", aes(fill = region))