Hi folks, I'm using R 2.5.0 under ESS under Windows XP. (This also happens using the Rgui application.) I'm trying to add lines to a plot originally made with "boxplot", but the lines appear in the wrong place. Below is a script that illustrates the problem # boxablinetest.R - script to show problem with abline on box plot x <- c( 2, 2, 2, 3, 3, 3, 4, 4, 4) y <- c( 1, 2, 3, 2, 3, 4, 3, 4, 5) xymodel <- lm( y~x) boxplot( y~x) abline( xymodel) # Wrong abcissae abline( v = 2.5) # Wrong abcissa abline( h = 2.75) # Right ordinate # -------------- end -------------- Here, I'm making a box plot with abscissae that start at 2. The box plot looks fine: the numbers 2, 3, and 4 appear on the x-axis and the boxes are centered at 2, 3, and 4. When I add the first abline, the line appears too low, but actually it is too far to the right. The abscissae are being interpreted without realizing that the plot originates at 2, not 1. The second call to abline should put a vertical line between 2 and 3, but instead it shows up between 3 and 4. Again, it appears that the offset in the origin of the boxplot is not accounted for. Finally the last abline appears where it should: between 2 and 3. Evidently, ordinate values are correctly interpreted. Does anyone have any advice? Thanks very much. Brian Wilfley
Brian Wilfley said the following on 6/21/2007 2:44 PM:> Hi folks, > > I'm using R 2.5.0 under ESS under Windows XP. (This also happens using > the Rgui application.) > > I'm trying to add lines to a plot originally made with "boxplot", but > the lines appear in the wrong place. Below is a script that > illustrates the problem > > # boxablinetest.R - script to show problem with abline on box plot > > x <- c( 2, 2, 2, 3, 3, 3, 4, 4, 4) > y <- c( 1, 2, 3, 2, 3, 4, 3, 4, 5) > > xymodel <- lm( y~x) > > boxplot( y~x) > abline( xymodel) # Wrong abcissae > abline( v = 2.5) # Wrong abcissa > abline( h = 2.75) # Right ordinate > > # -------------- end -------------- > > Here, I'm making a box plot with abscissae that start at 2. The box > plot looks fine: the numbers 2, 3, and 4 appear on the x-axis and the > boxes are centered at 2, 3, and 4. > > When I add the first abline, the line appears too low, but actually it > is too far to the right. The abscissae are being interpreted without > realizing that the plot originates at 2, not 1. > > The second call to abline should put a vertical line between 2 and 3, > but instead it shows up between 3 and 4. Again, it appears that the > offset in the origin of the boxplot is not accounted for. > > Finally the last abline appears where it should: between 2 and 3. > Evidently, ordinate values are correctly interpreted. > > Does anyone have any advice? > > Thanks very much. > > Brian Wilfley > > ______________________________________________ > R-help at stat.math.ethz.ch 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.That's because the x is converted to a factor (see ?boxplot). Here's what you want: ## changesd x==3 to 6 to demonstrate call to boxplot below x <- c(2, 2, 2, 3, 3, 3, 6, 6, 6) y <- c(1, 2, 3, 2, 3, 4, 3, 4, 5) xymodel <- lm(y ~ x) boxplot(y ~ x) ## note x-labels! ## now fix your problem plot(y ~ x, type = "n", xlim = c(1, 7)) bxp(boxplot(y ~ x, plot = FALSE), at = c(2, 3, 6), add = TRUE, boxwex = 0.5, boxfill = "lightblue") abline(xymodel) abline(v = 2.5) abline(h = 2.75) HTH, --sundar
Boxplot positions and labels are not the same thing. You have groups 'called' "2", "3", "4". As factors - which is what bocplot will turn them into - they will be treated as arbitrary labels and _numbered_ 1:3 (try as.numeric(factor(x)). So your lm() used 2:4, but your plot (and abline) uses 1:3 for positions and "2" - "4" as labels. The best option used to be to plot the boxes at positions 2:4. Look at the at= parameter in boxplot. But that is now of little help because there is no way of overriding xlim, leaving you no alternative but to reformulate your model with an offset or something. I will take up the boxplot xlim issue separately on R-dev; it's not the only such. Steve Ellison.>>> "Brian Wilfley" <bwilfley at tripleringtech.com> 21/06/2007 22:44:17 >>>I'm trying to add lines to a plot originally made with "boxplot", but the lines appear in the wrong place. ******************************************************************* This email and any attachments are confidential. Any use, co...{{dropped}}