Search code examples
rggplot2quarto

Special characters in ggplot2 axis labels from Quarto to PDF


I'm trying to add both the lowercase greek symbol delta and permille to axis labels in ggplot2 within Quarto, then knit to PDF intact. The results inline look fine, but when in PDF format the permille comes out as ellipses (...). I am using Arial Unicode MS font, specified in the YAML. Here is my base code, using both the permille symbol and unicode as tests:

sia_data_plot <- read_excel("~/filepath/doc.xlsx")

sia_data_plot <- dplyr::filter(sia_data_plot, d13C < max(d13C))

sia_data_plot <- dplyr::filter(sia_data_plot, d15N < max(d15N))

sia_month_facet <- sia_data_plot 

sia_month_facet <- ggplot(data = sia_data_plot, aes(x = d13C, y = d15N)) +
   geom_point(aes(color = Tissue, shape = Transect), size = 3) +
   facet_wrap(~Month) +
   facet_wrap(~factor(Month, levels=c('March', 'June', 'August', 'October'))) +
   scale_colour_brewer(type = "seq", palette = "Dark2") +
   ggtitle("Isotopic composition biplot by month") +
   labs(x = expression(paste(delta^15 * "N (‰)")), y = expression(delta^15 * "N (\u2030)")) +
   theme_grey()

print(sia_month_facet)

Solution

  • Edit: So you need to update the theme accordingly to add your font. Then for it to render you need to set the device to cairo_pdf

    ---
    title: "Untitled"
    format: pdf
    knitr: 
      opts_chunk: 
        dev: "cairo_pdf"
    ---
    
    
    
    ```{r}
    
    options(tidyverse.quiet = TRUE)
    library(palmerpenguins)
    library(latex2exp)
    library(tidyverse)
    
    
    plot_dat = penguins |>
        mutate(sqr_body_mass = body_mass_g^2,
               sqr_bill_depth = bill_depth_mm^2)
    
    
    ggplot(plot_dat, aes(x = sqr_bill_depth, y = sqr_body_mass, color = species)) +
        geom_point() +
        geom_smooth() + 
        labs(x = expression(paste(delta^15 * "N (‰)"))) +
        theme(text = element_text(family = "Arial Unicode MS"))