Lasse Kliemann
2009-Oct-23 22:00 UTC
[R] Distance between axis and label adjusted automatically?
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. Thank you Lasse -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 835 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20091024/0b14559f/attachment-0002.bin>
Jim Lemon
2009-Oct-24 09:12 UTC
[R] Distance between axis and label adjusted automatically?
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