[R] axis labels in multiple plots

Marc Schwartz marc_schwartz at comcast.net
Tue Jun 19 23:06:34 CEST 2007


On Tue, 2007-06-19 at 14:31 -0600, Héctor Villalobos wrote:
> Hi,
> 
> I'am trying to make a multiple bar plot over a map and I'm having difficulties with the distance
> between axes labels and the axis.  Trying to control this with mgp does not help because it
> controls both axes simultaneously.  For example, with default values (mgp = c(3, 1, 0)) y-axis
> labels are ok, but x-axis labels are not. Setting mgp = c(3, 0, 0) gives good x-axis labels but
> the y-axis labels are over the axis.  Since I'm using subplot() from TechingDemos package I
> don't know how to pass the mgp argument for every axis (like : axis(2, mgp = c(3, 1, 0)).
> 
> I'm using R version 2.5.0 with Windows XP
> 
> 
> ##
> sim.data <- array(runif(420), dim = c(4, 5, 7, 3),
>    dimnames = list(paste("var", 1:4, sep = ""), paste("year", 1:5, sep = ""),
>    paste("lat", 1:7, sep = ""), paste("lon", 1:3, sep = "")) )
> x.pos <- c(3, 6, 9)
> y.pos <- c(1,2,3,4,5,6,7)
> 
> 
> ##  This will be the map, its empty in this example
>       plot(x = 1:10, y = 1:10, type = "n", xlim = c(1, 10), ylim = c(1,8) )
> 
> ##  And now the bar plots
>       for (l in 7:1) {
>          for (m in 1:3) {
> 
>       subplot(barplot(sim.data[, , l, m], las = 1, names.arg = paste("year", 1:5),
>           mgp = c(3, 0, 0), cex.axis = 0.7, cex.names = 0.7,),
>           x = x.pos[m], y = y.pos[l], size = c(1.3,0.5), vadj = 0 )
>         }
>       }
> 
> 
> Any hints ?
> 
> Hctor

I don't use that package or the functions, but it looks like from your
example above, that you might be able to create a modified barplot()
function and then call that in subplot(). 

For example:

mybarplot <- function(height, x.names, ...)
{
  mp <- barplot(height, axes = FALSE, ...)
  mtext(1, at = mp, text = x.names, line = 0)
  axis(2, las = 1, line = -0.75)
}


See ?mtext

Now contrast the spacing of the bar and axis labels:

 par(mfrow = c(2, 1))
 barplot(1:5, names.arg = paste("year", 1:5), las = 1)
 mybarplot(1:5, x.names = paste("year", 1:5))


If something like that works, you can then replace your call to
barplot() above with mybarplot() and adjust the other arguments as you
may require to achieve your desired result.

HTH,

Marc Schwartz



More information about the R-help mailing list