gpt4 book ai didi

r - 将带有多个换行符和斜体字的文本添加到 R 中的绘图中

转载 作者:行者123 更新时间:2023-12-01 05:38:59 24 4
gpt4 key购买 nike

我想在图中添加几行文本,其中一些单词是斜体的。文本应该是这样的:

Italic Text: Some
words with
new lines.

More italic text: Yet
more words divided
by new lines.

Italics again: And more
text with
new lines.



以下代码打印一行带有斜体字的文本:
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 = ""
)
)
)

enter image description here

这会创建换行符,但没有斜体:
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.")

enter image description here

但是当我尝试将文本分成几行并添加斜体时,换行符会导致奇怪的结果:
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 = ""
)
)
)

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

将带有多个斜体字的多行文本添加到绘图中的最简单方法是什么?
  • 理想情况下,仅使用基础 R。
  • 并且没有痛苦地分别定位每一行文本。
  • 最佳答案

    我们可以使用 substitute()结合斜体和普通文本。 To left align the text we can use option pos=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)

    enter image description here

    注意:它在导出时会移动一点。我已导出分辨率为 500x500 的图形。

    关于r - 将带有多个换行符和斜体字的文本添加到 R 中的绘图中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54734048/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com