Skip to content

Rendering ggtext annotations in cowplot::plot_grid on Linux HPC #209

@elgabbas

Description

@elgabbas

I am trying to export PDF files for plots produced by cowplot that include complex labels created with ggtext. There is a text rendering issue (squeezed spaces between words) when using cowplot with a Linux HPC. The same script works nicely on Windows.

Here is a reprex small example

library(cowplot)
library(magritr)
library(ggplot2)
library(ggtext)

# Example list of plots, each uses `ggtext::element_markdown`  in the title
plot_example <- ggplot2::ggplot() +
  ggplot2::geom_point(aes(x = 1, y = 1)) +
  ggplot2::labs(title = "Some text --- Some text") + 
  ggplot2::theme_minimal() +
  ggplot2::theme(
    text = ggplot2::element_text(family = "sans"),
    plot.title = ggtext::element_markdown(face = "bold", size = 20, hjust = 0.5))

# text rendering is as expected under Windows and Linux
grDevices::cairo_pdf(filename = "pdf_2.pdf", width = 18, height = 8, onefile = TRUE)
plot(plot_example)
# grid::grid.draw(plot_list2)
grDevices::dev.off()

Image

When arranging multiple plots using cowplot::plot_grid and adding a common title to them, I have issues with rendering titles created with ggtext. Both the common title and each plot title have issues rendering spaces. I tried to use cowplot::set_null_device("pdf") and cowplot::set_null_device(cowplot::cairo_null_device) with no difference. Adding more spaces (including HTML space symbols) to the text did not help.

# A common title plot that uses `ggtext::element_markdown` 
PlotTitle <- ggplot2::ggplot() +
  ggplot2::labs(title = "Some text --- Some text") + 
  ggplot2::theme_minimal() +
  ggplot2::theme(
    # This is to maintain the same font irrespective of the operating system
    text = ggplot2::element_text(family = "sans"),
    plot.title = ggtext::element_markdown(face = "bold", size = 20, hjust = 0.5))

plot_list <- list(plot_example, plot_example, plot_example)
plot_list2 <- cowplot::plot_grid(
  plotlist = plot_list, ncol = 3, nrow = 1, align = "hv") %>%
  cowplot::plot_grid(PlotTitle, ., ncol = 1, rel_heights = c(0.05, 1))

# cowplot::set_null_device("pdf")
cowplot::set_null_device(cowplot::cairo_null_device)
grDevices::cairo_pdf(filename = "pdf_1.pdf", width = 18, height = 8, onefile = TRUE)
plot(plot_list2)
# grid::grid.draw(plot_list2)
grDevices::dev.off()

Image

Is it possible to get correctly rendered ggtext labels while still using cairo_pdf()? Is it possible to add a theme information to the plots (e.g., to include text = ggplot2::element_text(family = "sans"))?
In my current project, I tried pdf() and ggsave() and had some issues, and I switched to use cairo_pdf().

Thanks in advance.

sessionInfo()
R version 4.4.1 (2024-06-14)
Platform: x86_64-pc-linux-gnu
Running under: SUSE Linux Enterprise Server 15 SP5

Matrix products: default
BLAS/LAPACK: /opt/cray/pe/libsci/24.03.0/GNU/12.3/x86_64/lib/libsci_gnu_mp.so.6.0;  LAPACK version 3.10.1

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

time zone: Europe/Berlin
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base

other attached packages:
[1] magrittr_2.0.3 ggtext_0.1.2   ggplot2_3.5.2

loaded via a namespace (and not attached):
 [1] vctrs_0.6.5         cli_3.6.5           xfun_0.52
 [4] rlang_1.1.6         stringi_1.8.7       cowplot_1.1.3
 [7] renv_1.1.4          generics_0.1.4      litedown_0.7
[10] glue_1.8.0          labeling_0.4.3      markdown_2.0
[13] gridtext_0.1.5      scales_1.4.0        grid_4.4.1
[16] tibble_3.2.1        lifecycle_1.0.4     stringr_1.5.1
[19] BiocManager_1.30.25 compiler_4.4.1      dplyr_1.1.4
[22] RColorBrewer_1.1-3  Rcpp_1.0.14         pkgconfig_2.0.3
[25] farver_2.1.2        R6_2.6.1            dichromat_2.0-0.1
[28] tidyselect_1.2.1    pillar_1.10.2       commonmark_1.9.5
[31] tools_4.4.1         withr_3.0.2         gtable_0.3.6
[34] xml2_1.3.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions