[R] parameter yaxs / function hist (graphics)

Marc Schwartz (via MN) mschwartz at mn.rr.com
Wed Aug 9 16:39:36 CEST 2006


Paulo,

Try the following:

x <- rnorm(100)

par(xaxs = "i")
par(yaxs = "i")

hist(x, breaks = seq(-4, 4, 0.5), ylim = c(0, 40),
     xlim = c(-4, 4))

box()


The problem is that graphics:::plot.histogram() is coded in such a way
that the use of 'xaxs' and 'yaxs' are ineffectual, as they are not
passed to the internal call to plot.window(), which does not provide for
'...' args in this case. So they cannot be passed 'inline' as you
attempted.

Thus, you need to set these prior to the plotting of the histogram.

BTW, a better option for the call to axis() relative to using the 'line'
argument, is to use the 'pos' argument and set it to 0:

x <- rnorm(100)

par(xaxs = "i")
par(yaxs = "i")

hist(x, breaks = seq(-4, 4, 0.5), ylim = c(0, 40),
     xlim = c(-4, 4), xaxt = "n")

axis(1, pos = 0)

box()

HTH,

Marc Schwartz

On Tue, 2006-08-08 at 18:49 -0300, Paulo Barata wrote:
> Dear Paulo,
> 
> Thank you for your reply. But I doubt yours is a proper
> solution to my request, for some reasons:
> 
> 1. The position of the axis graphed with the command axis(1, line=-1)
> depends on the size of the graphics window.
> 
> 2. After your graph is on the screen, in case one may want a boxed
> graph, a box() command will produce a histogram "floating in the air",
> not "touching" the horizontal axis.
> 
> Of course, one could build a proper box (with labels, etc.) by means
> of more primitive graphics functions like lines (package graphics)
> and others, but I think that would mean a lot of work.
> 
> Thank you again.

> Paulo Justiniano Ribeiro Jr wrote:
> > Paulo
> > 
> > One possibility is to draw the histogram without axes and then add them 
> > wherever you want.
> > 
> > For instance with something along the lines:
> > 
> > x <- rnorm(500)
> > hist(x, axes=F)
> > axis(1, line=-1)
> > 
> > For more details: ?axis
> > 
> > best
> > P.J.
> > > 
> > On Mon, 7 Aug 2006, Paulo Barata wrote:
> > 
> >>
> >> Dear R users,
> >>
> >> The parameters xaxs and yaxs (function par, package graphics)
> >> seem not to work with the function hist (package graphics),
> >> even when the parameters xlim and ylim are defined.
> >>
> >> Is there any way to make yaxs="i" and xaxs="i" work properly
> >> with the function hist, mainly to produce histograms that
> >> "touch" the horizontal axis? The R documentation and the
> >> R mailing lists archive don't seem to be of help here.
> >>
> >> I am using R 2.3.1, running under Windows XP.
> >>
> >> ## Example:
> >> x <- rnorm(100)
> >> hist(x,breaks=seq(-4,4,0.5),ylim=c(0,40),yaxs="i",
> >>   xlim=c(-4,4),xaxs="i")
> >> box()
> >>
> >> Thank you very much.
> >>
> >> Paulo Barata
> >>



More information about the R-help mailing list