• Example shown in Figure 3.30 in Paul Murrell's R Graphics book.
    • Also be aware of the Hmisc histbackback function, which creates back-to-back histograms.

   
library(Hmisc)
library(Design)
data(chickwts)
contents(chickwts)
describe(chickwts)
# Want to determine no. of chicks above
#   the median weight
chickwts <- upData(chickwts,
   median.weight = ifelse(weight > median(weight, na.rm = TRUE), 1, 0),
   levels = list(median.weight = c("Below", "Above")))
# Look at simple table of freuquencies:
with(chickwts, table(feed, median.weight))

# Let's turn the table output into a back-to-back barplot
# Define the data:
datta <- with(chickwts, table(feed, median.weight))
# Define some parameters:
xlimit <- 15 # dependent on max frequency given in table
ticks <- seq(-xlimit, xlimit, 5) # where to plac the tickmarks
nlines <- nlevels(chickwts$feed) # number of groups
yvals <- 1:nlines # y values for the groups
h <- 0.25 # height of bars
# Start plotting
par(mar=c(0.5, 5, 0.5, 1))
plot.new() # start a new plot frame
plot.window(xlim=c(-xlimit, xlimit), ylim=c(-2.0, nlines+0.5))
lines(rep(0, 2), c(-2.0, nlines+0.5), col="grey")
segments(-xlimit, yvals, xlimit, yvals, lty="dotted")
rect(-datta[, "Below"], yvals-h, 0, yvals+h, col="dark grey")
rect(0, yvals-h, datta[, "Above"], yvals+h, col="light grey")
mtext(levels(chickwts$feed), at=yvals, adj=1, side=2, las=2)
par(cex.axis=0.5, mex=0.5)
axis(1, at=ticks, labels=abs(ticks), pos = 0)
tw <- 1.5*strwidth("Below")
rect(-tw, -1-h, 0, -1+h, col="dark grey")
rect(0, -1-h, tw, -1+h, col="light grey")
text(0, -1, "Below", pos=2)
text(0, -1, "Above", pos=4)
text(0, -1.5, "Back-to-back Barplot of Chick Weights Below/Above Median Value", 
   adj = 0.5)
Edit | Attach | Print version | History: r2 < r1 | Backlinks | View wiki text | Edit WikiText | More topic actions...
Topic revision: r1 - 08 Nov 2006, TheresaScott
 

This site is powered by FoswikiCopyright © 2013-2017 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback