Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R alignment of axis labels with expressions

Tags:

plot

r

I would like to plot a variable name and its symbol. Because some variables have long names, I'm attempting to mix line breaks with the axis labels. This causes funny things to happen in the alignments:

par(mar=c(1,12,1,1))

plot( y=1:6, 1:6, yaxt="n", ylim=c(1,6), ylab="" )

axis(2, at=6:1, las=1,
     labels=c(
  expression( paste( 'Variable, ',rho, sep="" ) ),
  expression( paste( 'Another variable,\n',alpha, sep="" ) ),
  expression( paste( 'Also\nvariable,\n',beta, sep="" ) ),
  expression( paste( 'This\nvariable too, ',Gamma, sep="" ) ),
  expression( paste( 'Verybigname\nvariable,',C[zeta], sep="" ) ),
  expression( paste( 'Verybigname\nvariable,',C[zeta],"moretext\n",C[delta], sep="" ) ) ))

The first line (from the top) is what I expected the others to look like in some way, which is justified next to the axis.

The rest of the lines all show the basic problem that the variable name is justified with respect to the second element of the expression, not to the axis. This gets particularly weird in the last line where a large space is inserted between the variable name and its symbol.

Ideally, I would like the labels right justified, and centered on the tick mark.

like image 790
noname Avatar asked Mar 03 '16 20:03

noname


2 Answers

This is the best I can do with using text with positioning on the side "by hand". (Many efforts with axis using 'hadj' and 'padj' failed.) To try to get the plotmath greeks under the text values, I needed to use the atop function. This in turn required using phantom. I mostly use pure plotmath with ~ and * as connectors rather than relying on paste, but here your use of "\n" does acomplish a line break (which I found surprising). By the way, the plotmath paste function does not actually accept a sep argument.

par(mar=c(1,12,1,1))
plot( y=1:6, 1:6, yaxt="n", ylim=c(1,6), ylab="" )

axis(2, at=6:1, las=1,
     labels=rep("",6 ))
text( 0.5, 6:1- c(0,0,.2,.1,.1,.1), c(
   expression( Variable~~rho ) ,
   expression( atop(~~Another~variable, phantom("Another~variable")~alpha)  ),
   expression( atop(paste( '        Also\n   variable') ,phantom("variable")~beta ) ),
   expression( atop( paste( '            This\nvariable too') ,phantom("           ")~Gamma ) ),
   expression( atop( paste( '   Verybigname\n            variable,'),phantom("  Verybigname")~C[zeta] ) ),
   expression(  paste( '   Verybigname\nvariable,',C[zeta],"moretext\n",C[delta] ) ) ), 
xpd=TRUE ,adj=1)

enter image description here

like image 66
IRTFM Avatar answered Sep 20 '22 11:09

IRTFM


A solution with ggplot2, using atop for multi-line.
It will automatically position the labels as you want

library(ggplot2)
DF <- data.frame(x = 1:6, y = 1:6)
labels = c(
  expression( Variable~~rho ) ,
  expression( atop(~~Another~variable, phantom("Another~variable")~alpha)  ),
  expression( atop(paste( '        Also\n   variable') ,phantom("variable")~beta ) ),
  expression( atop( paste( '            This\nvariable too') ,phantom("           ")~Gamma ) ),
  expression( atop( paste( '   Verybigname\n            variable,'),phantom("  Verybigname")~C[zeta] ) ),
  expression(  paste( '   Verybigname\nvariable,',C[zeta],"moretext\n",C[delta] ) ) )

ggplot(DF, aes(x = x, y, y = y)) +
geom_point() +
scale_y_continuous(breaks = 1:6, labels = rev(labels))

ggplot and plotmath labels

I think it is possible to make a fonction that wrap mixed text-plotmath labels, but it seems more complicated that manually create your labels.

like image 37
cderv Avatar answered Sep 20 '22 11:09

cderv