Line-break and superscript in axis label ggplot2 R

Viewed 946

I am trying to plot a graph in ggplot2. The label on y axis should include a line break and text in superscript, and should be centered. It should look like:

"Density of mobile invertebrates (indiv. m^-2)"

The line break should be after "mobile" and the "per square m" should be in superscript. I've tried

p+ ylab(bquote("Density of mobile\ninvertebrates"(indiv.~m^-2)))

but it is not centered and there's a funny space before the "(indiv.~m^-2)"

1 Answers

You could use expression() and atop() to autoformat white space.

ggplot(data,aes(x=x,y=y)) + 
   geom_point() + 
   xlab( expression(atop("Density of mobile",paste("invertebrates (ind.~",m^{2},")"))))

enter image description here

Data

set.seed(1) 
data <- data.frame(x = 1:10, y = 1:10 + runif(-1,1,n=10))
Related