Rain cloud plot

Author
Affiliation

P K Parida

CRFM and ICAR

Published

September 8, 2024

1 Rain cloud plot

rain cloud plot is generally visualize a the distribution of data in three different format in a single plot to make it beautiful and interesting. distribution plot, box plot and dot plot are combined together to make rain cloud plot. As in th box plot the distribution of data is not very clear,where as the distribution is quite more clear in the rain cloud plot.

2 Setting the theme for the plot

We should have ggdist package for for the distribution map . if it is not there, we can install it by install.packages("ggdist") .

library(tidyverse)
Warning: package 'ggplot2' was built under R version 4.3.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggdist)
Warning: package 'ggdist' was built under R version 4.3.3
theme_set(
  theme_minimal(
    base_size = 12,
    base_family = 'Source Sans Pro'
  ) +
    theme(panel.grid.minor = element_blank())
) 

We will work on Penguin dataset

Setting the colour for three species of penguins

colors <- thematic::okabe_ito(3)
names(colors) <- unique(palmerpenguins::penguins$species)
title_text <- glue::glue(
  'Penguin weights (in g) for the Species',  
  '<span style = "color:{colors["Adelie"]}">**Adelie**</span>,', 
  '<span style = "color:{colors["Chinstrap"]}">**Chinstrap**</span>', 
  'and',
  '<span style = "color:{colors["Gentoo"]}">**Gentoo**</span>', 
  .sep = ' '
)

Code for making the plot

palmerpenguins::penguins |> 
  filter(!is.na(sex)) |> 
  ggplot(aes(x = body_mass_g, fill = species, y = species)) +
  geom_boxplot(width = 0.1) +
  geom_dots(
    side = 'bottom', 
    height = 0.55,
    position = position_nudge(y = -0.075)
  ) +
  stat_slab(
    position = position_nudge(y = 0.075), 
    height = 0.75
  ) +
  scale_y_discrete(
    breaks = c('Adelie', 'Gentoo', 'Chinstrap')
  ) +
  scale_fill_manual(values = colors) +
  labs(
    x = element_blank(), 
    y = element_blank(), 
    title = title_text
  ) +
  theme(
    plot.title = ggtext::element_markdown(),
    legend.position = 'none'
  )

Changing the geom from geom_dot to geom_point and providing a shape of | for distribution of the data

palmerpenguins::penguins |> 
  filter(!is.na(sex)) |> 
  ggplot(aes(x = body_mass_g, fill = species, y = species)) +
  geom_boxplot(width = 0.1) +
  geom_point(
    aes(color = species),
    shape = "|",
    size = 10,
    alpha = 0.5,
    position = position_nudge(y = -0.175)
  ) +
  stat_slab(
    position = position_nudge(y = 0.075), 
    height = 0.75
  )

Now we can change the colour and remove the legend from the above figure

palmerpenguins::penguins |> 
  filter(!is.na(sex)) |> 
  ggplot(aes(x = body_mass_g, fill = species, y = species)) +
  geom_boxplot(width = 0.1) +
  geom_point(
    aes(color = species),
    shape = "|",
    size = 10,
    alpha = 0.5,
    position = position_nudge(y = -0.215)
  ) +
  stat_slab(
    position = position_nudge(y = 0.075), 
    height = 0.75
  ) + 
  scale_y_discrete(
    breaks = c('Adelie', 'Gentoo', 'Chinstrap')
  ) +
  scale_fill_manual(values = colors) +
  labs(
    x = element_blank(), 
    y = element_blank(), 
    title = title_text
  ) +
  theme(
    plot.title = ggtext::element_markdown(),
    legend.position = 'none'
  )

Not getting the same result with the geom_dot

palmerpenguins::penguins |> 
  filter(!is.na(sex)) |> 
  ggplot(aes(x = body_mass_g, fill = species, y = species)) +
  geom_boxplot(width = 0.1) +
  geom_dots(
    side = 'bottom', 
    position = position_nudge(y = -0.075),
    height = 0.55
  ) +
  stat_slab(
    position = position_nudge(y = 0.075), 
    height = 0.75
  ) 

Now we can change the colour and remove the legend

palmerpenguins::penguins |> 
  filter(!is.na(sex)) |> 
  ggplot(aes(x = body_mass_g, fill = species, y = species)) +
  geom_boxplot(width = 0.1) +
  geom_dots(
    side = 'bottom', 
    position = position_nudge(y = -0.075),
    height = 0.55
  ) +
  stat_slab(
    position = position_nudge(y = 0.075), 
    height = 0.75
  ) + 
  scale_y_discrete(
    breaks = c('Adelie', 'Gentoo', 'Chinstrap')
  ) +
  scale_fill_manual(values = colors) +
  labs(
    x = element_blank(), 
    y = element_blank(), 
    title = title_text
  ) +
  theme(
    plot.title = ggtext::element_markdown(),
    legend.position = 'none'
  )