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

Marianne Promberger mpromber at psych.upenn.edu
Sun Oct 5 19:32:36 CEST 2008


Arrgh. I spent so long fiddling with this, and two minutes after I mail
to the list I find the solution.

barchart(xtabs(amount~month+what, data=t1),stack=F,auto.key=T)

(It took me a long time to discover the extended xtabs formula, and
before I had kept trying to combine xtabs with the "|" factor notation
in lattice, like barchart(xtabs(amount~what)|month or so. 

Sorry for the clutter.
m.



On Sunday, 05 October 2008, 18:12 (UTC+0100), Marianne Promberger wrote:
> 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
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 

-- 
Marianne Promberger
Graduate student in Psychology
http://www.psych.upenn.edu/~mpromber
PGP/GnuPG public key: http://promberger.info/mpromber-pgp.asc



More information about the R-help mailing list