Create two charts as shown from the Nations dataset
#devtools::install_github("hrbrmstr/streamgraph")
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.0 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.1 ✔ tibble 3.1.8
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
library(ggplot2)
library(dplyr)
library(devtools)
## Loading required package: usethis
library(RColorBrewer)
library(streamgraph)
library(alluvial)
library(plotly)
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
library(ggalluvial)
Set working directory and load the dataset
setwd("C:/Users/jakea/OneDrive/Desktop/Database Data_110")
nations <- read.csv("nations.csv")
Create a new variable the GDP of each country in trillons of dollars
using mutate by multiplying gdp_percap by population and dividing by a
trillion.
nations_gdp <- nations %>%
mutate(gdp = (population * gdp_percap) / 1000000000000)
Filter nations_gdp for desired countries and preview
chart1_gdp <- nations_gdp %>%
filter(country %in% c("China","Germany","Japan", "United States"))
Create dotted line chart outline
chart1 <- ggplot(chart1_gdp, aes(x = year, y = gdp)) +
labs(title = "China's Rise to Become the Largest Economy", fill = "") + # Removes the title from legend
xlab("Year") +
ylab("GDP ($ trillion)")
chart1

Add both geom_point and geom_line layer and use Set1 ColorBrewer
plattee
chart1 <- ggplot(chart1_gdp, aes(x = year, y = gdp, fill = country)) +
scale_color_brewer(palette = "Set1") +
theme_bw() +
geom_line(aes(color = country), linewidth = .75) +
geom_point(aes(color = country)) +
labs(title = "China's Rise to Become the Largest Economy") +
xlab("Year") +
ylab("GDP ($ trillion)") +
guides(fill = guide_legend(title = NULL),
color = guide_legend(title = NULL))
chart1

Add interactivity with ggplotly
chart1 <- ggplotly(chart1)
chart1
Group_by region and year, and then summarize on your mutated value
for gdp
chart2_gdp <- nations_gdp %>%
group_by(region, year) %>%
summarize(gdp = sum(gdp, na.rm = TRUE))
## `summarise()` has grouped output by 'region'. You can override using the
## `.groups` argument.
head(chart2_gdp)
## # A tibble: 6 × 3
## # Groups: region [1]
## region year gdp
## <chr> <int> <dbl>
## 1 East Asia & Pacific 1990 5.52
## 2 East Asia & Pacific 1991 6.03
## 3 East Asia & Pacific 1992 6.50
## 4 East Asia & Pacific 1993 7.04
## 5 East Asia & Pacific 1994 7.64
## 6 East Asia & Pacific 1995 8.29
Create Ribbions and Area plot outline for GDP by World Bank
Region
chart2 <- ggplot(chart2_gdp, aes(x = year, y = gdp, fill = region)) +
labs(title = "GDP by World Bank Region") +
xlab("Year") +
ylab("GDP ($ trillion)")
chart2

Add ineractivity
chart2 <- ggplotly(chart2)
chart2