6

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!

Community
  • 1
  • 1
S.A.
  • 1,819
  • 1
  • 24
  • 39

2 Answers2

6

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*"]"))
vanao veneri
  • 970
  • 2
  • 12
  • 31
3

A single expression seems to do the trick:

g + ylab(expression(paste("Power [",mu, V^2,"]")))
Erwan LE PENNEC
  • 516
  • 3
  • 10