4

我想在绘图中添加几行文本,其中一些单词以斜体显示。文本应如下所示:

斜体文本:一些
带有
换行符的单词。更多斜体

文本:
更多的单词被换行符分隔


再次斜体:以及更多带有 新行的
文本。

以下代码使用斜体字打印单行文本:

plot(c(0, 2), c(0, 2))
text(1, 1, bquote(
        paste(
            italic("Italic Text:"),
            " Some words with new lines. ",
            italic("More italic text:"),
            "Yet more words divided by new lines. ",
            italic("Italics again:"),
            "And more text with new lines.",
            sep = ""
        )
    )
)

在此处输入图像描述

这会创建换行符,但没有斜体:

plot(c(0, 2), c(0, 2))
text(1, 1, "Italic Text: Some\nwords with\nnew lines.\n\nMore italic text: Yet\nmore words divided\nby new lines.\n\nItalics again: And more\ntext with\nnew lines.")

在此处输入图像描述

但是当我尝试将文本分成几行并添加斜体时,换行符会导致奇怪的结果:

plot(c(0, 2), c(0, 2))
text(1, 1, bquote(
        paste(
            italic("Italic Text:"),
            " Some\nwords with\nnew lines.\n\n",
            italic("More italic text:"),
            "Yet\nmore words divided\nby new lines.\n\n",
            italic("Italics again:"),
            "And more\ntext with\nnew lines.",
            sep = ""
        )
    )
)

在此处输入图像描述

atop(),正如其他答案中所建议的,仅适用于两行。

将带有多个斜体字的多行文本添加到绘图中的最简单方法是什么?

  • 理想情况下,仅使用基数 R。
  • 并且无需痛苦地单独定位每一行文本。
4

3 回答 3

3

我们可以substitute()用来组合斜体和普通文本。要左对齐文本,我们可以使用 optionpos=4。然后我们可以像这样把它摆弄在一起。

plot(c(0, 2), c(0, 2))
text(1, 1.9, substitute(paste(italic("Italic:"), " Some")), pos=4)
text(1, 1.7, "words with\nnew lines.", pos=4)
text(1., 1.4, substitute(paste(italic("More italic text:"), " Yet")), pos=4)
text(1, 1.21, "words with\nnew lines.", pos=4)
text(1., .9, substitute(paste(italic("Italics again:"), " And more")), pos=4)
text(1, .71, "text with\nnew lines.", pos=4)

在此处输入图像描述

注意:导出时它会稍微移动。我以 500x500 的分辨率导出了该图。

于 2019-02-17T15:26:18.947 回答
2

这个怎么样?

plot(c(0, 10), c(0, 10))
text(
  5,
  7:2,
  c(
    expression(paste(italic("Italic Text:"), " Some")),
    "\nwords with\nnew lines.",
    expression(paste(italic("More italic text:"), " Yet")),
    "\nmore words divided\nby new lines.",
    expression(paste(italic("Italics again:"), "And more")),
    "\ntext with\nnew lines."
  )
)

在此处输入图像描述

我认为正在发生的事情是小插图中text提到的。

如果标签比 x 和 y 长,则将坐标循环到标签的长度

这可能是你得到奇怪布局的原因。如果你7:2在里面更改我的代码text,我也会得到你的“混乱”布局。只有中心段落看起来仍然“关闭” - 我真的不知道为什么......它看起来和其他 2 一样:-s。其余的看起来就在你正在寻找的东西上,我接受了吗?无论如何,看起来就像你的第二张照片,我用它作为我的参考点......

我只是对此进行了更多的爱抚..如果您想要左对齐,只需在函数pos = 4内部添加即可。text

在此处输入图像描述

从这里开始,你应该能够准确地得到你想要的。希望这可以帮助!

于 2019-10-10T17:52:09.173 回答
0

我不知道斜体的工作......但我写的那个包可以帮助你:

devtools::install_github("igorkf/breaker")
library(breaker)

nbreak("Some text that you want and the italic stuff", n = 3)
#[1] "Some text that\nyou want and\nthe italic stuff"

#You can also use this with purrr:
purrr::map_chr(list("the first text that you want",
                    "the second text that you want",
                    "the third text that you want"),
                  ~nbreak(.x, n = 2))
#[1] "the first\ntext that\nyou want"  "the second\ntext that\nyou want" "the third\ntext that\nyou want"

你也只能打破一次使用loop = F

于 2019-02-27T02:11:17.770 回答