Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 label: Combination of Greek symbol and exponential term,

Tags:

r

ggplot2

I need to label my y axis so that it displays first the word "Power" followed by an expression in square brackets: [micro Volt squared].

Power in micro Volt squared

I can produce single parts of the overall label that I want, but I run into problems when I want to combine them:

x <- 1:10; y <- 10:1; z <- data.frame(x,y)
g <- ggplot(z, aes(x,y) + geom_bar(stat='identity')

g + ylab('Power') # No problem
g + ylab(paste('Power [', ']')) #No problem
g + ylab(expression(mu)) # No problem
g + ylab(expression(V^2)) # No problem

However, this seems not to be possible:

g + ylab(paste('Power [', expression(mu), expression(V^2), ']'))

The output does not evalute the expressions (mu and V^2):

ggplot2 Output:Problem y label

Where am I going wrong? Is the paste() command the wrong approach in general? I have also had a look at unicode characters (Unicode Characters in ggplot2 PDF Output) ... but that would still leave me with the question how I adequately combine all the single terms.

Your help is much appreciated!

like image 338
S.A. Avatar asked Jan 17 '16 10:01

S.A.


1 Answers

You need to put everything inside the expression. You can use an asterisk for separation, the tilde is for space. So this should be right.

g + ylab(expression("Power"~"["*mu*V^2*"]"))
like image 107
vanao veneri Avatar answered Oct 01 '22 12:10

vanao veneri