[R] Odp: aggregation question

Petr PIKAL petr.pikal at precheza.cz
Thu Jul 7 13:27:18 CEST 2011


OK

> Petr,
> 
> Maybe I did not make it clear, I apologize for that:
> 
> I want January to December on the X Axis (as 12 discrete (months))
> and then for each month the values for each year as bars in
> different colors next to each other, ie Jan-2009, Jan-2011,
> Jan-2011...Dec-2009, Dec-2011, Dec-2011 whereas at the moment I get
> Jan-2009, Feb-2009, Mar-2009...Oct-2011, Nov-2011, Dec-2011

Well you can look at examples of barplot, especially this one

barplot(VADeaths, beside = TRUE,
        col = c("lightblue", "mistyrose", "lightcyan",
                "lavender", "cornsilk"),
        legend = rownames(VADeaths), ylim = c(0, 100))
title(main = "Death Rates in Virginia", font.main = 4)


If you look at VADeaths structure you see you need some structured data

x<-seq(as.Date("2000/1/1"), by="month", length.out=24)
x.m<-aggregate(1:24, list(format(x, "%m"), format(x, "%Y")), sum)
x.m
   Group.1 Group.2  x
1       01    2000  1
2       02    2000  2
3       03    2000  3
4       04    2000  4
5       05    2000  5
<snip>

and you can use e.g. xtabs or maybe cast from reshape package

x.xt<-xtabs(x~Group.1+Group.2,x.m)
barplot(x.xt, beside=TRUE, col=rainbow(12))
x.xt<-xtabs(x~Group.2+Group.1,x.m)
barplot(x.xt, beside=T, col=1:2)

Or you could look at ggplot2 package.

Regards
Petr




> 
> In SQL something like GROUP BY MONTH, YEAR as opposed to GROUP BY
> YEAR, MONTH.
> 
> greetings, el
> 
> on 2011-07-07 11:08 Petr PIKAL said the following:
> [...]
> >> How do I get the monthly aggregated payments in different colors
> >> next to each other (ie for each year in a different color with the x
> >> axis showing the months)?
> > 
> > What about putting suitable set of colours to col argument?
> > 
> > Regards
> > Petr
> [...]
> 
> -- 
> Dr. Eberhard W. Lisse  \        / Obstetrician & Gynaecologist (Saar)
> el at lisse.NA el108-ARIN / *     |   Telephone: +264 81 124 6733 (cell)
> PO Box 8421             \     /   Please do NOT email to this address
> Bachbrecht, Namibia     ;____/        if it is DNS related in ANY way



More information about the R-help mailing list