Saturday, August 20, 2011

R - par() change the position of axes labels and tick marks

  • create a example plot using the trees data set
head(trees)
  Girth Height Volume
1   8.3     70   10.3
2   8.6     65   10.3
3   8.8     63   10.2
4  10.5     72   16.4
5  10.7     81   18.8
6  10.8     83   19.7
  • now we create the plot (girth on the x-axis, heigth on the y-axis, size and color of the circles indicating the volume of the trees):
my.col <- rainbow(100, start=0.3,end=0.9) ## provid an vector with colors
with(trees, symbols(Girth,Height, circles=Volume/30, bg=my.col[Volume], inches=0.3)) ## plot the example plot (symbols plot) 

  • now we save the current par()-settings (for recovering) - and so we can restore it later
my.old.par <- par()
  • the mgp controls the position of the axes labels, and tick labels and the axes
  • default value c(3,1,0)
  • the first entry controls the position of the axis labels, the second the position of the tick labels
par(mgp=c(-2,-1,0))
with(trees, symbols(Height, Girth, circles=Volume/30, bg=my.col[Volume], inches=0.3, main="Trees") )

  • so if we change the first two values of mgp to negative numbers the axis labels and the tick labels are now located in the inner region
  • the absolute value define the distances, so we try this second example:
par(mgp=c(-2,-3,0))
with(trees, symbols(Height, Girth, circles=Volume/30, bg=my.col[Volume], inches=0.3, main="Trees") )

  • the third element of mgp controls the axes and it have the same effect, every value different from zero moves the axes
par(mgp=c(-2,-3,-1))
with(trees, symbols(Height, Girth, circles=Volume/30, bg=my.col[Volume], inches=0.3, main="Trees") )

my.col <- rainbow(100, start=0.3,end=0.9) ## provid an vector with colors
with(trees, symbols(Height, Girth, circles=Volume/30, bg=my.col[Volume], inches=0.3)) ## plot the example plot (symbols plot) 
  • if you want to change just the orientation of the ticks use tcl
  • tcl defines the length of tick marks as a fraction of the height of a line of text
  • default ist -0.5
  • if you use a positive value the orientation is changed
par(mgp=c(3,1,0),tcl=0.5) ### set mgp back to default, change tcl
with(trees, symbols(Height, Girth, circles=Volume/30, bg=my.col[Volume], inches=0.3, main="Trees") )

No comments :

Post a Comment