[R] [SOLVED] barchart for aggregated (sum) data in lattice?
h.wickham at gmail.com
Sun Oct 5 20:20:30 CEST 2008
You can also do it with ggplot2:
qplot(month, weight = amount, fill = what, data=t1, geom="bar")
You can find out more at http://had.co.nz/ggplot2
(And you probably want to set the levels of the months so they don't
appear in alphabetical order)
On Sun, Oct 5, 2008 at 12:32 PM, Marianne Promberger
<mpromber at psych.upenn.edu> wrote:
> 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.
> 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.
>> 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:
>> But that doesn't sum the data for t1$amount for each month first.
>> How could I do that?
>> Marianne Promberger
>> Graduate student in Psychology
>> R-help at r-project.org mailing list
>> 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
> PGP/GnuPG public key: http://promberger.info/mpromber-pgp.asc
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help