[R] Possible x-axis label bug in barplot() when "plot = F"

Uwe Ligges ligges at statistik.uni-dortmund.de
Tue May 21 10:01:54 CEST 2002


Marc Schwartz wrote:
> 
> > The help page says "plot: logical. If FALSE, nothing is plotted."
> > barplot() just calculates the midpoints it would use in the case of
> > plot=TRUE.
> >
> > In your second example a device is opened by plot.new(), which has the
> > default user coordinates par("usr")
> > [1] -0.04  1.04 -0.04  1.04
> > and the result of your call to axis() is expected.
> >
> > So plot.new() is useless in either of your examples. In the first one
> > barplot() generates a new plot anyway, in the second example it makes
> > axis() work on a way, you don't want it to work ...
> >
> > The real question (at least for me) is: What are you going to do with
> > your code?
> >
> > Uwe Ligges
> 
> Uwe,
> 
> Thanks for your reply.
> 
> I am attempting to parallel (using barplot() ) an example in the
> demo(graphics) code which enables the plot region to be colored
> differently than the margins and allows for flexibility in defining the
> axis labels, etc.
> 
> The demo code (which is for the "Level of Interest in R" line graphic)
> shows that the plot region can be colored differently by setting the
> background color using par(bg = "color") and then calling plot(x, type =
> "n", axes = FALSE, ann = FALSE) so that nothing is actually plotted.
> 
> I presume that this sets up the coordinate system for the graphic, such
> that subsequent graphic components can then be placed within the same
> relative coordinate system area. Perhaps that is not a valid presumption
> on my part.
> 
> A call is then made to rect() using corner positions from par("usr"),
> which allows for the plot region to be filled with a different color
> than the margin.
> 
> Subsequent calls are made to plot the data points, lines, titles, etc.
> so that they do not overwrite the newly colored plot region.
> 
> I was trying to parallel this using barplot() by doing the following
> (ignore the color combinations for now):
> 
> > par(bg = "blue")
> > midpts <- barplot(1:10, axes = F, axisnames = F)
> > usr <- par("usr")
> > rect(usr[1], usr[3], usr[2], usr[4], col = "white")
> > abline(h = 1:10, lty = "dotted")
> > rect(midpts - .4, 0, midpts + .4, 1:10, col = "yellow")
> > axis(1, at = midpts, col.axis = "white")
> > axis(2, 1:10, col.axis = "white", las = 1)
> > title("This Is A Test\nShowing Different Plot Region Color", col.main
> = "yellow")
> 
> The above code actually works fine in terms of the final result for
> demonstration purposes to give you a sense of what I am trying to do.  I
> was just attempting to parallel the aforementioned plot() code so as to
> not actually plot the graphic, overwrite it with the first call to
> rect() and then draw subsequent components individually.
> 
> Was I wrong to attempt to take the same approach?
> 
> I actually get errors on subsequent graphic calls, if I try to use
> barplot(...., plot = F) first, without having first called plot.new().
> 
> Again, perhaps I am missing something fundamental here.


Sorry for answering so late. I was quite busy the during last days.
I don't know exactly why the argument plot=FALSE was introduced to
barplot(), obviously the author only tried to get the midpoints, but not
the behaviour of plot() with argument type="n".

Nevertheless, it is possible to extend barplot() to get the desired
behaviour.
Since I have not the time to try it out, let's see it as an exercise:
Change the following lines in barplot.default() from

  if (plot) {
        opar <- if (horiz) 
            par(xaxs = "i", xpd = xpd)
        else par(yaxs = "i", xpd = xpd)
        on.exit(par(opar))
        plot.new()
        plot.window(xlim, ylim, log = "", ...)
        xyrect <-  ...

to
   opar <- if (horiz) 
      par(xaxs = "i", xpd = xpd)
   else par(yaxs = "i", xpd = xpd)
   on.exit(par(opar))
   plot.new()
   plot.window(xlim, ylim, log = "", ...)
   if (plot) {
    xyrect <-  ...

i.e. just open a new device and set up the window dimensions in any case
(even with plot=FALSE).
Whether it is a bug or a feature is not clear to me ...

Uwe Ligges
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list