[R] Re: RE Vertical bars with barchart()

Deepayan Sarkar deepayansarkar at yahoo.com
Wed Mar 27 19:28:47 CET 2002


[I'mm cc-ing r-help on this, since some others might be interested (Tony 
Rossini is, at least)]

Yes, I saw your mail and was wondering about what would be the best approach 
to do it. It's definitely not trivial, mainly because Trellis is not designed 
to handle factors on the x-axis (chiefly for convenience, as factors typically
have long names which are difficult to write on the x-axis). 

Anyway, what you essentially need to do is 

1. Change the prepanel function so that you get the correct x and y axis
2. Change the panel function to draw what you want
3. Write the y- and x-axes labels explicitly

The first step is trivial if you use the default prepanel for barchart etc
(it's not very obvious, but barchart, dotplot etc all use a function called
prepanel.default.bwplot as its default prepanel function). The second is also 
easy by modifying panel.barchart. Unfortunately, the third step would need 
some case-specific tweaking.

There is also an easier way. It would be more transparent, and would 
work in more general cases (where, for instance, you could put more complicated

arguments in scales= etc). But, it's essentially a hack, because it uses the
(undocumented and liable to change) structure of a trellis object. Needless
to say, this would not work in S+ (though the first should).

I'm giving code for both approaches with the barley data (included in lattice) 
as an example. The rot= things are needed here because the names are long, you
might not need that.

#### The 'proper' approach


my.prepanel <- function(x, y, ...)
    foo <- prepanel.default.bwplot(x, y, ...)
    list(xlim = foo$ylim, ylim = foo$xlim)

my.panel <- function (x, y, box.ratio = 1, col = bar.fill$col,
                      levels.y = NULL, ...) 
    bar.fill <- trellis.par.get("bar.fill")
    xmin <- current.viewport()$yscale[1]#
    yscale <- current.viewport()$xscale#
    if (is.null(levels.y)) 
        levels.y <- floor(yscale[2]) - ceiling(yscale[1]) + 1
    height <- box.ratio/(1 + box.ratio)
    for (i in seq(along = x)) {
        grid.rect(gp = gpar(fill = col),
                  x = y[i], y = unit(0, "npc"),
                  width = height, height = x[i] - xmin,
                  just = c("centre", "bottom"),
                  default.units = "native")


barchart(variety ~ yield | year * site, data = barley) #usual barchart

barchart(variety ~ yield | year * site, data = barley,
         prepanel = my.prepanel, panel = my.panel,
         ylab = "yield", xlab = "",
         scales = list(x = list(at = 1:nlevels(barley$variety),
                       labels = levels(barley$variety),
                       rot = 30),
         y = list(at = lpretty(range(barley$yield)),
         labels = as.character(lpretty(range(barley$yield))

## hack : still need my.panel above. scales is just to 
## demonstrate flexibility

foo <- barchart(variety ~ yield | year * site, data = barley,
                scales = list(x=list(relation="free")),
                panel = "my.panel")

# switching appropriate components :
foo[c("x.limits", "y.limits", "x.scales", "y.scales",
      "xlab", "ylab")] <-
    foo[c("y.limits", "x.limits", "y.scales", "x.scales",
          "ylab", "xlab")]

# changing rot, since default of 0 would cause overlap
foo$x.scales$rot <- 90


PS: The ability to do this sort of thing is a very natural thing to ask for 
(in fact, Kurt had asked me about this quite some time ago), and I will try 
to integrate this into some future release. (The only real issue is the 
default placement of the x-axis labels.)

--- "Waichler, Scott R" <Scott.Waichler at pnl.gov> wrote:
> Deepayan,
> You may have noticed my posting to r-help about obtaining
> vertical bars with barchart().  I've gotten a couple of replies back
> suggesting the use of barplot().  I should have clarified that 
> I want to do this in a lattice.  I don't know how to do this 
> with the functions available with lattice.  
> Scott
> Scott Waichler, Senior Research Scientist
> Hydrology Group/Environmental Technology Division
> Battelle Pacific Northwest National Laboratory
> P.O. Box 999,  MSIN:  K9-36
> Richland, WA   99352
> (voice) 509-372-4423  (fax) 509-372-6089
> (email) scott.waichler at pnl.gov
> (web) http://hydrology.pnl.gov
> ________________________________________________


Yahoo! Movies - coverage of the 74th Academy Awards®

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