1

I would like to create a plot in ggplot2, where the xaxis is |r|, meaning the absolute value of r. Based on How to use Greek symbols in ggplot2? I've found that I can use the following to get the greek symbol beta, but how I do get the absolute value bars?

p <- p + xlab(expression(beta))

Community
  • 1
  • 1
pir
  • 5,513
  • 12
  • 63
  • 101

1 Answers1

5

Use paste inside expression to add in the pipes, |.

xlab(expression(paste("|", beta, "|")))

Which would look something like: enter image description here

Edit to show how to change fonts

If you're not using greek letters, you don't necessarily need expression. You can control the family and face of the letters via element_text in `theme. You just need to figure out what you are going for.

Here's one way to change the family and font:

xlab("| r |") + 
theme(axis.title.x = element_text(family = "serif", face = "italic"))

To do the same thing using expression:

xlab(expression(italic("| r |"))) + 
theme(axis.title.x = element_text(family = "serif"))

enter image description here

aosmith
  • 34,856
  • 9
  • 84
  • 118
  • Thanks for the quick response! I'm not using it for a greek letter, but for a normal letter. Is there a way to make it look more math-like? For instance, like the label in this plot: http://imgur.com/5vGZ8MW – pir Nov 19 '15 at 20:48
  • 2
    @felbo Maybe you want italics and/or a different font? You could try playing around with fonts a la `+ theme(axis.title.x = element_text(family = "serif"))`. You can do italics inside expressions, as well - try just `italic("| r |")` in `expression`. – aosmith Nov 19 '15 at 21:53