Search code examples
rggplot2line-breaksgeom-barmathematical-expressions

How to insert line breaks in ggplot2' geom_label


Note: this question is updated, so see "EDIT"

I'm drawing a bar chart with some annotations using ggplot2. I would like to add a line break (\n) before and after the mathematical symbol code (i.e. %down%). However, paste(..., sep = "\n") in geom_label(aes(label=paste(...))) fails with the following error: Error in parse(text = text[[i]]) : <text>:2:1: unexpected SPECIAL. What should I do?

---
output: 
  bookdown::pdf_document2:
    latex_engine: xelatex
    keep_tex: true
    dev: cairo_pdf
---
knitr::opts_chunk$set(
  dev      = "cairo_pdf",
  dev.args = list(family = "Roboto Medium")
  )
library(tidyverse)
library(magrittr)
tibble(
  F1 = factor(
    c(-0.5, -0.5, 0.5, 0.5), 
    levels = c(-0.5, 0.5), 
    labels = c("A", "H")
  ), 
  F2 = factor(
    c(-0.5, 0.5, -0.5, 0.5), 
    levels=c(-0.5, 0.5), 
    labels=c("A", "H")
  ),
  pct = c(60, 20, 40, 20)
) %>% 
  ggplot(
    ., 
    aes(
      x = F1, 
      y = pct, 
      color = F2, 
      fill = F2
    )
  )+
  geom_bar(
    stat="identity",
    width = 0.6,
    position = position_dodge(width = 0.7)
  ) +
  geom_label(
    parse = TRUE,
    aes(
      label = paste(
        sep = "", # "\n"
        F1, 
        "%down%", 
        F2
      ),
      vjust = -1
    ),
    position = position_dodge2(
      width = 1 
    ),
    fill = "white",
    colour = "black",
    label.size = NA,
    size = 5
  ) 

EDIT

Although the answer by @the-mad-statter is quite useful and enables me to add a newline, the down arrow character \u2193 does not appear, since my font used in the chart does not seem to have \u2193. I'm using Roboto Medium by specifying knitr::opts_chunk$set(dev.args = list(family = "Roboto Medium")).


Solution

  • Something that seems to work for me is to use the showtext package in combination with the desired Unicode symbol:

    ---
    output: 
      bookdown::pdf_document2:
        latex_engine: xelatex
        keep_tex: true
        dev: cairo_pdf
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(
      echo     = FALSE, 
      dev      = "cairo_pdf",
      dev.args = list(family = "Roboto")
    )
    
    library(tidyverse)
    
    library(showtext)
    font_add_google("Roboto")
    showtext_auto()
    ```
    
    ```{r}
    tibble(
      F1 = factor(
        c(-0.5, -0.5, 0.5, 0.5), 
        levels = c(-0.5, 0.5), 
        labels = c("A", "H")
      ), 
      F2 = factor(
        c(-0.5, 0.5, -0.5, 0.5), 
        levels=c(-0.5, 0.5), 
        labels=c("A", "H")
      ),
      pct = c(60, 20, 40, 20)
    ) %>% 
      ggplot(
        ., 
        aes(
          x = F1, 
          y = pct, 
          color = F2, 
          fill = F2
        )
      )+
      geom_bar(
        stat="identity",
        width = 0.6,
        position = position_dodge(width = 0.7)
      ) +
      geom_label(
        parse = FALSE,
        aes(
          label = paste(
            sep = "\n",
            F1, 
            "\u2193", 
            F2
          ),
          vjust = -1
        ),
        position = position_dodge2(
          width = 1 
        ),
        fill = "white",
        colour = "black",
        label.size = NA,
        size = 5
      )
    ```