Dear Walt,
One way to do it is the following:
#########start s-plus code
TESTDATA <- barley # barley comes with s-plus
set.seed(56748) # just to ensure you get same results
TESTDATA$NEGPOSYIELD <- TESTDATA$yield
# now randomly replace some yield values by its negative values
TESTDATA$NEGPOSYIELD[ sample(1:nrow(TESTDATA),60) ] <- - TESTDATA$yield[
sample(1:nrow(TESTDATA),60) ]
# define new panel.barchart which has zero as origin
panel.barchart2 <-
function(x, y, col = trellis.par.get("bar.fill")$col, border = 1, ...)
{
ok <- !is.na(x) & !is.na(y)
x <- x[ok]
y <- y[ok]
n <- length(y)
NAs <- rep(NA, n)
origin <- 0
y1 <- y - 1/3
y2 <- y + 1/3
polygon(c(rbind(0, x, x, 0, NAs)), c(rbind(y1, y1, y2, y2,
NAs)),
col = col, border = as.numeric(border), ...)
abline(v=origin)
}
barchart(variety ~ NEGPOSYIELD | year * site, data = TESTDATA , aspect =
0.4, xlab = "Barley Yield (bushels/acre)",panel=panel.barchart2)
################# end s-plus code
( I know that barchart may not be the best approach to use but I
believe this is what you were looking for)
Samer
Samer Mouksassi Pharm.D.
Senior Associate Scientist
Pharsight- A Certara(tm) Company
2000 Peel, Suite 570
Montreal, Quebec, Canada H3A 2W5
Phone: 514.789.2184
Mobile: 514.475.9339
Fax: 514.789.2192
Email: smouksassi@pharsight.com
The information contained in this electronic mail message (including any
attachments) is intended only for the personal and confidential use of
the designated recipient(s) named above. If the reader of this message
is not the intended recipient or an agent responsible for delivering it
to the intended recipient, you are hereby notified that you have
received this message in error and that any review, dissemination,
distribution, or copying of this message is strictly prohibited. If you
have received this communication in error, please notify the sender
immediately by telephone and/or e-mail (smouksassi@pharsight.com) and
destroy any and all copies of this message in your possession (whether
hardcopies or electronically stored copies). Thank you.
-----Original Message-----
From: s-news-owner@lists.biostat.wustl.edu
[mailto:s-news-owner@lists.biostat.wustl.edu] On Behalf Of Stephen
Kaluzny
Sent: Wednesday, July 15, 2009 7:18 PM
To: s-news@lists.biostat.wustl.edu
Subject: Re: [S] barchart in Trellis
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
--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu. To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message: unsubscribe s-news
|