Distributions

The Hills are Shadows, said Tennyson

Arvind V.

2024-06-22

“Never let the future disturb you. You will meet it, if you have to, with the same weapons of reason which today arm you against the present.”

— Marcus Aurelius

Setting up R Packages

library(tidyverse)
library(mosaic)
library(ggformula)
library(skimr)
##
# install.packages("remotes")
# library(remotes)
# remotes::install_github("wilkelab/ggridges")
library(ggridges) # Ridge Density Plots
##
library(janitor) # Data cleaning and tidying package
library(visdat) # Visualize whole dataframes for missing data
library(naniar) # Clean missing data
library(DT) # Interactive Tables for our data
library(tinytable) # Elegant Tables for our data
library(ggrepel) # Repel overlapping text labels in ggplot2
library(marquee) # Annotations in ggplot2

Plot Fonts and Theme

Code
library(systemfonts)
library(showtext)
## Clean the slate
systemfonts::clear_local_fonts()
systemfonts::clear_registry()
##
showtext_opts(dpi = 96) # set DPI for showtext
sysfonts::font_add(
  family = "Alegreya",
  regular = "../../../../../../fonts/Alegreya-Regular.ttf",
  bold = "../../../../../../fonts/Alegreya-Bold.ttf",
  italic = "../../../../../../fonts/Alegreya-Italic.ttf",
  bolditalic = "../../../../../../fonts/Alegreya-BoldItalic.ttf"
)

sysfonts::font_add(
  family = "Roboto Condensed",
  regular = "../../../../../../fonts/RobotoCondensed-Regular.ttf",
  bold = "../../../../../../fonts/RobotoCondensed-Bold.ttf",
  italic = "../../../../../../fonts/RobotoCondensed-Italic.ttf",
  bolditalic = "../../../../../../fonts/RobotoCondensed-BoldItalic.ttf"
)
showtext_auto(enable = TRUE) # enable showtext
##
theme_custom <- function() {
  theme_bw(base_size = 10) +

    # theme(panel.widths = unit(11, "cm"),
    #       panel.heights = unit(6.79, "cm")) + # Golden Ratio

    theme(
      plot.margin = margin_auto(t = 1, r = 2, b = 1, l = 1, unit = "cm"),
      plot.background = element_rect(
        fill = "bisque",
        colour = "black",
        linewidth = 1
      )
    ) +

    theme_sub_axis(
      title = element_text(
        family = "Roboto Condensed",
        size = 10
      ),
      text = element_text(
        family = "Roboto Condensed",
        size = 8
      )
    ) +

    theme_sub_legend(
      text = element_text(
        family = "Roboto Condensed",
        size = 6
      ),
      title = element_text(
        family = "Alegreya",
        size = 8
      )
    ) +

    theme_sub_plot(
      title = element_text(
        family = "Alegreya",
        size = 14, face = "bold"
      ),
      title.position = "plot",
      subtitle = element_text(
        family = "Alegreya",
        size = 10
      ),
      caption = element_text(
        family = "Alegreya",
        size = 6
      ),
      caption.position = "plot"
    )
}

## Use available fonts in ggplot text geoms too!
ggplot2::update_geom_defaults(geom = "text", new = list(
  family = "Roboto Condensed",
  face = "plain",
  size = 3.5,
  color = "#2b2b2b"
))
ggplot2::update_geom_defaults(geom = "label", new = list(
  family = "Roboto Condensed",
  face = "plain",
  size = 3.5,
  color = "#2b2b2b"
))

ggplot2::update_geom_defaults(geom = "marquee", new = list(
  family = "Roboto Condensed",
  face = "plain",
  size = 3.5,
  color = "#2b2b2b"
))
ggplot2::update_geom_defaults(geom = "text_repel", new = list(
  family = "Roboto Condensed",
  face = "plain",
  size = 3.5,
  color = "#2b2b2b"
))
ggplot2::update_geom_defaults(geom = "label_repel", new = list(
  family = "Roboto Condensed",
  face = "plain",
  size = 3.5,
  color = "#2b2b2b"
))

## Set the theme
ggplot2::theme_set(new = theme_custom())

## tinytable options
options("tinytable_tt_digits" = 2)
options("tinytable_format_num_fmt" = "significant_cell")
options(tinytable_html_mathjax = TRUE)


## Set defaults for flextable
flextable::set_flextable_defaults(font.family = "Roboto Condensed")

What graphs will we see today?

Variable #1 Variable #2 Chart Names Chart Shape
Quant None Density plot, Ridge Density Plot

What kind of Data Variables will we choose?

No Pronoun Answer Variable/Scale Example What Operations?
1 How Many / Much / Heavy? Few? Seldom? Often? When? Quantities, with Scale and a Zero Value.Differences and Ratios /Products are meaningful. Quantitative/Ratio Length,Height,Temperature in Kelvin,Activity,Dose Amount,Reaction Rate,Flow Rate,Concentration,Pulse,Survival Rate Correlation

Inspiration

Code
ggplot2::theme_set(new = theme_custom())

lincoln_weather %>%
  gf_density_ridges_gradient(Month ~ `Max Temperature [F]`,
    group = ~Month
  ) %>%
  gf_refine(scale_fill_viridis_c(
    name = "Temperature [F]",
    option = "B"
  )) %>%
  gf_labs(title = "Weather in Lincoln, Nebraska")
Figure 1: Lincoln, Nebraska, in the Sun

April is the cruelest month, said T.S Eliot. But December in Nebraska must be tough.

What is a “Density Plot”?

As we saw earlier, Histograms are best to show the distribution of raw Quantitative data, by displaying the number of values that fall within defined ranges, often called buckets or bins.

Sometimes it is useful to consider a chart where the bucket width shrinks to zero!

You might imagine a density chart as a histogram where the buckets are infinitesimally small, i.e. zero width. Think of the frequency density as a differentiation (as in calculus) of the histogram. By taking the smallest of steps \(\sim 0\), we get a measure of the slope of distribution. This may seem counter-intuitive, but densities have their uses in spotting the ranges in the data where there are more frequent values. In this, they serve a similar purpose as do histograms, but may offer insights not readily apparent with histograms, especially with default bucket widths. The chunkiness that we see in the histograms is removed and gives us a smooth curve showing in which range the data are more frequent.

Case Study-1: penguins dataset

We will first look at at a dataset that is available as a part of the {palmerpenguins} package (and also directly available in R now), the penguins dataset. Data were collected and made available by Dr. Kristen Gorman and the Palmer Station, Antarctica LTER, a member of the Long Term Ecological Research Network.

Read the Data

data(penguins_raw, package = "datasets")
names(penguins_raw)
 [1] "studyName"           "Sample Number"       "Species"            
 [4] "Region"              "Island"              "Stage"              
 [7] "Individual ID"       "Clutch Completion"   "Date Egg"           
[10] "Culmen Length (mm)"  "Culmen Depth (mm)"   "Flipper Length (mm)"
[13] "Body Mass (g)"       "Sex"                 "Delta 15 N (o/oo)"  
[16] "Delta 13 C (o/oo)"   "Comments"           

Examine the Data

As per our Workflow, we will look at the data using all the three methods we have seen.

Data Munging

Among the variables that define the physical measurements of the penguins, there are a couple of entries that show missing data. Elsewhere there are more. The variable names also are human-readable, but not really computer-readable.

So let us follow through with our Data Munging Process:

penguins_clean <- penguins_raw %>%
  naniar::replace_with_na_all(condition = ~ .x %in% common_na_strings) %>% # replace common NA strings with actual NA
  naniar::replace_with_na_all(condition = ~ .x %in% common_na_numbers) %>%
  janitor::clean_names(case = "snake") %>% # clean names

  dplyr::mutate(across(where(is.character), as_factor)) %>% # make factors
  dplyr::relocate(where(is.factor)) # move factors to the right of rownames

glimpse(penguins_clean)
Rows: 344
Columns: 17
$ study_name        <fct> PAL0708, PAL0708, PAL0708, PAL0708, PAL0708, PAL0708…
$ species           <fct> Adelie Penguin (Pygoscelis adeliae), Adelie Penguin …
$ region            <fct> Anvers, Anvers, Anvers, Anvers, Anvers, Anvers, Anve…
$ island            <fct> Torgersen, Torgersen, Torgersen, Torgersen, Torgerse…
$ stage             <fct> "Adult, 1 Egg Stage", "Adult, 1 Egg Stage", "Adult, …
$ individual_id     <fct> N1A1, N1A2, N2A1, N2A2, N3A1, N3A2, N4A1, N4A2, N5A1…
$ clutch_completion <fct> Yes, Yes, Yes, Yes, Yes, Yes, No, No, Yes, Yes, Yes,…
$ sex               <fct> MALE, FEMALE, FEMALE, NA, FEMALE, MALE, FEMALE, MALE…
$ comments          <fct> Not enough blood for isotopes., NA, NA, Adult not sa…
$ sample_number     <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1…
$ date_egg          <date> 2007-11-11, 2007-11-11, 2007-11-16, 2007-11-16, 200…
$ culmen_length_mm  <dbl> 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1, …
$ culmen_depth_mm   <dbl> 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1, …
$ flipper_length_mm <dbl> 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 186…
$ body_mass_g       <dbl> 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475, …
$ delta_15_n_o_oo   <dbl> NA, 8.94956, 8.36821, NA, 8.76651, 8.66496, 9.18718,…
$ delta_13_c_o_oo   <dbl> NA, -24.69454, -25.33302, NA, -25.32426, -25.29805, …
Code
penguins_clean %>%
  DT::datatable(
    caption = htmltools::tags$caption(
      style = "caption-side: top; text-align: left; color: black; font-size: 150%;",
      "Penguins Dataset (Clean)"
    ),
    options = list(pageLength = 10, autoWidth = TRUE)
  ) %>%
  DT::formatStyle(
    columns = names(penguins_clean),
    fontFamily = "Roboto Condensed",
    fontSize = "12px"
  )
Table 1: Penguins Clean Dynamic Data Table

Data Dictionary

We will restrict ourselves to some of the variables that pertain, by and large, to the body dimensions of our penguins:

Qualitative Data

  • sex: male and female penguins
  • species: Three adorable types!
  • island: they have islands to themselves!!
  • region: Antarctica, duh!! Hmmm….
Figure 2: Penguin Species

Quantitative Data

  • bill_length_mm: The length of the penguins’ bills
  • bill_depth_mm: See the picture!!
  • flipper_length_mm: Flippers! Penguins have “hands”!!
  • body_mass_g: Mass in grams. Grams? Grams??? Why, these penguins are like human babies!!❤️
  • culmen_depth_mm: Depth of the culmen (the upper ridge of the bill)
  • culmen_length_mm: Length of the culmen
Figure 3: Penguin Features

Business Insights on Examining the penguins dataset

  • This is a smallish dataset (344 rows, 8 columns).
  • They define the various physical dimensions of the penguins, along with other variables pertaining to their study.

Plotting Densities

Business Insights from penguin Densities

Pretty much similar conclusions as with histograms. Although densities may not be used much in business contexts, they are better than histograms when comparing multiple distributions! So you should use them!

Ridge Plots

Sometimes we may wish to show the distribution/density of a Quant variable, against several levels of a Qual variable. For instance, the prices of different items of furniture, based on the furniture “style” variable. Or the sales of a particular line of products, across different shops or cities. We did this with both histograms and densities, by colouring based on a Qual variable, and by facetting using a Qual variable. There is a third way, using what is called a ridge plot. {ggformula} supports this plot by importing/depending upon the {ggridges} package. {ggridges} provides direct support for ridge plots, and can be used as an extension to {ggplot2} and {ggformula}.

Business Insights from mpg Ridge Plots

This is another way of visualizing multiple distributions, of a Quant variable at different levels of a Qual variable. We see that the distribution of hwy mileage varies substantially with drv type.

Wait, But Why?

  • Densities are sometimes easier to compare side by side. That is what Claus Wilke says, at least. Perhaps because they look less “busy” than histograms.
  • Ridge Density Plots are very cool when it comes to comparing the density of a Quant variable as it varies against the levels of a Qual variable, without having to facet or group.
  • It is possible to plot 2D-densities too, for two Quant variables, which give very evocative contour-like plots. Try to do this with the faithful dataset in R.

Conclusion

  • Histograms and Frequency Distributions are both used for Quantitative data variables
  • Whereas Histograms “dwell upon” counts, ranges, means and standard deviations
  • Frequency Density plots “dwell upon” probabilities and densities
  • Ridge Plots are density plots used for describing one Quant and one Qual variable (by inherent splitting)
  • We can split all these plots on the basis of another Qualitative variable.(Ridge Plots are already split)
  • Long tailed distributions need care in visualization and in inference making!

Your Turn

Star Trek Books

Which would be the Group By variables here? And what would you summarize? With which function?

Math Anxiety! Hah! Peasants.

References

  1. Winston Chang (2024). R Graphics Cookbook. https://r-graphics.org
  2. See the scrolly animation for a histogram at this website: Exploring Histograms, an essay by Aran Lunzer and Amelia McNamara
  3. Minimal R using mosaic. https://cran.r-project.org/web/packages/mosaic/vignettes/MinimalRgg.pdf
  4. Sebastian Sauer, Plotting multiple plots using purrr::map and ggplot
R Package Citations
Package Version Citation
ggridges 0.5.7 Wilke (2025)
NHANES 2.1.0 Pruim (2015)
resampledata3 1.0 Chihara and Hesterberg (2022)
rtrek 0.5.2 Leonawicz (2025)
TeachHist 0.2.1 Lange (2023)
TeachingDemos 2.13 Snow (2024)
tidyplots 0.3.1 Engler (2025)
tinyplot 0.5.0 McDermott, Arel-Bundock, and Zeileis (2025)
tinytable 0.13.0 Arel-Bundock (2025)
visualize 4.5.0 Balamuta (2023)
Arel-Bundock, Vincent. 2025. tinytable: Simple and Configurable Tables in HTML,” LaTeX,” Markdown,” Word,” PNG,” PDF,” and Typst Formats. https://doi.org/10.32614/CRAN.package.tinytable.
Balamuta, James. 2023. visualize: Graph Probability Distributions with User Supplied Parameters and Statistics. https://doi.org/10.32614/CRAN.package.visualize.
Chihara, Laura, and Tim Hesterberg. 2022. Resampledata3: Data Sets for Mathematical Statistics with Resampling and R (3rd Ed). https://doi.org/10.32614/CRAN.package.resampledata3.
Engler, Jan Broder. 2025. “Tidyplots Empowers Life Scientists with Easy Code-Based Data Visualization.” iMeta, e70018. https://doi.org/10.1002/imt2.70018.
Lange, Carsten. 2023. TeachHist: A Collection of Amended Histograms Designed for Teaching Statistics. https://doi.org/10.32614/CRAN.package.TeachHist.
Leonawicz, Matthew. 2025. rtrek: Data Analysis Relating to Star Trek. https://doi.org/10.32614/CRAN.package.rtrek.
McDermott, Grant, Vincent Arel-Bundock, and Achim Zeileis. 2025. tinyplot: Lightweight Extension of the Base r Graphics System. https://doi.org/10.32614/CRAN.package.tinyplot.
Pruim, Randall. 2015. NHANES: Data from the US National Health and Nutrition Examination Study. https://doi.org/10.32614/CRAN.package.NHANES.
Snow, Greg. 2024. TeachingDemos: Demonstrations for Teaching and Learning. https://doi.org/10.32614/CRAN.package.TeachingDemos.
Wilke, Claus O. 2025. ggridges: Ridgeline Plots in ggplot2. https://doi.org/10.32614/CRAN.package.ggridges.