s-news
[Top] [All Lists]

Re: barchart in Trellis

To: s-news@lists.biostat.wustl.edu
Subject: Re: barchart in Trellis
From: Stephen Kaluzny <skaluzny@tibco.com>
Date: Wed, 15 Jul 2009 16:17:45 -0700
In-reply-to: <4A5B3085.2080005@earthlink.net>
References: <4A5B3085.2080005@earthlink.net>
User-agent: Mutt/1.4.2.2i
On Mon, Jul 13, 2009 at 09:03:01AM -0400, Data Analytics Corp. wrote:

> I have a simple question.  I drew a bar chart in trellis that I would 
> like to redo.  There are six panels.  Each panel has six bars 
> representing six food products.  The panels are the attributes of the 
> food products: smell, sweetness, etc.  The scale in each panel ranges 
> from -60% to +20% and represent a net score - basically the percentage 
> of people favoring the attribute or not.  A negative score means more 
> people disliked the attribute; positive means more liked it.  The bars 
> all start from the left vertical axis making interpretation slightly 
> challenging.  I'd like to have the bars start in the center of each 
> panel at the 0% point so that bars extending to the right clearly show 
> positive percentages of people and those to the left clearly show 
> negative ones.  The command I used is
> 
> barchart(code ~ net | att, data == all.net, aspect = 1)
> 
> where code is for the product, net is for the percentage scores, and att 
> is for the attributes.

A quick thought is to use a dotplot instead of a barchart. The dotplot
allows for negative values without distorting the origin. You can add a
vertical line at zero in each panel if that is appropriate using a
modified panel.dotplot e.g.

"mypanel.dotplot"<-
function(x, y, pch = dot.symbol$pch, col = dot.symbol$col, cex = dot.symbol$cex,
        font = dot.symbol$font, ...)
{
        ok <- !is.na(x) & !is.na(y)
        dot.symbol <- trellis.par.get("dot.symbol")
        dot.line <- trellis.par.get("dot.line")
        abline(h = unique(y[ok]), lwd = dot.line$lwd, lty = dot.line$lty, col
                 = dot.line$col)
        points(x, y, pch = pch, col = col, cex = cex, font = font, ...)
        # add vertical line at zero:
        abline(v = 0, lwd = 0, lty = 2, col = dot.line$col)
}

and then use the call:

    dotplot(code ~ net | att, data=all.net, panel=mypanel.dotplot)

-Stephen

<Prev in Thread] Current Thread [Next in Thread>