Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass multiple variables and greek letters to ggtitle

Tags:

r

ggplot2

I need to pass three variables to a ggtitle. Something like

lambda=1
alpha=0.9
mem=2000
g <- ggplot(data=data.frame(x=0,y=0))+geom_point(aes(x=x,y=y))
s<-sprintf("\\alpha=%f, \\lambda=%f, \\memory=%g",alpha,lambda,mem)
g+ggtitle(s)

but unfortunately no greek letters are shown (I know there is expression but I could not figure out how to use it). With just one variable, there is a thread with a solution (bquote). For passing multiple variables there is another thread but does not treat greek symbols.

Many thanks

like image 596
Ed Mendes Avatar asked May 20 '15 01:05

Ed Mendes


1 Answers

In order for the greek letters to render properly, you need to build an expression, not a string. Here's one way to do that

g + ggtitle(bquote(list(alpha==.(alpha), lambda==.(lambda), memory==.(mem))))

bquote() will work with any number of variables, not just one.

enter image description here

like image 199
MrFlick Avatar answered Nov 04 '22 10:11

MrFlick