Load primary packages

# Load required packages
library(tidyverse)
library(ggplot2)
library(wesanderson)

Streamgraphs

Streamgraphs is a type of chart that’s like a stacked area chart. Instead of plotting values against a conventional y axis, streamgraphs make the starting point of each section balanced in the middle of the chart(symmetrical around the x axis).

Advantages:

Uses:

# remotes::install_github("davidsjoberg/ggstream")
library(ggstream)

head(blockbusters)
ggplot(blockbusters, 
       aes(year, box_office, fill = genre)) +
  geom_stream() +
  scale_fill_manual(values = wes_palette("Darjeeling2")) +
  theme_minimal()

Ridgeline plots

Ridgeline plots, sometimes called joyplots, are like lines that overlap a bit and look like a range of mountains.

Advantages:

Uses:

# install.packages("ggridges")
library(ggridges)

head(blockbusters)
ggplot(blockbusters, 
       aes(x = box_office, y = genre, fill = genre)) +
  geom_density_ridges(scale = 4) +
  scale_fill_manual(values = wes_palette("Darjeeling2")) +
  theme_minimal()

Sankey diagrams

Sankey diagrams are like maps that show how things move from one place to another. They help us understand where things come from and where they go. These things can be stuff we use, like materials, or money we spend.

library(devtools)
# devtools::install_github("davidsjoberg/ggsankey")
library(ggsankey)

example_dat <-
  mtcars %>%
  make_long(cyl, vs, am, gear, carb) # function in ggsankey to format data correctly

head(example_dat)
ggplot(example_dat,
       aes(x = x, 
           next_x = next_x, 
           node = node, 
           next_node = next_node,
           fill = factor(node))) +
  geom_sankey(flow.alpha = .6) +
  theme_minimal()

Advantages:

Uses:

# install.packages("ggalluvial")
library(ggalluvial)

head(UCBAdmissions)
## , , Dept = A
## 
##           Gender
## Admit      Male Female
##   Admitted  512     89
##   Rejected  313     19
## 
## , , Dept = B
## 
##           Gender
## Admit      Male Female
##   Admitted  353     17
##   Rejected  207      8
## 
## , , Dept = C
## 
##           Gender
## Admit      Male Female
##   Admitted  120    202
##   Rejected  205    391
## 
## , , Dept = D
## 
##           Gender
## Admit      Male Female
##   Admitted  138    131
##   Rejected  279    244
## 
## , , Dept = E
## 
##           Gender
## Admit      Male Female
##   Admitted   53     94
##   Rejected  138    299
## 
## , , Dept = F
## 
##           Gender
## Admit      Male Female
##   Admitted   22     24
##   Rejected  351    317
ggplot(as.data.frame(UCBAdmissions),
       aes(y = Freq, axis1 = Gender, axis2 = Dept)) +
  geom_alluvium(aes(fill = Admit), width = 1/12) +
  scale_fill_manual(values = wes_palette("Darjeeling2")) +
  theme_minimal()

Bump charts

Bump Chart is a special form of a line plot. This chart is well-suited for exploring changes in rank over time.

Advantages:

Uses:

# devtools::install_github("davidsjoberg/ggbump")
library(ggbump)

blockbusters2 <-
  blockbusters %>% 
  filter(genre %in% c("Action", "Comedy", "Drama")) %>% 
  group_by(year) %>% 
  mutate(rank = rank(box_office))

head(blockbusters2)
ggplot(blockbusters2, 
       aes(year, rank, color = genre)) +
  geom_point(size = 7) +
  geom_bump() +
  scale_color_manual(values = wes_palette("Darjeeling2")) +
  theme_minimal()

Waffle charts

Waffle charts are similar to pie charts, but they use squares instead of slices. It’s like a big square made of 100 smaller squares in a 10-by-10 pattern. We color these squares to show different proportions, just like we color slices in a pie chart.

Advantages:

Uses:

# install.packages("waffle", repos = "https://cinc.rud.is")
# remotes::install_github("hrbrmstr/waffle")
library(ggplot2)
library(waffle)
expenses <- c(`Infants: <1(16467) `=16467, `Children: <11(30098) `=30098,
              `Teens: 12-17(20354)`=20354, `Adults:18+(12456) `=12456,
              `Elderly: 65+(12456) `=12456)
head(expenses)
##   Infants: <1(16467)  Children: <11(30098)    Teens: 12-17(20354) 
##                 16467                 30098                 20354 
##    Adults:18+(12456)   Elderly: 65+(12456)  
##                 12456                 12456
waffle(expenses/1000, rows=5, size=0.6, 
       colors=c("#44D2AC", "#E48B8B", "#B67093", 
                "#3A9ABD", "#CFE252"), 
       title="Age Groups bifurcation", 
       xlab="1 square = 1000 persons")

Beeswarm charts

Beeswarm plot is a type of scatter plot that is used for representing categorical values.

Advantages:

Uses:

# install.packages("ggbeeswarm")
library(ggbeeswarm)

head(blockbusters)
ggplot(blockbusters, 
       aes(x = genre, y = box_office, color = genre)) + 
  geom_quasirandom() +
  theme_minimal() +
  scale_color_manual(values = wes_palette("Darjeeling2")) +
  theme_minimal()

Mosaic charts

Mosaic plot is like a special stacked bar chart. It’s used to show how things are divided into groups, using percentages. This plot is a picture of a table that compares different things.

Advantages:

Uses:

# devtools::install_github("haleyjeppson/ggmosaic")
library(ggmosaic)

head(UCBAdmissions)
## , , Dept = A
## 
##           Gender
## Admit      Male Female
##   Admitted  512     89
##   Rejected  313     19
## 
## , , Dept = B
## 
##           Gender
## Admit      Male Female
##   Admitted  353     17
##   Rejected  207      8
## 
## , , Dept = C
## 
##           Gender
## Admit      Male Female
##   Admitted  120    202
##   Rejected  205    391
## 
## , , Dept = D
## 
##           Gender
## Admit      Male Female
##   Admitted  138    131
##   Rejected  279    244
## 
## , , Dept = E
## 
##           Gender
## Admit      Male Female
##   Admitted   53     94
##   Rejected  138    299
## 
## , , Dept = F
## 
##           Gender
## Admit      Male Female
##   Admitted   22     24
##   Rejected  351    317
ggplot(as.data.frame(UCBAdmissions)) +
  geom_mosaic(aes(x = product(Admit, Dept), fill = Gender, weight = Freq)) +
  scale_fill_manual(values = wes_palette("Darjeeling2")) +
  theme_minimal()