我有以下ggplot:
library(ggplot2)
library(ggtext)
library(ggdist)
library(latex2exp)
set.seed(123)
DF <- rbind(data.frame('Label' = 'A', val = rnorm(200, 5)),
data.frame('Label' = 'B', val = rnorm(500, 10)))
ggplot(DF, aes(Label, val))
stat_dots(aes(fill = Label))
geom_textbox(aes(-Inf, -Inf, hjust = 0, vjust = 0, label = parse(text = TeX(r'(\tau)'))), data.frame())
基本上我想在視窗中撰寫LaTeX語法。這里我舉了一個小例子,但是在我原來的例子中,我有一個很大的表達。textboxggplotLaTeX
使用上面的代碼,我得到以下錯誤:
Don't know how to automatically pick scale for object of type expression. Defaulting to continuous.
Error: Aesthetics must be valid data columns. Problematic aesthetic(s): label = parse(text = TeX("\\tau")).
Did you mistype the name of a data column or forget to add after_stat()?
任何如何LaTeX在textboxin 中使用的指標ggplot都會非常有幫助。
謝謝你的指點。
uj5u.com熱心網友回復:
?latex2exp::latex2exp_supported()似乎不包括tau,因此無法將其翻譯為plotmath. 一種解決方法是繪制一個空的文本框并用于annotate()在其頂部放置一個圖層,這可以采用 LaTex,正如 Svannoy
但是,仍然有一些懸而未決的問題——如何處理不受支持的運算式,例如 tau?
該TeX呼叫的輸出是
"paste(' ','',hat(paste('Y')),'',phantom() == phantom(),'B',phantom() [ {paste('0')} ],'',phantom() phantom(),'B',phantom() [ {paste('1')} ],'X',phantom() [ {paste('1')} ],'')"
而不是與搞亂了,比完全實作其他諸如latex2expr包裝后端,我們可以用另一種手動插入annotate。鑒于,這不是漂亮的代碼。
ggplot(DF, aes(Label, val))
stat_dots(aes(fill = Label))
geom_textbox(x= -Inf, y= -Inf, hjust = 0, vjust = 0, label = "")
annotate(geom='text',
x= -Inf, y= -Inf, hjust = 0, vjust = 0,
label= TeX(" $\\hat{Y} = B_0 B_1X_1",
output='character'), parse = TRUE)
annotate(geom = 'text',
x= -Inf, y= -Inf, hjust = -23.5, vjust = -0.20,
label = sprintf('%s', "\u03C4"), parse = T)
其中 tau 是 Unicode 表示。最后,笨重的部分是調整變數以使其位于正確的位置。

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/329914.html
