[R] Centering lines on barplot centers.
Greg.Snow at imail.org
Mon Sep 12 21:48:55 CEST 2011
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
greg.snow at imail.org
> -----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.
> I am trying to port one of my plotting S+ functions to R and I am
> 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 =
> 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,
> 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
> from one plot to the other but there is a slight offset in the location
> 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",
> type = "b", lwd = 1.0, cex = 0.80, xlab = "", ylab = "",
> axes = FALSE)
> pretty.bar <- pretty(c(0, max(observed.means[, c("observed",
> 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
> PLEASE do read the posting guide http://www.R-project.org/posting-
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help