[R] Distance between axis and label adjusted automatically?

Jim Lemon jim at bitwrit.com.au
Sat Oct 24 11:12:45 CEST 2009


On 10/24/2009 09:00 AM, Lasse Kliemann wrote:
> Is there a way to have the distance between label and axis
> adjusted automatically? This is interesting in particular for the
> y-axis, when it is not known in beforehand how many digits the
> numbers will have. It may happen then, that numbers and label
> overlap.
>
> All examples that I found work by estimating the required
> distance manually in beforehand and then providing large enough
> margins, or positioning the label far enough away from the axis.
>
> I have a series of plots, that should be done automatically. They
> have different magnitudes in y-direction. So there is no
> universal estimation that looks good in all cases -- of course I
> can provide a "safety margin", but it looks stupid when there is
> too much space between label and axis.
>
> I tried to use the relative positioning mechanism of frames,
> roughly like this:
>
>    fb<- packGrob(fg,yaxisGrob(vp="dataRegion"),side="left")
>    fg<- packGrob(fg,textGrob("y axis label",rot=90),side="left")
>
> But it appears that the true size of the axis is not respected;
> numbers on the axis and the label still may overlap.
>
> Can anyone provide a simple example how to do this right? I would
> favor a solution using the 'grid' package.
>
>    
Hi Lasse,
You can see one way to do this in the getMarginWidth function code 
(plotrix). This calculates the longest label and then adjusts the margin 
accordingly. I don't think it would be too hard to adapt the approach to 
grid graphics.

Jim




More information about the R-help mailing list