[R] ylim problem in barplot
Ben Bolker
bolker at ufl.edu
Thu Jan 5 14:54:59 CET 2006
Bliese, Paul D LTC USAMH <paul.bliese <at> us.army.mil> writes:
>
> R Version 2.2.0
>
> Platform: Windows
>
> When I use barplot but select a ylim value greater than zero, the graph
> is distorted. The bars extend below the bottom of the graph.
>
The problem is that barplot() is really designed to work
with zero-based data. I don't know if the Powers That Be
will say that "fixing" this would violate the spirit of
barplot (although I see there is some code in barplot
that deals with figuring out the base of the rectangle
in the logarithmic case, where 0 obviously doesn't work)
Here's a workaround:
barplot(c(200,300,250,350)-150,axes=FALSE)
axis(side=2,at=seq(0,200,by=50),labels=seq(150,350,by=50))
And here's a diff: if you want to hack barplot yourself,
sink("newbarplot.R")
barplot.default
sink()
## go edit newbarplot.R; add barplot.default <- to
## the first line, remove the namespace information
## from the last line, and substitute the lines
## in the first chunk below with exclamation points for
## the lines in the second chunk below with exclamation
## points
source("newbarplot.R")
cheers
Ben
*** newbarplot2.R 2006-01-05 08:52:11.000000000 -0500
--- /usr/local/src/R/R-2.2.1/src/library/graphics/R/barplot.R 2005-10-06
06:22:59.000000000 -0400
***************
*** 85,97 ****
if (logy && !horiz && !is.null(ylim)) ylim[1]
else if (logx && horiz && !is.null(xlim)) xlim[1]
else 0.9 * min(height)
! } else {
! rectbase <- if (!horiz && !is.null(ylim))
! ylim[1]
! else if (horiz && !is.null(xlim))
! xlim[1]
! else 0
! }
## if stacked bar, set up base/cumsum levels, adjusting for log scale
if (!beside)
height <- rbind(rectbase, apply(height, 2, cumsum))
--- 85,92 ----
if (logy && !horiz && !is.null(ylim)) ylim[1]
else if (logx && horiz && !is.null(xlim)) xlim[1]
else 0.9 * min(height)
! } else rectbase <- 0
!
## if stacked bar, set up base/cumsum levels, adjusting for log scale
if (!beside)
height <- rbind(rectbase, apply(height, 2, cumsum))
More information about the R-help
mailing list