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 ? Héctor [[alternative HTML version deleted]]
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 ? > > HctorI 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
There are 2 approaches that should work for you (actually there are probably more, but these 2 are what I would suggest). The first is to wrap barplot in your own function that also adjusts the parameters and adds the axis. The other is to use the output from subplot to go back and annotate the plots with the additional axis. Here is a quick example that shows both versions that you don't want (the top to barplots) and the 2 approaches I mention (the bottom 2). library(TeachingDemos) plot(0:10, 0:10, type='n') subplot( barplot(1:3, names=letters[1:3]), 2,8 ) op <- par(mgp=c(3,0,0)) subplot( barplot(1:3, names=letters[1:3]), 8,8 ) par(op) tmp.bar <- function(...){ op <- par(mgp=c(3,0,0)) barplot(..., yaxt='n') par(op) axis(2) } subplot( tmp.bar(1:3, names=letters[1:3]), 8,2 ) op <- par(no.readonly=TRUE) tmp <- subplot( barplot(1:3, names=letters[1:3], yaxt='n'), 2, 2, pars=list(mgp=c(3,0,0))) tmp$mgp = c(3,1,0) par(tmp) axis(2) par(op) Hope this helps (and that the pasted code is readable), ________________________________ From: r-help-bounces@stat.math.ethz.ch on behalf of Héctor Villalobos Sent: Tue 6/19/2007 2:31 PM To: r-help@stat.math.ethz.ch Subject: [R] axis labels in multiple plots 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 ? Héctor [[alternative HTML version deleted]] [[alternative HTML version deleted]]