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. For instance the command produces a problematic graph. barplot(c(200,300,250,350),ylim=c(150,400)) Any help would be appreciated. Paul [[alternative HTML version deleted]]
On Thu, 2006-01-05 at 14:01 +0100, Bliese, Paul D LTC USAMH wrote:> 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.> For instance the command produces a problematic graph.> barplot(c(200,300,250,350),ylim=c(150,400))> Any help would be appreciated.> PaulUse: barplot(c(200, 300, 250, 350), ylim = c(150, 400), xpd = FALSE) The 'xpd = FALSE' will enable clipping of the graphic at the boundary of the plot region. See ?par for more information on 'xpd'. HTH, Marc Schwartz
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))
>>>>> "PaulB" == Bliese, Paul D LTC USAMH <paul.bliese at us.army.mil> >>>>> on Thu, 5 Jan 2006 14:01:17 +0100 writes:PaulB> R Version 2.2.0 PaulB> Platform: Windows PaulB> When I use barplot but select a ylim value greater PaulB> than zero, the graph is distorted. The bars extend PaulB> below the bottom of the graph. Well, my question would be if that is not a feature :-) Many people would consider barplots that do not start at 0 as "Cheating with Graphics" (in the vein of "Lying with Statistics"). PaulB> For instance the command produces a problematic graph. PaulB> barplot(c(200,300,250,350),ylim=c(150,400)) The advantage of the current graphic drawn is that everyone *sees* that the bars were cut off {and that one should really think twice before producing such cheating graphics.. :-)} plot(c(200,300,250,350), ylim=c(150,400), type = "h", lwd=20, xaxt="n", col="gray") produces something closer to what you like. [yes, you can get rid of the roundedness of the thick-line ends; --> ?par and look for 'lend'; --> op <- par(lend = 1) ; plot(.........) ; par(op) In R-devel (i.e. from R 2.3.0 on) you can even say plot(c(200,300,250,350), ylim=c(150,400), type = "h", lwd=20, xaxt="n", col="gray", lend = 1) ] But after all, I tend to agree that R should behave a bit differently here, e.g., first giving a warning about the non-approriate ylim but then still obey the ylim specification more nicely. Regards, Martin Maechler
Bliese, Paul D LTC USAMH wrote: > > 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. Have a look at the gap.barplot function in the plotrix package. A new version (2.0.1) has just been uploaded and should turn up soon. Jim