[R] make many barplot into one plot

"Sibylle Stöckli" sibylle.stoeckli at gmx.ch
Fri Oct 29 14:33:40 CEST 2010


Dear R users


I would like to group my barplot graph (see example on the R help link). The proposed R code, adding individual bars to the plot, looks really overwhelming. My specific dataset just consists of five groups and three different levels within each groups (the individual bars). The .txt file is read as matrix (horizontal: group, vertical: levels).

The R trellis barchart (function group="") is an easy function, but unfortunately the upper plot part look much different from other graphs. I would therefore prefer barplot to stansdardize my plots within the manuscript.

It would be very  helpful for me to know if anyone else has worked on the barplot group function.

Thanks
Sibylle


http://onertipaday.blogspot.com/2007/05/make-many-barplot-into-one-plot.html

R code from the link
## I have 4 tables like this:satu <- array(c(5,15,20,68,29,54,84,119), dim=c(2,4), dimnames=list(c("Negative", "Positive"), c("Black", "Brown", "Red", "Blond")))dua <- array(c(50,105,30,8,29,25,84,9), dim=c(2,4), dimnames=list(c("Negative", "Positive"), c("Black", "Brown", "Red", "Blond")))tiga <- array(c(9,16,26,68,12,4,84,12), dim=c(2,4), dimnames=list(c("Negative", "Positive"), c("Black", "Brown", "Red", "Blond")))empat <- array(c(25,13,50,78,19,34,84,101), dim=c(2,4), dimnames=list(c("Negative", "Positive"), c("Black", "Brown", "Red", "Blond")))# rbind() the tables togetherTAB <- rbind(satu, dua, tiga, empat)# Do the barplot and save the bar midpointsmp <- barplot(TAB, beside = TRUE, axisnames = FALSE)# Add the individual bar labelsmtext(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 groupsat <- t(sapply(seq(1, nrow(TAB), by = 2),function(x) colMeans(mp[c(x, x+1), ])))# Add the group labels for each pairmtext(1, at = at, text = rep(c("satu", "dua", "tiga", "empat"), 4),line = 1, cex = 0.75)# Add the color labels for each groupmtext(1, at = colMeans(mp), text = c("Black", "Brown", "Red", "Blond"), line = 2)
-- 
Sicherer, schneller und einfacher. Die aktuellen Internet-Browser -
jetzt kostenlos herunterladen! http://portal.gmx.net/de/go/chbrowser



More information about the R-help mailing list