user3022875
user3022875

Reputation: 9018

math expression on ggplots

I'd like to put the expression

a x b^.5 x c^.8

on a ggplot label

here is the code:

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$'))

any idea how to do that?

Upvotes: 1

Views: 140

Answers (1)

Suhas Hegde
Suhas Hegde

Reputation: 416

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"))

enter image description here enter image description here

Upvotes: 1

Related Questions