[R] barchart for aggregated (sum) data in lattice?

Marianne Promberger mpromber at psych.upenn.edu
Sun Oct 5 19:12:50 CEST 2008


Hi list,

I have data in a dataframe t1, with a column for different amounts
spent, a column what it was spent on, and a column with dates, from
which I create a new column with months.

Example:

amount <- rep(c(10,20,30),3)
what <-  rep(c("food","books","cycling"),3)
when <- c(rep("2008-09-05",5),rep("2008-10-07",4))
t1 <- data.frame(amount,what,when)
t1$when <- as.Date(t1$when)
t1$month <- format(as.Date(t1$when),"%b")

I want to have a barplot for each month, showing the sum spent in the
different categories. 

I figured I can do this with traditional graphics using:

barplot(xtabs(amount~what+month, data=t1),beside=T)

But I'd like to be able to do this in lattice. 

I tried:

barchart(amount~what|month,t1)

But that doesn't sum the data for t1$amount for each month first.

How could I do that?

Thanks,

Marianne

-- 
Marianne Promberger
Graduate student in Psychology
http://www.psych.upenn.edu/~mpromber



More information about the R-help mailing list