1

I have input data with very many observations per day. I can make the barplot I want with 1 day by using table().

If I try to load more than 1 day and call table() I run out of memory.

I'm trying to table() each day and concatenate the tables into totals I can then barplot later. But I just cannot work out how to take the already tabled data and barplot each day as a stacked column.

After looping and consolidating I end up with something like this: 2 days of observations. (the Freq column is the default from the previous table() calls)

What is the best way to do a stacked barplot when my data ends up like this?

> data.frame(CLIENT=c("Mr Fluffy","Peppa Pig","Mr Fluffy","Dr Who"), Freq=c(18414000,9000000,7000000,15000000), DAY=c("2011-11-03","2011-11-03","2011-11-04","2011-11-04"))
     CLIENT     Freq        DAY
1 Mr Fluffy 18414000 2011-11-03
2 Peppa Pig  9000000 2011-11-03
3 Mr Fluffy  7000000 2011-11-04
4    Dr Who 15000000 2011-11-04
>
> # What should I put here?
Terry
  • 199
  • 3
  • 8

2 Answers2

3

I'm assuming that you are using base graphics since you mention barplot. Here is an approach using that:

wide <- reshape(dat, idvar="CLIENT", timevar="DAY", direction="wide")
barplot(as.matrix(wide[-1]), beside=FALSE)

Alternatively, using ggplot2:

library("ggplot2")

ggplot(dat, aes(x=DAY, y=Freq)) +
  geom_bar(aes(fill=CLIENT), position="stack")
Brian Diggs
  • 57,757
  • 13
  • 166
  • 188
  • Thanks Brian for your answer. There is a slight problem with the barplot output in that the `NA` values genereated by `reshape` mess up the final `barplot` representation. I fixed this up by putting in `0` instead. e.g. `wide[is.na(wide)] <- 0` . BTW I _was_ looking for the barplot (though thanks for the ggplot2) because I've found ggplot2 runs out of memory on some different graphs of the same data :( – Terry Nov 03 '11 at 13:28
1

Try ggplot2:

ggplot(df,aes(DAY,fill=CLIENT,weight=Freq))+geom_bar()

Shamelessly ripped from here:

http://had.co.nz/ggplot2/geom_bar.html

Spacedman
  • 92,590
  • 12
  • 140
  • 224