[R] Centering lines on barplot centers.

gerald.jean at dgag.ca gerald.jean at dgag.ca
Mon Sep 12 21:33:45 CEST 2011


Hello,

I am trying to port one of my plotting S+ functions to R and I am having
difficulties!!!  I am including here only the troublesome code!

I first produce a barplot, saving the positions of the bar's centers.

  par(mar = c(6.1, 5.1, 4.1, 4.1), mgp = c(3, 3.0, 0))
  ticks.loc  <- barplot(sum.of.weights, col = 5, xlab = "", ylab = "",
                        axes = FALSE, axisnames = FALSE)
  pretty.bar <-pretty(c(0, sum.of.weights), 6)
  pretty.lab <- paste(pretty.bar, "%", sep = "")
  axis(side = 2, at = pretty.bar, labels = pretty.lab, col = 1, line = 0,
       cex.axis = 0.80, las = 2, mgp = c(3, 2, 0))
  my.axis(side = 1, at = ticks.loc, labels = plot.labels,
          las = 2, col = 1, adj = 1, cex.axis = labels.cex, mgp = c(2, 1,
0))
  box()
  title(main = titre, cex = title.cex)

Then I would like to plot three lines with the x-positions of the points on
the bar's centers.  In S+ I use "par(new = TRUE, xaxs = "d")", which
freezes the "x-axis" and that does the trick.  In R this option is not
supported, I tried to play with the user-coordinates, using the same ones
from one plot to the other but there is a slight offset in the location of
the points w.r. to the centers of the bar???

  user.corr <- par("usr")
  par(new = TRUE, mar = c(6.1, 5.1, 4.1, 4.1), mgp = c(3, 3.0, 0),
      usr = user.corr)
  matplot(x = ticks.loc,
          y = observed.means[, c("observed", "rebal.old", "rebal.new")],
          type = "b", lwd = 1.0, cex = 0.80, xlab = "", ylab = "",
          axes = FALSE)
  pretty.bar <- pretty(c(0, max(observed.means[, c("observed", "rebal.old",
                                                   "rebal.new")])))
  axis(side = 4, at = pretty.bar, line = 0, mgp = c(2, 2, 0), las = 2,
       col.axis = 1, cex.axis = 0.80)

Any suggestions???  Thanks in advance,

Gérald Jean
Conseiller senior en statistiques,
VP Actuariat et Solutions d'assurances,
Desjardins Groupe d'Assurances Générales
télephone            : (418) 835-4900 poste (7639)
télecopieur          : (418) 835-6657
courrier électronique: gerald.jean at dgag.ca


More information about the R-help mailing list