gpt4 book ai didi

r - 如何使用 latex 注释() ggplot

转载 作者:行者123 更新时间:2023-12-02 11:15:46 25 4
gpt4 key购买 nike

我想使用 annotate() 将 latex 文本添加到 ggplot2 图中。使用表达式(),如所述 here将 latex 添加到轴标签似乎不起作用。也就是说:

# Use expression() to create subscripted text
p <- ggplot(mpg, aes(x=cty, y=hwy)) + geom_point() +
scale_x_continuous(expression(text[subscript]))

# But expression() in annotate adds nothing to the plot
p + annotate("text", x=10, y=40, label=expression(text[subscript]))

# Passing regular text to annotate works fine
p + annotate("text", x=10, y=40, label="foo")

为什么annotate表达式的处理方式与其他ggplot函数不同?我怎样才能用 latex 注释?

最佳答案

有一个名为 latex2expR 包可能会有所帮助。它具有函数 TeX,它接受一些用美元符号 $ 括起来的 LaTeX 表达式,如下例所示:

library(latex2exp)
library(ggplot2)

qplot(1, "A")+
ylab(TeX("Formula: $\\frac{2hc^2}{\\lambda^\\beta}$"))+
xlab(TeX("$\\alpha$"))

Example

更多示例可以在this vignette中找到.

关于r - 如何使用 latex 注释() ggplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12514612/

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