首页 > 解决方案 > ggplots 上的数学表达式

问题描述

我想表达

axb^.5 xc^.8

在 ggplot 标签上

这是代码:

library(latex2exp)

    df = data.frame(x= 1:5,y = 1:5)
    ggplot(df,aes( x= x ,y = y))+geom_point()+
      geom_text(x = 1 , y =1, label = TeX('$ a \\times b^.5 \\times c^.8$'))

知道怎么做吗?

标签: rggplot2

解决方案


library(latex2exp)
library(ggplot2)
library(dplyr)


tex_var <- '$ a \\times b^.5 \\times c^.8$'

# to label each point with the LaTEX expression
df = data.frame(x= 1:5,y = 1:5, z = )
ggplot(df,aes( x= x ,y = y)) +
  geom_point() +
  geom_text(aes( y = y+.25),label = TeX(tex_var, "text"))


# to put it as a label to the whole plot
ggplot(df,aes( x= x ,y = y)) + 
  geom_point() +
  labs(title = TeX(tex_var, "text")) +
  theme(plot.title=element_text(size=20, face="bold", color="red"))

在此处输入图像描述 在此处输入图像描述


推荐阅读