Posts

Alluvial plot

Jan 1, 0001

library(tidyverse)
library(ggalluvial)

states_all
# A tibble: 13,168,874 x 5
#    bin            `SD 3m` `SD 6m` `SD 12m` `CR 12m`
#    <chr>            <dbl>   <dbl>    <dbl>    <dbl>
#  1 chr1:0-200           8       8        8        8
#  2 chr1:200-400         8       8        8        8
#  3 chr1:400-600         8       8        8        8
#  4 chr1:600-800         8       8        8        8
#  5 chr1:800-1000        8       8        8        8
#  6 chr1:1000-1200       8       8        8        8
#  7 chr1:1200-1400       8       8        8        8
#  8 chr1:1400-1600       8       8        8        8
#  9 chr1:1600-1800       8       8        8        8
# 10 chr1:1800-2000       8       8        8        8
# … with 13,168,864 more rows

state_transition_lodes <- states_all %>%
  group_by(`SD 3m`, `SD 12m`, `CR 12m`) %>%
  tally() %>%
  ungroup() %>%
  mutate(
    `SD 3m` = factor(`SD 3m`, levels = c('1','2','3','4','5','6','7','8')),
    `SD 12m` = factor(`SD 12m`, levels = c('1','2','3','4','5','6','7','8')),
    `CR 12m` = factor(`CR 12m`, levels = c('1','2','3','4','5','6','7','8')),
    alpha = ifelse((`SD 3m` == `SD 12m` & `SD 3m` == `CR 12m`), 0, 0.5)
  ) %>%
  to_lodes_form(key = 'Demographic', axes = 1:3)

state_transition_lodes
# A tibble: 1,290 x 5
#        n alpha alluvium Demographic stratum
#    <int> <dbl>    <int> <fct>       <fct>
#  1 25324   0          1 SD 3m       1
#  2  2792   0.5        2 SD 3m       1
#  3  1482   0.5        3 SD 3m       1
#  4  3040   0.5        4 SD 3m       1
#  5   187   0.5        5 SD 3m       1
#  6    74   0.5        6 SD 3m       1
#  7   254   0.5        7 SD 3m       1
#  8   237   0.5        8 SD 3m       1
#  9   872   0.5        9 SD 3m       1
# 10  1053   0.5       10 SD 3m       1
# … with 1,280 more rows

fig_4C <-
  state_transition_lodes %>%
  ggplot(aes(x = Demographic, stratum = stratum, alluvium = alluvium, y = n,
    label = stratum)) +
  scale_x_discrete(labels = c('SD 3m', 'SD 12m', 'CR 12m'), position = 'top') +
  geom_alluvium(aes(fill = stratum, alpha = alpha), width = 1/3) +
  geom_stratum(aes(fill = stratum, alpha = 0.75), width = 1/3) +
  geom_label(stat = 'stratum') +
  scale_fill_brewer(palette = 'Dark2', direction = 1) +
  theme_bw() +
  labs(x = '', y = '', title = 'C') +
  theme_bw() +
  theme(
    plot.title = element_text(face = 'bold', size = 20),
    legend.position = 'none',
    axis.title = element_blank(),
    axis.text.x = element_text(face = 'bold', colour = 'black', size = 15),
    axis.text.y = element_blank(),
    axis.ticks.x = element_blank(),
    axis.ticks.y = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank()
  )
ggsave('figure_4/C.pdf', fig_4C)











p <- ggplot(
    data = titanic_wide,
    aes(
      axis1 = Class,
      axis2 = Sex,
      axis3 = Age,
      y = Freq
    )
  ) +
  scale_x_discrete(limits = c('Class', 'Sex', 'Age'), expand = c(0.1, 0), position = 'top') +
  geom_alluvium(aes(fill = Survived)) +
  geom_stratum() +
  scale_fill_jcolors(palette = 'default') +
  geom_text(stat = 'stratum', label.strata = TRUE) +
  theme_bw() +
  theme(
    plot.title = element_text(face = 'bold', size = 20),
    legend.position = 'none',
    axis.title = element_blank(),
    axis.text.x = element_text(face = 'bold', colour = 'black', size = 15),
    axis.text.y = element_blank(),
    axis.ticks.x = element_blank(),
    axis.ticks.y = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank()
  )

Alluvial plot

Jan 1, 0001

Context and code for this plot can be found in my scRNA-seq workflow in the chapter “Cell type annotation with SingleR”.

Distribution plot 1

Jan 1, 0001

library(tidyverse)
library(wesanderson)

# https://github.com/allisonhorst/palmerpenguins
library(palmerpenguins)

p <- ggplot(penguins, aes(species, flipper_length_mm)) +
  geom_jitter(aes(color = species), alpha = 0.75, size = 3, show.legend = FALSE) +
  geom_violin(aes(fill = species), alpha = 0.25, show.legend = FALSE) +
  geom_boxplot(outlier.size = 0, width = 0.2, alpha = 0.75, show.legend = FALSE) +
  scale_color_manual(values = wes_palette('BottleRocket2')) +
  scale_fill_manual(values = wes_palette('BottleRocket2')) +
  scale_y_continuous(labels = scales::comma) +
  theme_bw() +
  labs(y = 'Flipper length [mm]') +
  theme(
    axis.title.x = element_blank(),
    panel.grid.major.x = element_blank()
  )

ggsave('1.png', p, height = 5, width = 6)

Distribution plot 2

Jan 1, 0001

library(tidyverse)
library(gghalves)
library(wesanderson)

# https://github.com/allisonhorst/palmerpenguins
library(palmerpenguins)

p <- ggplot(penguins, aes(species, body_mass_g)) +
  geom_jitter(aes(color = species), alpha = 0.75, size = 1.5, show.legend = FALSE) +
  geom_half_violin(aes(fill = species), side = 'l', alpha = 0.25, show.legend = FALSE) +
  geom_half_boxplot(aes(fill = species), side = 'r', outlier.size = 0, width = 0.5, alpha = 0.75, show.legend = FALSE) +
  scale_color_manual(values = wes_palette('Darjeeling1', 3, type = 'discrete')) +
  scale_fill_manual(values = wes_palette('Darjeeling1', 3, type = 'discrete')) +
  scale_y_continuous(labels = scales::comma) +
  theme_bw() +
  labs(y = 'Body mass [g]') +
  theme(
    axis.title.x = element_blank(),
    panel.grid.major.x = element_blank()
  )

ggsave('2.png', p, height = 5, width = 6)

Distribution plot 3

Jan 1, 0001

library(tidyverse)
library(gghalves)
library(ggbeeswarm)
library(wesanderson)

# https://github.com/allisonhorst/palmerpenguins
library(palmerpenguins)

p <- ggplot(penguins, aes(species, culmen_length_mm, color = species)) +
  geom_beeswarm(priority = 'none', size = 1.5, beeswarmArgs = list(side = -1)) +
  geom_half_boxplot(aes(fill = species), side = 'r', color = 'black', outlier.size = 0, outlier.color = NA, width = 0.5, alpha = 0.75) +
  scale_color_manual(name = 'Species', values = wes_palette('Darjeeling1', 3, type = 'discrete')) +
  scale_fill_manual(values = wes_palette('Darjeeling1', 3, type = 'discrete')) +
  scale_y_continuous(labels = scales::comma) +
  theme_bw() +
  labs(y = 'Culmen length [mm]') +
  theme(
    legend.position = 'none',
    axis.title.x = element_blank(),
    panel.grid.major.x = element_blank()
  )
  
ggsave('3.png', p, height = 5, width = 6)

Distribution plot 3

Jan 1, 0001

library(tidyverse)
library(gghalves)
library(ggbeeswarm)
library(wesanderson)

# https://github.com/allisonhorst/palmerpenguins
library(palmerpenguins)

p <- ggplot(penguins, aes(species, culmen_depth_mm, fill = species)) +
  geom_violin(draw_quantiles = c(0.25, 0.75), scale = 'count', trim = FALSE, linetype = 'dashed') +
  geom_violin(draw_quantiles = 0.5, scale = 'count', trim = FALSE, fill = 'transparent') +
  stat_summary(fun = mean, geom = 'point', size = 5, color = 'black') +
  scale_color_manual(name = 'Species', values = wes_palette('Darjeeling1', 3, type = 'discrete')) +
  scale_fill_manual(values = wes_palette('Darjeeling1', 3, type = 'discrete')) +
  scale_y_continuous(labels = scales::comma) +
  theme_bw() +
  labs(y = 'Culmen depth [mm]') +
  theme(
    legend.position = 'none',
    axis.title.x = element_blank(),
    panel.grid.major.x = element_blank()
  )
  
ggsave('4.png', p, height = 5, width = 6)