【发布时间】: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