Formatting axis labels

Author

Charlotte Soneson, Michael Stadler

Summary

This document provides some examples of how to format various types of labels in ggplot2 plots.

Prepare data

Run the following code to load the packages and data that are used in this document:

suppressPackageStartupMessages({
    library(dplyr)
    library(tibble)
    library(swissknife)
    library(ggplot2)
    library(ggtext)
    library(ggrepel)
})

loadExampleData("mycars")
`mycars`: re-encoded version of `datasets::mtcars`
mycars <- mycars |>
    rownames_to_column("model")

tibble(mycars)
# A tibble: 32 × 14
   model         mpg cyl    disp    hp  drat    wt  qsec    vs    am  gear  carb
   <chr>       <dbl> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1 Mazda RX4    21   6      160    110  3.9   2.62  16.5     0     1     4     4
 2 Mazda RX4 …  21   6      160    110  3.9   2.88  17.0     0     1     4     4
 3 Datsun 710   22.8 4      108     93  3.85  2.32  18.6     1     1     4     1
 4 Hornet 4 D…  21.4 6      258    110  3.08  3.22  19.4     1     0     3     1
 5 Hornet Spo…  18.7 8      360    175  3.15  3.44  17.0     0     0     3     2
 6 Valiant      18.1 6      225    105  2.76  3.46  20.2     1     0     3     1
 7 Duster 360   14.3 8      360    245  3.21  3.57  15.8     0     0     3     4
 8 Merc 240D    24.4 4      147.    62  3.69  3.19  20       1     0     4     2
 9 Merc 230     22.8 4      141.    95  3.92  3.15  22.9     1     0     4     2
10 Merc 280     19.2 6      168.   123  3.92  3.44  18.3     1     0     4     4
# ℹ 22 more rows
# ℹ 2 more variables: engine_shape <fct>, transmission <fct>

Construct basic plot

Using the mycars data, create the following basic plot.

Code
ggplot(mycars, 
       aes(x = mpg, y = disp, color = cyl)) + 
    geom_point(size = 4) +
    scale_color_manual(values = c(`4` = "purple", `6` = "orange", 
                                  `8` = "forestgreen"), 
                       name = "Number of\ncylinders") + 
    labs(x = "Miles/gallon", y = "Displacement") + 
    theme_bw(base_size = 13)

Add labels to points

The first extension we will make to the basic plot is to add a label to each point. We will use the model column. Note that the labels are added in such a way that they do not overlap (more precisely, different labels are designed to repel each other to avoid overlaps whenever possible).

Code
ggplot(mycars, 
       aes(x = mpg, y = disp, color = cyl, label = model)) + 
    geom_point(size = 4) +
    geom_text_repel(min.segment.length = 0, max.overlaps = Inf) + 
    scale_color_manual(values = c(`4` = "purple", `6` = "orange", 
                                  `8` = "forestgreen"), 
                       name = "Number of\ncylinders") + 
    labs(x = "Miles/gallon", y = "Displacement") + 
    theme_bw(base_size = 13)

Format axis labels using markdown

Next, we turn our attention to the axis labels. The ggtext package provides functionality to format the axes (or other text elements) using markdown. Use this functionality to add formatting (italics and bold) to the x-axis title, and add a unit to the y-axis title (here, it is helpful to note that HTML code can be interpreted by markdown).

Code
ggplot(mycars, 
       aes(x = mpg, y = disp, color = cyl, label = model)) + 
    geom_point(size = 4) +
    geom_text_repel(min.segment.length = 0, max.overlaps = Inf) + 
    scale_color_manual(values = c(`4` = "purple", `6` = "orange", 
                                  `8` = "forestgreen"), 
                       name = "Number of\ncylinders") + 
    labs(x = "_Miles/gallon_ (**mpg**)", y = "Displacement (in<sup>3</sup>)") + 
    theme_bw(base_size = 13) + 
    theme(axis.title.x.bottom = element_markdown(),
          axis.title.y.left = element_markdown())

Add special characters to labels

Next, we will change the axis titles completely (the x-axis title to \(\alpha\), and the y-axis title to \(\beta^4\)). Note that these labels don’t necessarily make much sense in this case, but they serve as a useful example for general special characters. We can achieve our goal in several ways - for example, using the R expression() function:

Code
## Using expression
ggplot(mycars, 
       aes(x = mpg, y = disp, color = cyl, label = model)) + 
    geom_point(size = 4) +
    geom_text_repel(min.segment.length = 0, max.overlaps = Inf) + 
    scale_color_manual(values = c(`4` = "purple", `6` = "orange", 
                                  `8` = "forestgreen"), 
                       name = "Number of\ncylinders") + 
    labs(x = expression(~ alpha), y = expression(~ beta ^ 4)) + 
    theme_bw(base_size = 13)

or by using Unicode characters characters (note that these may not be properly displayed unless the plot backend is set to Cairo):

Code
## Using Unicode
ggplot(mycars, 
       aes(x = mpg, y = disp, color = cyl, label = model)) + 
    geom_point(size = 4) +
    geom_text_repel(min.segment.length = 0, max.overlaps = Inf) + 
    scale_color_manual(values = c(`4` = "purple", `6` = "orange", 
                                  `8` = "forestgreen"), 
                       name = "Number of\ncylinders") + 
    labs(x = "\U03B1", y = "\U03B2\U2074") + 
    theme_bw(base_size = 13)

Resources

  • Some useful examples of using expression and bquote to add special characters to plot labels. Also see ?plotmath.

Session info

Code
sessioninfo::session_info()
─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.5.2 (2025-10-31)
 os       macOS Sequoia 15.7.1
 system   aarch64, darwin20
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       Europe/Zurich
 date     2025-11-03
 pandoc   3.6.4 @ /usr/local/bin/ (via rmarkdown)
 quarto   1.7.28 @ /usr/local/bin/quarto

─ Packages ───────────────────────────────────────────────────────────────────
 package              * version date (UTC) lib source
 abind                  1.4-8   2024-09-12 [1] CRAN (R 4.5.0)
 Biobase                2.70.0  2025-10-29 [1] Bioconductor 3.22 (R 4.5.1)
 BiocGenerics           0.56.0  2025-10-29 [1] Bioconductor 3.22 (R 4.5.1)
 BiocParallel           1.44.0  2025-10-29 [1] Bioconductor 3.22 (R 4.5.1)
 Cairo                  1.7-0   2025-10-29 [1] CRAN (R 4.5.0)
 cli                    3.6.5   2025-04-23 [1] CRAN (R 4.5.0)
 codetools              0.2-20  2024-03-31 [2] CRAN (R 4.5.2)
 commonmark             2.0.0   2025-07-07 [1] CRAN (R 4.5.0)
 DelayedArray           0.36.0  2025-10-29 [1] Bioconductor 3.22 (R 4.5.1)
 dichromat              2.0-0.1 2022-05-02 [1] CRAN (R 4.5.0)
 digest                 0.6.37  2024-08-19 [1] CRAN (R 4.5.0)
 dplyr                * 1.1.4   2023-11-17 [1] CRAN (R 4.5.0)
 evaluate               1.0.5   2025-08-27 [1] CRAN (R 4.5.1)
 farver                 2.1.2   2024-05-13 [1] CRAN (R 4.5.0)
 fastmap                1.2.0   2024-05-15 [1] CRAN (R 4.5.0)
 fs                     1.6.6   2025-04-12 [1] CRAN (R 4.5.0)
 generics               0.1.4   2025-05-09 [1] CRAN (R 4.5.0)
 GenomeInfoDb           1.46.0  2025-10-29 [1] Bioconductor 3.22 (R 4.5.1)
 GenomicRanges          1.62.0  2025-10-29 [1] Bioconductor 3.22 (R 4.5.1)
 ggplot2              * 4.0.0   2025-09-11 [1] CRAN (R 4.5.0)
 ggrepel              * 0.9.6   2024-09-07 [1] CRAN (R 4.5.0)
 ggtext               * 0.1.2   2022-09-16 [1] CRAN (R 4.5.0)
 glue                   1.8.0   2024-09-30 [1] CRAN (R 4.5.0)
 gridtext               0.1.5   2022-09-16 [1] CRAN (R 4.5.0)
 gtable                 0.3.6   2024-10-25 [1] CRAN (R 4.5.0)
 htmltools              0.5.8.1 2024-04-04 [1] CRAN (R 4.5.0)
 htmlwidgets            1.6.4   2023-12-06 [1] CRAN (R 4.5.0)
 httr                   1.4.7   2023-08-15 [1] CRAN (R 4.5.0)
 IRanges                2.44.0  2025-10-29 [1] Bioconductor 3.22 (R 4.5.1)
 jsonlite               2.0.0   2025-03-27 [1] CRAN (R 4.5.0)
 KernSmooth             2.23-26 2025-01-01 [2] CRAN (R 4.5.2)
 knitr                  1.50    2025-03-16 [1] CRAN (R 4.5.0)
 labeling               0.4.3   2023-08-29 [1] CRAN (R 4.5.0)
 lattice                0.22-7  2025-04-02 [1] CRAN (R 4.5.0)
 lifecycle              1.0.4   2023-11-07 [1] CRAN (R 4.5.0)
 litedown               0.8     2025-11-02 [1] CRAN (R 4.5.0)
 magrittr               2.0.4   2025-09-12 [1] CRAN (R 4.5.1)
 markdown               2.0     2025-03-23 [1] CRAN (R 4.5.0)
 Matrix                 1.7-4   2025-08-28 [2] CRAN (R 4.5.2)
 MatrixGenerics         1.22.0  2025-10-29 [1] Bioconductor 3.22 (R 4.5.1)
 matrixStats            1.5.0   2025-01-07 [1] CRAN (R 4.5.0)
 pillar                 1.11.1  2025-09-17 [1] CRAN (R 4.5.0)
 pkgconfig              2.0.3   2019-09-22 [1] CRAN (R 4.5.0)
 png                    0.1-8   2022-11-29 [1] CRAN (R 4.5.0)
 purrr                  1.1.0   2025-07-10 [1] CRAN (R 4.5.1)
 R6                     2.6.1   2025-02-15 [1] CRAN (R 4.5.0)
 RColorBrewer           1.1-3   2022-04-03 [1] CRAN (R 4.5.0)
 Rcpp                   1.1.0   2025-07-02 [1] CRAN (R 4.5.0)
 rlang                  1.1.6   2025-04-11 [1] CRAN (R 4.5.0)
 rmarkdown              2.30    2025-09-28 [1] CRAN (R 4.5.0)
 rstudioapi             0.17.1  2024-10-22 [1] CRAN (R 4.5.0)
 S4Arrays               1.10.0  2025-10-29 [1] Bioconductor 3.22 (R 4.5.1)
 S4Vectors              0.48.0  2025-10-29 [1] Bioconductor 3.22 (R 4.5.1)
 S7                     0.2.0   2024-11-07 [1] CRAN (R 4.5.0)
 scales                 1.4.0   2025-04-24 [1] CRAN (R 4.5.0)
 Seqinfo                1.0.0   2025-10-29 [1] Bioconductor 3.22 (R 4.5.1)
 sessioninfo            1.2.3   2025-02-05 [1] CRAN (R 4.5.0)
 SparseArray            1.10.1  2025-10-31 [1] Bioconductor 3.22 (R 4.5.1)
 stringi                1.8.7   2025-03-27 [1] CRAN (R 4.5.0)
 stringr                1.5.2   2025-09-08 [1] CRAN (R 4.5.0)
 SummarizedExperiment   1.40.0  2025-10-29 [1] Bioconductor 3.22 (R 4.5.1)
 swissknife           * 0.44    2025-11-03 [1] Github (fmicompbio/swissknife@c69e512)
 tibble               * 3.3.0   2025-06-08 [1] CRAN (R 4.5.0)
 tidyselect             1.2.1   2024-03-11 [1] CRAN (R 4.5.0)
 UCSC.utils             1.6.0   2025-10-29 [1] Bioconductor 3.22 (R 4.5.1)
 usethis                3.2.1   2025-09-06 [1] CRAN (R 4.5.0)
 utf8                   1.2.6   2025-06-08 [1] CRAN (R 4.5.0)
 vctrs                  0.6.5   2023-12-01 [1] CRAN (R 4.5.0)
 withr                  3.0.2   2024-10-28 [1] CRAN (R 4.5.0)
 xfun                   0.54    2025-10-30 [1] CRAN (R 4.5.0)
 xml2                   1.4.1   2025-10-27 [1] CRAN (R 4.5.0)
 XVector                0.50.0  2025-10-29 [1] Bioconductor 3.22 (R 4.5.1)
 yaml                   2.3.10  2024-07-26 [1] CRAN (R 4.5.0)

 [1] /Users/stadler/Library/R/arm64/4.5/library/__bioc322
 [2] /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/library
 * ── Packages attached to the search path.

──────────────────────────────────────────────────────────────────────────────