Skip to content
Prev 105395 / 398506 Next

Make many barplot into one plot

On Fri, 2006-12-01 at 18:02 +0100, Muhammad Subianto wrote:
I would encourage you to look at the barchart() function in the lattice
package, which in many ways is better suited to doing multi-dimensional
plots.

That being said:

# rbind() the tables together
TAB <- rbind(satu, dua, tiga, empat)

# Do the barplot and save the bar midpoints
mp <- barplot(TAB, beside = TRUE,
              axisnames = FALSE)

# Add the individual bar labels
mtext(1, at = mp, text = c("N", "P"),
      line = 0, cex = 0.5)

# Get the midpoints of each sequential pair of bars
# within each of the four groups
at <- t(sapply(seq(1, nrow(TAB), by = 2),
               function(x) colMeans(mp[c(x, x+1), ])))

# Add the group labels for each pair
mtext(1, at = at,
      text = rep(c("satu", "dua", "tiga", "empat"), 4),
      line = 1, cex = 0.75)

# Add the color labels for each group
mtext(1, at = colMeans(mp),
      text = c("Black", "Brown", "Red", "Blond"),
      line = 2)


Take a look at ?barplot and note that the function returns the bar
midpoints, which in this case is a matrix:
[,1] [,2] [,3] [,4]
[1,]  1.5 10.5 19.5 28.5
[2,]  2.5 11.5 20.5 29.5
[3,]  3.5 12.5 21.5 30.5
[4,]  4.5 13.5 22.5 31.5
[5,]  5.5 14.5 23.5 32.5
[6,]  6.5 15.5 24.5 33.5
[7,]  7.5 16.5 25.5 34.5
[8,]  8.5 17.5 26.5 35.5


Then look at ?mtext for the labelling.

HTH,

Marc Schwartz