【问题标题】:How to insert line breaks in ggplot2' geom_label如何在ggplot2'geom_label中插入换行符
【发布时间】:2021-03-26 16:02:49
【问题描述】:

注意:此问题已更新,请参阅“编辑”

我正在使用 ggplot2 绘制带有一些注释的条形图。我想在数学符号代码(即%down%)之前和之后添加一个换行符(\n)。但是,geom_label(aes(label=paste(...))) 中的 paste(..., sep = "\n") 失败并出现以下错误:Error in parse(text = text[[i]]) : <text>:2:1: unexpected SPECIAL。我该怎么办?

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

编辑

虽然@the-mad-statter 的answer 非常有用,并且可以让我添加换行符,但没有出现向下箭头字符\u2193,因为我在图表中使用的字体似乎没有@ 987654330@。我通过指定knitr::opts_chunk$set(dev.args = list(family = "Roboto Medium")) 来使用Roboto Medium

【问题讨论】:

  • 你编织成什么格式(例如,html、pdf)?您使用的是什么设备(例如 png、pdf)?
  • @the-mad-statter 我编辑了我的问题,以便您可以看到我正在使用cairo_pdf 编写 PDF 文件。
  • 提供的答案似乎解决了您最初的大部分问题 - 如何添加换行符,以及(根据当时可用的信息)如何显示您想要的符号。我建议提出一个新问题,重点是使用 Roboto 字体的向下箭头字符,而不是编辑问题以“移动球门柱”。这将使疯狂的国家在回答您提出的问题时得到应有的赞誉,并且新问题将得到更多关注,它将显示为“未回答”。

标签: r ggplot2 line-breaks geom-bar mathematical-expressions


【解决方案1】:

似乎对我有用的方法是将 showtext 包与所需的 Unicode 符号结合使用:

---
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
  )
```

【讨论】:

  • 感谢您的第一个有用的回答!我赞成你的。确实,您的回答使我能够添加换行符。但是,向下箭头字符\u2193 没有出现,因为我在图表中使用的字体Roboto Medium 似乎没有\u2193。我用knitr::opts_chunk$set(dev.args = list(family = "Roboto Medium")) 指定了字体。你有什么办法解决这个问题吗?
  • 您好 Carlos,请查看此解决方案是否适合您。
  • 抱歉回复晚了!除了我使用的字体的新问题外,您的回答对我来说很好。我接受您的回答,这使我能够添加新行并告诉我如何插入向下箭头的 unicode 字符。
猜你喜欢
  • 2019-01-30
  • 2018-05-25
  • 1970-01-01
  • 1970-01-01
  • 2014-12-24
  • 2018-04-23
  • 2019-05-27
  • 2012-04-16
  • 2013-12-05
相关资源
最近更新 更多