Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Baseline alignment of axis labels

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

like image 597
Manuel Weinkauf Avatar asked Aug 26 '15 13:08

Manuel Weinkauf


People also ask

How do I align axis labels?

Tip You can also change the horizontal alignment of axis labels, by right-clicking the axis, and then click Align Left , Center , or Align Right on the Mini toolbar.

Can you format axis labels?

To change the format of text in category axis labels: Right-click the category axis labels you want to format, and click Font. On the Font tab, choose the formatting options you want.


1 Answers

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

like image 156
A. Webb Avatar answered Sep 28 '22 16:09

A. Webb