[R] abline() plot order
Marc Schwartz
MSchwartz at medanalytics.com
Thu Aug 7 18:41:20 CEST 2003
On Thu, 2003-08-07 at 11:07, Al Piszcz wrote:
> I am performing this sequence
>
> barplot
> title
> legend
> abline
>
> When abline renders the lines they appare to be in the layer
> above the bars in the graph. Is there a way to make them
> render first or 'behind' the bars?
>
> Thanks.
Are you using abline() for a single line or for a grid?
barplot2() in the 'gregmisc' package on CRAN can help with either
scenario.
If you want a grid behind the bars, use 'plot.grid = TRUE' in barplot2.
The default will draw thin dotted lines behind the bars at each axis
tick mark. You can specify the line type and width for the grid as well.
Example:
barplot2(1:5, plot.grid = TRUE)
If you want a single line (ie. a benchmark), you could use barplot2()
the first time to draw the chart, use abline() to draw your single line
and then use barplot2() again, with the 'add = TRUE' argument, which
would re-draw the bars over the existing plot.
Example:
barplot2(1:5)
abline(h = 2)
barplot2(1:5, add = TRUE)
An alternative to using barplot2() would be to use par(new = TRUE) as
well. This will enable you to add a new plot to the existing figure.
First case:
barplot(1:5)
abline(h = axTicks(2), lty = "dotted")
par(new = TRUE)
barplot(1:5)
Second case:
barplot(1:5)
abline(h = 2)
par(new = TRUE)
barplot(1:5)
See ?axTicks and ?par for additional information.
I'll make a note to myself to add an option to barplot2 for a single
line that can be added behind the bars. Pretty easy fix.
HTH,
Marc Schwartz
More information about the R-help
mailing list