library(ggplot2)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ tibble 3.0.6 ✓ dplyr 1.0.4
## ✓ tidyr 1.1.3 ✓ stringr 1.4.0
## ✓ readr 1.4.0 ✓ forcats 0.5.1
## ✓ purrr 0.3.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
setwd("~/Desktop/Pankti _ Data Science")
nations <-read_csv("~/Desktop/Pankti _ Data Science/nations.csv")
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## iso2c = col_character(),
## iso3c = col_character(),
## country = col_character(),
## year = col_double(),
## gdp_percap = col_double(),
## population = col_double(),
## birth_rate = col_double(),
## neonat_mortal_rate = col_double(),
## region = col_character(),
## income = col_character()
## )
Creating new field = GDP and filtering the country for Chart 1
nations_new<-nations %>%
mutate(GDP = gdp_percap * population/10^12)%>%
filter( country == "India" | country == "China" | country == "Germany" | country == "United States")
Chart1 - GDP by countries
Chart1 <- ggplot(nations_new,aes(x=year, y=GDP,group = country))+
labs(title = "GDP by countries")+
xlab("Year")+
ylab("GDP ($ trillion)")+
scale_color_brewer(palette = "Set1")
Chart1

Chart1 - GDP by countries with geom_line and geom_point graph
Chart1+
geom_line(aes(color = country))+
geom_point(aes(color = country))

Creating new field = GDP, grouping by year and region,summarizing GDP and removing NA for Chart 2
nations_new1<-nations %>%
mutate(GDP = gdp_percap * population/10^12)%>%
group_by(region, year)%>%
summarise(GDP = sum(GDP, na.rm = TRUE))
## `summarise()` has grouped output by 'region'. You can override using the `.groups` argument.
Chart2 - GDP by region
Chart2 <- ggplot(nations_new1,aes(x=year, y=GDP,fill= region))+
labs(title = "GDP by region")+
xlab("Year")+
ylab("GDP ($ trillion)")+
scale_color_brewer(palette = "Set2")
Chart2

Chart2 - GDP by region using geom_area()
Chart2 +
geom_area(alpha=0.6 , size=0.3, colour="white")
