1
xlab(expression(paste("CO"^"2", " concentration", "\n Lolium perenne")))

This is my current coding. With my xlab I want to have CO^2 concentration on one line, and then "Lolium perenne" beneath that but it also needs to be in italics. Currently this code places "concentration" on the line above "CO^2" and "Lolium Perenne". Please help!

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104
  • Do provide a reproducible example. See [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to create one. – mnm Dec 21 '17 at 02:43
  • 2
    Newlines are not possible using `expression(...)` natively, but a workaround is to use `expression(atop(CO^2*" concentration", italic("Lolium perenne")))` – Brian Dec 21 '17 at 03:32
  • I have provided an answer with a reproducible example. Feel free to copy into your question. I didn't want to completely rewrite your question. I have also included @Brian's suggestion as the answer. In general, on SO, we want helpful answers in the "Answer" part, not as comment. Comments are open to deletion at any time. – Claus Wilke Dec 21 '17 at 18:59

1 Answers1

2

Let's turn this into a reproducible example and then answer, using the approach @Brian suggested.

The following doesn't work. The x axis title is all on one line:

library(ggplot2)
set.seed(124)
d <- data.frame(x = rnorm(50),
                y = rnorm(50))

ggplot(d, aes(x, y)) + geom_point() +
  xlab(expression(paste("CO"^"2", " concentration", "\n Lolium perenne")))

enter image description here

However, using the atop() function, we can obtain this result:

ggplot(d, aes(x, y)) + geom_point() +
  xlab(expression(atop(CO[2]*" concentration", italic("Lolium perenne"))))

enter image description here

We have typeset "Lolium perenne" in italics, using the italic() function. We have also written CO2 with a subscript, which is presumably what is needed here.

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104
  • I didn't post it as an answer because I think it's a workaround with too many drawbacks to be generally recommendable. Trying to solve that sort of problem has been on my backburner for a while. Thanks for your effort. (Also now that I see it rendered, they should be `CO[2]`, not `CO^2`). – Brian Dec 22 '17 at 00:09
  • I made the change to `CO[2]`. – Claus Wilke Dec 22 '17 at 00:17