我正在使用{glue}包來撰寫運算式,然后我將其決議并顯示在 ggplot2 注釋中。
但是,如果我有一個多行運算式,它們不會垂直對齊。我怎樣才能實作這樣的對齊?我以為atop displaystyle會這樣做,但事實并非如此。
library(ggplot2)
library(glue)
b.text <- "bottom part of the expression"
t.text <- "top part of the expression"
ggplot()
labs(subtitle = parse(text = glue("list(atop('{t.text}', '{b.text}'))")))

uj5u.com熱心網友回復:
我建議創建一個向量并使用glue_collapse 用換行符折疊它
library(ggplot2)
library(glue)
b.text <- "bottom part of the expression"
t.text <- "top part of the expression"
vec <- c(t.text, b.text)
ggplot()
labs(subtitle = glue_collapse(vec, sep = "\n"))

由reprex 包(v2.0.1)于 2021 年 11 月 25 日創建
uj5u.com熱心網友回復:
如果我們想使用 OP 的代碼,請用較少的字符數填充字串中的空格
library(ggplot2)
library(stringr)
library(glue)
mx <- max(nchar(t.text), nchar(b.text)) 1
ggplot()
labs(subtitle = parse(text = glue("list(atop('{str_pad(t.text, width = mx 2, side = 'right')}', '{b.text}'))")))
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/366449.html
