library(dplyr)
library (ggplot2)Nations Chart
Loading Libraries
Importing the data set
nations <- read.csv("C:/Users/thilo/OneDrive/Desktop/DATA 110/nations.csv")
head(nations) iso2c iso3c country year gdp_percap population birth_rate neonat_mortal_rate
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.0
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
region income
1 Europe & Central Asia High income
2 Europe & Central Asia High income
3 Europe & Central Asia High income
4 Europe & Central Asia High income
5 Europe & Central Asia High income
6 Europe & Central Asia High income
Creating a new variable, gdp of each countries in trillions of dollars
nations <- nations |>
mutate(gdp = (gdp_percap * population)/ 10^12)Filtering the countries
chart1 <- nations |>
filter(country %in% c("India","Sri Lanka","Nepal","Pakistan"))Plotting the chart 1
ggplot(chart1, aes(x= year, y = gdp, color = country))+
geom_point()+
geom_line()+
scale_color_brewer(palette = "Set1")+
labs(title = "GDP Growth in South Asian Countries", x= "Year", y= "GDP($trillion)",color = "country")+
theme_light() ##Because I do not like how it looks like with theme_minimal, it looks like empty so I chose theme_light to add some gray linesGrouping and summarize for chart 2
chart2<- 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.
Plotting the chart 2
ggplot(chart2, aes(x=year,y=GDP, fill = region))+
geom_area(color = "white", linewidth = 0.2)+ ##to add a thin white line, I used 0.2 linewidth and the color.
scale_fill_brewer(palette = "Set2")+
labs(title = "GDP by World Bank Region", x= "Year", y = "GDP($ trillion)", fill = "region")+
theme_minimal() ##For this one I think it's better with theme_minimal instead of theme_line.Citations
Peng, Roger D. Exploratory Data Analysis with R. Leanpub, 2020.
Even though the book says lwd to make the layers I used linewidth because I googled to check if the both same and yes, lwd means linewidth.