使用latex2exp :: TeX用方程式注释由ggplot2绘制的图 [英] Annotate a plot made with ggplot2 with an equation using latex2exp::TeX
本文介绍了使用latex2exp :: TeX用方程式注释由ggplot2绘制的图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的目标是通过使用软件包 latex2exp
将ggplot2绘制的图放在图中的空白区域中,以对图进行注释.例如,给定等式:
eqn<-"$ \\ textbf {Volume}(ml)= 0.035 \\ cdot \\ textbf {CSA}(mm ^ 2)+ 0.127 \\ cdot \\ textbf {age}(月)-7.8 $"
在R 3.5中运行以下代码:
库(ggplot2)库(latex2exp)ggplot(mpg,aes(x =偏移,y =高速公路))+geom_point()+注释(文本",x = 4,y = 40,label = TeX(eqn),hjust = 0,大小= 5)
我的示例代码失败,并出现以下错误:
stats :: complete.cases(df [,vars,drop = FALSE])中的 错误:参数的无效类型"(表达式)
我在做什么错了?
解决方案
有效的解决方案,基于:
My goal is to annotate a plot made with ggplot2 by placing an equation in white space within the plot using the package latex2exp
For example, given the equation:
eqn <- "$\\textbf{Volume}(ml) = 0.035 \\cdot \\textbf{CSA}(mm^2) + 0.127 \\cdot \\textbf{age}(months) - 7.8$"
Running the following code in R 3.5:
library(ggplot2)
library(latex2exp)
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point() +
annotate("text", x = 4, y = 40, label=TeX(eqn), hjust=0, size = 5)
My example code fails with the error below:
Error in stats::complete.cases(df[, vars, drop = FALSE]) :
invalid 'type' (expression) of argument
What am I doing wrong?
解决方案
A working solution, based on:
https://github.com/stefano-meschiari/latex2exp/issues/13
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point() +
annotate("text", x=3, y=40, label=TeX(eqn, output="character"),
hjust=0, size = 4, parse = TRUE)
这篇关于使用latex2exp :: TeX用方程式注释由ggplot2绘制的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文