簡體   English   中英

將帶有多個換行符和斜體字的文本添加到 R 中的繪圖中

[英]Add text with multiple line breaks and italic words to a plot in R

我想在圖中添加幾行文本,其中一些單詞是斜體的。 文本應該是這樣的:

斜體文本:一些
詞與
新線路。

更多斜體文字:然而
更多的單詞分開
通過新的線路。

再次斜體:還有更多
文字與
新線路。

以下代碼打印一行帶有斜體字的文本:

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。
  • 並且沒有痛苦地分別定位每一行文本。

我們可以使用substitute()來組合斜體和普通文本。 要左對齊文本,我們可以使用選項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)

在此處輸入圖片說明

注意:它在導出時會移動一點。 我已導出分辨率為 500x500 的圖形。

這個怎么樣?

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 長,坐標將循環到標簽的長度

這可能是您獲得奇怪布局的原因。 如果你在text更改我的7:2代碼,我也會得到你“搞砸”的布局。 只有中間的段落看起來仍然“關閉” - 我真的不知道為什么......它看起來和其他 2 :-s 其余的看起來很符合您的要求,我接受嗎? 無論如何,看起來就像你的第二張照片,我用它作為我的參考點......

我只是喜歡這個..如果你想要左對齊,只需在text函數中添加pos = 4

在此處輸入圖片說明

從這里開始,您應該能夠准確地獲得您想要的東西。 希望這可以幫助!

我不知道斜體的工作......但我寫的那個包可以幫助你:

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中斷一次

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM