ylim problem in barplot

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

ylim problem in barplot

Bliese, Paul D LTC USAMH
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]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: ylim problem in barplot

Marc Schwartz (via MN)
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.

> Paul


Use:

  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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: ylim problem in barplot

Ben Bolker
In reply to this post by Bliese, Paul D LTC USAMH
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))

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: ylim problem in barplot

Ben Bolker
Ben Bolker <bolker <at> ufl.edu> writes:

>
> 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)
>

  hmm, replying to myself ...
  Now that I think about it, I don't know if the default behavior should
necessarily be to set the baseline at ylim[1] or xlim[1] ...  (i.e., you
can imagine setting ylim negative to allow more space
below the bars ... you could allow a "baseline" argument,
but this would then be ripe for abuse ...  perhaps this
discussion should move to r-devel, if anyone cares  ... )

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: ylim problem in barplot

Martin Maechler
In reply to this post by Bliese, Paul D LTC USAMH

>>>>> "PaulB" == Bliese, Paul D LTC USAMH <[hidden email]>
>>>>>     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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: ylim problem in barplot

Robert Baer
>     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.
>     PaulB> For instance the command produces a problematic graph.
>     PaulB> barplot(c(200,300,250,350),ylim=c(150,400))

> 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").

Well, consider this example:
barplot(c(-200,300,-250,350),ylim=c(-99,400))

It seems that barplot uses ylim and pretty to decide things about the axis
but does some slightly unexpected things with the bars themselves that are
not just at the 'zero' end of the bar.

Rob

____________________________
Robert W. Baer, Ph.D.
Associate Professor
Department of Physiology
A. T. Still University of Health Science
800 W. Jefferson St.
Kirksville, MO 63501-1497 USA

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: ylim problem in barplot

Ben Bolker
Robert Baer <rbaer <at> atsu.edu> writes:


> Well, consider this example:
> barplot(c(-200,300,-250,350),ylim=c(-99,400))
>
> It seems that barplot uses ylim and pretty to decide things about the axis
> but does some slightly unexpected things with the bars themselves that are
> not just at the 'zero' end of the bar.
>
> Rob
>

  in previous cases I think there was room for debate about
the appropriate behavior.  What do you think should happen
in this case?  Cutting off the bars seems like the right thing
to do; is your point that the axis being confined to positive
values (a side effect of setting ylim) is weird?

  Ben

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: ylim problem in barplot

Martin Maechler
>>>>> "Ben" == Ben Bolker <[hidden email]>
>>>>>     on Thu, 5 Jan 2006 19:21:48 +0000 (UTC) writes:

    Ben> Robert Baer <rbaer <at> atsu.edu> writes:
    >> Well, consider this example:
    >> barplot(c(-200,300,-250,350),ylim=c(-99,400))
    >>
    >> It seems that barplot uses ylim and pretty to decide things about the axis
    >> but does some slightly unexpected things with the bars themselves that are
    >> not just at the 'zero' end of the bar.
    >>
    >> Rob

no, there's no pretty() involved.  
Maybe it helps you to just type box()
after the plot.  Simply, the usual par("mar") margins are set.

I think ___in conclusion___  that Marc Schwartz'  solution has been
right on target all along:

>>>>   Use 'xpd = FALSE' if you set 'ylim' because otherwise, the
>>>>   result may be confusing.

The real "problem" of barplot.default() is the fact that
'xpd = TRUE' is the default, and AFAIK that's not the case
for other high-level plot functions.

One could debate if the default setting for xpd should not be changed to
 
   xpd = (is.null(ylim) && !horiz) || (is.null(xlim) && horiz)

Now this has definitely gotten a topic for R-devel, and not
R-help anymore.

    Ben> in previous cases I think there was room for debate about
    Ben> the appropriate behavior.  What do you think should happen
    Ben> in this case?  Cutting off the bars seems like the right thing
    Ben> to do;

    Ben> is your point that the axis being confined to positive values (a side effect of setting ylim) is weird?

    Ben> Ben

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: ylim problem in barplot

Jim Lemon-2
In reply to this post by Bliese, Paul D LTC USAMH
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html