Density plots

Metabolites and lipids descriptive statistical analysis in R

The density plot was already presented in the previous subchapter. Density plots are used to depict the distribution of a numeric variable. Every density plot can be viewed as a derivative of a histogram. Therefore, its applications are similar to those of a histogram. Density plots can be an effective way of visualizing concentration distributions of lipids or metabolites in biological materials. Check out the selected example published in Nature:

  • T. Takeuchi et al. Gut microbial carbohydrate metabolism contributes to insulin resistance. DOI: https://doi.org/10.1038/s41586-023-06466-x - Fig. 1d & 2b, f (the authors of a manuscript published in Nature used density plots, e.g., for presenting and comparing fecal levels of monosaccharides across experimental groups (as decimal logarithm), or HOMA-IR, BMI, triglycerides (TG) and HDL-C levels among the participant clusters).

Preparing density plots via DataExplorer (level: basic)

DataExplorer enables producing a simple density plot through plot_density():

# Calling library:
library(DataExplorer)

# Reading about plot_density:
?plot_density()

# Preparing plot - PLOT A
data %>%
  filter(Label == 'N') %>%
  select(`SM 41:1;O2`) %>%
  plot_density(theme_config = 
                 list(panel.background = element_blank(),
                      axis.line.x = element_line(color = 'black'),
                      axis.line.y = element_line(color = 'black')),
               geom_density_args = list(color = 'royalblue', linewidth = 1)
               ) 
               
# Preparing plot - PLOT B
data %>%
  filter(Label == 'T') %>%
  select(`SM 41:1;O2`) %>%
  plot_density(theme_config = 
                 list(panel.background = element_blank(),
                      axis.line.x = element_line(color = 'black'),
                      axis.line.y = element_line(color = 'black')),
               geom_density_args = list(color = 'red2', linewidth = 1)
               )

Basic plot customization was performed using theme_config argument. We delivered a list with parameters to be changed, e.g., we removed the classic ggplot2 gray background using panel.backgroud and setting it element_blank():

Then, we changed the x-y axes color to black through axis.line.x and axis.line.y:

Finally, we changed the colors of the density plot curves to 'royalblue or 'red2', and linewidth to 1, using geom_density_args and a list containing information about a color and linewidth:

We obtain the following plots:

Density plots obtained through plot_density() from the DataExplorer.

Such simple plots can be used for a quick data inspection.

Preparing density plots via ggpubr (level: intermediate)

The ggpubr package contains function ggdensity(). We will show you how to prepare a density plot for a single lipid and multiple lipids:

The plots:

Density plots prepared using ggdensity() function from the ggpubr package.

Preparing density plots via ggplot2 (level: advanced)

As you know from the previous example with histograms, we can add a layer with a density plot using geom_density(). Take a look at the examples below - for a single lipid and multiple lipids. To customize the plot a bit more, we added mean values to them:

The output:

Density plots obtained using ggplot2 library.

It is possible to plot one density graph up, while the other down. Such a chart is known as a mirror density chart. This plot was inspired by an excellent source of ideas for beautiful R charts - R graph gallery:

R graph gallery - mirror density plots

The final plot:

Mirror density plot inspired by R graph gallery.

We can modify the code to obtain mirror density plots for multiple lipids at once:

We obtain the following plot:

Multiple mirror density plots for multiple SM species.

We can also add annotations to each plot. Annotations must be delivered through geom_text() as a data frame with labels and coordinates if multiple panels are created through facet_grid(). Here, we will use exemplary annotations: "Control - N" and "Patient - T" :

We obtain finally:

Multiple mirror density plots with annotations.

As annotations, one could add, for example, some additional clinical data, the exact value of mean and median concentration, etc.

Last updated