0

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")).

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
Carlos Luis Rivera
  • 3,108
  • 18
  • 45
  • What format are you knitting to (e.g., html, pdf)? What device are you using (e.g., png, pdf)? – the-mad-statter Mar 29 '21 at 21:19
  • @the-mad-statter I edited my question so that you can see I'm knitting a PDF file with `cairo_pdf`. – Carlos Luis Rivera Mar 29 '21 at 23:07
  • 2
    It seems like the answer provided addressed most of your original question - how to add a line break, and (based on the information available at the time) how to show the symbol you want. I'd suggest asking a **new question** focused on the down-arrow character with the Roboto font rather than editing the question to "move the goalposts" as it were. That will give the-mad-statter due credit for answering the question as you posed it, and the new question will get more attention at it will show up as "unanswered". – Gregor Thomas Mar 30 '21 at 16:36

1 Answers1

2

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
  )
```
the-mad-statter
  • 5,650
  • 1
  • 10
  • 20
  • Thank you for the first and useful answer! I up-voted yours. Indeed your answer enables me to add a newline. However, the down arrow character `\u2193` does not appear, since my font used in the chart [Roboto Medium](https://fonts.google.com/share?selection.family=Roboto:wght@500) does not seem to have `\u2193`. I specified the font by `knitr::opts_chunk$set(dev.args = list(family = "Roboto Medium"))`. Do you have any idea to solve this problem? – Carlos Luis Rivera Mar 26 '21 at 23:09
  • Hi Carlos, please see if this solution works for you. – the-mad-statter Mar 30 '21 at 16:32
  • Sorry for my late reply! Your answer works fine for me except the new issue of the font I used. I accept your answer which enables me to add new lines and tells me how to insert the unicode character of down arrow. – Carlos Luis Rivera Mar 30 '21 at 23:34