5

This might be easy to solve, but I did not find anything with google, maybe because the is hard to put into words. When running the following code

Dat<-matrix(c(1:10, 1:10, seq(0.1, 1, 0.1)), 10, 3)
par(mfrow=c(1, 2))
plot(Dat[,1], Dat[,2], ylab="Y", xlab=expression(paste("A unit (", m^2, ")", sep="")))
plot(Dat[,1], Dat[,3], ylab="Y", xlab="A unit")

I get a nice plot, but the x-axis labels are not on the same baseline. The reason is that the label of the left plot has a parentheses that reaches below the baseline of the text, and R is plotting labels such that the lowest point of the text string is taken as the baseline when using expression() in the xlab-command. Hence the "A unit" in the right plot is printed further down than in the left plot (see example image, indicated by red line). That is annoying if some of the labels have parentheses or letters with parts that reach below the baseline (like "g", "q", etc.) and others don't. Is there any way to fix that?

enter image description here

1 Answer 1

5

I may be wrong, but I do not think this occurs with normal text, e.g. I don't see it with labels of plain character vectors "qqq" and "ooo", which appear to be aligned to properly at their baseline. I also suspect that alignment issues with plotmath expressions is somewhat device dependent.

Plotmath does not have the sophistication of (La)Tex, so alignment to bounding box rather than baseline is a "feature". You can, of course, nudge position manually by fiddling with par settings or using mtext. In many cases, however, such as this one, you can achieve alignment by setting phantom characters to match. For example, changing the second plot to the following in your example produces satisfactory results:

plot(Dat[,1], Dat[,3], ylab="Y", 
  xlab=expression(paste(phantom('('),"A unit",phantom(')'))))

phantom parentheses

1
  • 1
    Thanks a lot, that does the trick. And yes, you are right, this problem only occurs when using expression(). A normal xlab="A unit (m)" and also xlab=paste("A unit (", "m", ")", sep=") are perfectly fine without such tricks. Commented Aug 26, 2015 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.