[R] Make many barplot into one plot
Deepayan Sarkar
deepayan.sarkar at gmail.com
Fri Dec 1 21:09:14 CET 2006
On 12/1/06, Marc Schwartz <marc_schwartz at comcast.net> wrote:
> On Fri, 2006-12-01 at 18:02 +0100, Muhammad Subianto wrote:
> > Dear all,
> > ## 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")))
> >
> > ## with barplot I can make a plot for each table:
> >
> > barplot(satu, beside=TRUE, legend.text=rownames(satu),
> > ylim = c(0, max(colSums(satu)) * 1.2))
> > x11()
> > barplot(dua, beside=TRUE, legend.text=rownames(dua),
> > ylim = c(0, max(colSums(dua)) * 1.2))
> > x11()
> > barplot(tiga, beside=TRUE, legend.text=rownames(tiga),
> > ylim = c(0, max(colSums(tiga)) * 1.2))
> > x11()
> > barplot(empat, beside=TRUE, legend.text=rownames(empat),
> > ylim = c(0, max(colSums(empat)) * 1.2))
> >
> > ## I can make all barplot above into one plot with
> >
> > x11(width=11,height=8)
> > ## Make a plot with 2 rows and 2 columns
> > oldpar <- par(mfrow=c(2,2),
> > barplot(above)
> > par(oldpar)
> >
> > ## Are there any functions to make all barplot above into one plot?
> > ## I would like to produce barplot like:
> >
> > | | | |
> > | | | | | | | | | | | | | |
> > |pos|neg|pos|neg|pos|neg|pos|neg| |pos|neg|pos|neg| ...
> > | | | | | | | | | | | | | |
> > --------------------------------- --------------------
> > satu dua tiga empat satu dua ...
> > black blond
> >
> > I would be grateful if anybody could help me.
> > Thank you very much.
>
> 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:
>
> [snipped]
And here is a lattice alternative:
dflist <- list()
for (nm in c("satu", "dua", "tiga", "empat"))
{
dflist[[nm]] <- as.data.frame.table(get(nm))
}
cdf <- do.call(make.groups, dflist)
barchart(Freq ~ which | Var2, data = cdf,
groups = Var1, stack = FALSE, origin = 0,
layout = c(4, 1), auto.key = TRUE)
barchart(which ~ Freq | Var2, data = cdf,
groups = Var1, stack = TRUE,
auto.key = TRUE)
-Deepayan
More information about the R-help
mailing list