Setting par(usr=something) does not survive the creating of a new high level
plot. Using par(new=TRUE) is to be avoided if at all possible (it just leads to
problems like yours), it would be better for you to use matlines instead of
matplot which adds lines to the current plot.
If you want to set the limits for a new plot to exactly match the previous one
use the xlim argument along with xaxs='i' (or an additional buffer will
be added again).
If you really need to adjust the range of one of the axes to combine the plots
(not recommended, this can cause confusion and emphasize meaningless crossings,
it is better to juxtapose two aligned plots) then consider using the updateusr
function in the TeachingDemos package.
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of gerald.jean at dgag.ca
> Sent: Monday, September 12, 2011 1:34 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] Centering lines on barplot centers.
>
>
> 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
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.