1

I've generated a set of levels from my dataset, and now I want to find a way to sum the rest of the data columns in order to plot it while plotting my first column. Something like:

levelSet <- cut(frame$x1, "cutting")
boxplot(frame$x1~levelSet)
for (l in levelSet)
{
  x2Sum<-sum(frame$x2[levelSet==l])
}

or maybe the inside of the loop should look like:

lines(sum(frame$x2[levelSet==l]))

Any thoughts? I am new to R, but I can't seem to get a hang of the indexing and ~ notation thus far.

I know r doesn't work this way, but I'd like functionality that 'looks' like

hist(frame$x2~levelSet)
## Or
hist(frame$x2, breaks = levelSet)
csgillespie
  • 59,189
  • 14
  • 150
  • 185
Bob
  • 47
  • 1
  • 4
  • I'm voting this up as it was a first post that provided a nice level of detail on what you were looking for. You probably want to add in a line or two at the beginning that creates a dataset that can be tested on. The `dput` command is particularly helpful for that. – Ari B. Friedman Mar 10 '11 at 05:37

1 Answers1

0

To plot a histograph, boxplot, etc. over a level set:

Try the lattice package:

library(lattice)
histogram(~x2|equal.count(x1),data=frame)

Substitute shingle for equal.count to set your own break points.

ggplot2 would also work nicely for this.

To put a histogram over a boxplot:

par(mfrow=c(2,1))
hist(x2)
boxplot(x2)

You can also use the layout() command to fine-tune the arrangement.

Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
  • Thanks for your help, but that does not seem to accomplish what I am looking for. Specifically, I want to be able to put a histogram of frame$x2 either right under or over the boxplot(frame$x1~levelSet). I think something like 'aggregate(frame$x2, by=list(levelSet), sum)' will do the trick. found [here](http://stackoverflow.com/questions/4737753/calculate-average-over-multiple-data-frames)... now the question becomes how to plot it. – Bob Mar 10 '11 at 13:16