[R] tapply() and barplot() help files for 1.8.1
David Whiting
david.whiting at ncl.ac.uk
Fri Apr 16 12:26:00 CEST 2004
Martin Maechler <maechler at stat.math.ethz.ch> writes:
> and I like to help you.
> As I keep installed `(almost) all released versions of R ever
> installed on our machines'
> I can easily run 1.8.1 (or 1.4.x or 1.0.x ...) for you.
>
> The only difference
> between the help page help(tapply)
> is an extra "require(stats)" statement at the beginning of the
> `Examples' section in 1.9.0.
>
> and the only change to tapply() is
> group <- rep.int(one, nx)#- to contain the splitting vector
> instead of
> group <- rep (one, nx)#- to contain the splitting vector
>
> which hardly should have adverse results.
>
> In barplot, there's the new 'offset' option --- not in NEWS (!!!!)
>
> and another change that may be a problem.
>
> Can you dig harder and if possible provide a reproducible (small..)
> example to make progress here...
>
Last night I found I had a backup of the source of 1.8.0, built that
and tested an example and it worked as in 1.9.0. I then started to
question my sanity (or at least my competence).
The code that follows should be a reproducible example. It creates a
data frame that has the same structure as the data I am working with
(with a number of other columns dropped) and is followed by the
function that creates the barplot. The changes I have had to make to
make it work as I thought it was working with 1.8.1 have ## NEW BIT
after them, i.e. those lines were not there in the version I ran with
1.8.1. The important new lines are:
x <- matrix(x) ## NEW BIT
and
beside = TRUE, ## NEW BIT
--- EXAMPLE ---
## Create some fake data.
x <- c(rep("", 926),
rep("All Other Perinatal Causes", 46),
rep("Anaemia", 3),
rep("Congenital Abnormalities", 1),
rep("Unsp. Direct Maternal Causes", 24))
y <- runif(length(x))
tempdat <- data.frame(smi=x, yllperdth=y)
## Define the function to make my barplot
bodShare <- function(x, fld, main = "", userpar = 18, xlimMult=1.3 ) {
###############################################
# A horizontal barchart to display BoD shares #
###############################################
z <- subset(x, as.character(x[,fld]) != "")
z[, fld] <- factor(z[, fld])
## We need to change the parameters of the chart.
## First save the old settings.
oldpar <- par("mar")
newpar <- par("mar")
## Increase the size of the margin on the left so there
## is enough space for the long text labels (which will
## be displayed horizontally on the y-axis).
newpar[2] <- userpar
## Reduce the top margin because I will use a \caption in LaTeX
## instead.
newpar[3] <- 1
## Now apply the new settings.
par(mar = newpar)
## Calculate the % of YLLs for each group in the cause classification.
x <- tapply(z$yllperdth, z[, fld], sum)
totalYLLs <- sum(x)
x <- x / totalYLLs * 100
x <- sort(x)
causeNames <- names(x) ## NEW BIT
x <- matrix(x) ## NEW BIT
## Plot the chart. horiz = TRUE makes it a bar instead of
## column chart. las = 1 prints the labels horizontally.
xplot <- barplot(x,
## main = main,
horiz = TRUE,
beside = TRUE, ## NEW BIT
names.arg = causeNames, ## NEW BIT
xlab = "Percent of YLLs",
xlim = c(0, max(x) * xlimMult),
las = 1)
text(x + (max(x) * .15), xplot, formatC(x, digits=1, format='f'))
## Reset the old margin parameters.
par(mar = oldpar)
## Write data to a table for export.
# First we need to remove newlines from labels.
names(x) <- sub("\n", "", names(x))
write.table(as.table(x), file = paste("tables/", fld, ".csv", sep=""), col.names=NA, sep="\t")
names(x) <- causeNames
x[length(x)]
}
## Create the barplot.
bodShare(tempdat, "smi")
--
David Whiting
Dar es Salaam, Tanzania
More information about the R-help
mailing list