# [R] Constructing bar charts with standard error bars

Ben Bolker bolker at zoo.ufl.edu
Thu Jul 26 21:46:57 CEST 2007

```John Zabroski wrote:
> On 7/25/07, Ben Bolker <bolker at ufl.edu> wrote:
>
> Thanks a lot!  I tried all three and they all seem very dependable.
> Also, I appreciate you rewriting my solution and adding elegance.
>
> Is there a way to extend the tick marks to the ylim values, such that
> the yscale ymax tickmark is something like max(xbar+se)?  In the
> documentation, I thought par(yaxp=c(y0,y1,n)) would do the trick, but
> after trying to use it I am not sure I understand what yaxp even does.

It took me quite a while to figure this out, I'm not surprised you
didn't ...

The very easiest way to do this is simply to set ylim to (0,0.4) -- since
you probably want to extend the axes upward to a "pretty" number
anyway.

The other standard way to do this is to use barplot with axes=FALSE and
then add the axes yourself, with the ticks specified wherever you want:

barplot(...,ylim=c(0,0.4),axes=FALSE)
axis(side=1)
axis(side=2,at=seq(0,0.4,length=8))

However, I was wondering what was up with yaxp, and why setting
it didn't seem to do anything.  The answer is lurking in ?par:

##     This parameter is reset when a user coordinate system is set
##           up, for example by starting a new page or by calling
##           'plot.window' or setting 'par("usr")': 'n' is taken from
##           'par("lab")'.  It affects the default behaviour of subsequent
##           calls to 'axis' for sides 1 or 3.

Thus, when barplot starts up and plots a new set of axes it RESETS
par("yaxp").  Thus

par(yaxp=...)); barplot(...)

doesn't work.

However,

barplot(...,yaxp=...)  does work.

It would actually be nice to have an axis style (xaxs,yaxs) that extended
the axis out beyond the range of the data until it found pretty labels that
extended beyond the data range -- for example, set the range according
to xaxs="r", find the pretty axis ticks, and then "add another tick" ...

cheers
Ben Bolker

```