tags:

views:

66

answers:

3

I have a data frame with a quantitative variable, x, and several different factors, f1, f2, ...,fn. The number of levels is not constant across factors.

I want to create a (single) plot of densities of x by factor level fi.

I know how to hand code this for a specific factor. For example, here is the plot for a factor with two levels.

# set up the background plot 
plot(density(frame$x[frame$f1=="level1"]))

# add curves 
lines(density(frame$x[frame$f1=="level2"]))

I could also do this like so:

# set up the background plot 
plot(NA)

# add curves 
lines(density(frame$x[frame$f1=="level1"]))
lines(density(frame$x[frame$f1=="level2"]))

What I'd like to know is how can I do this if I only specify the factor as input. I don't even know how to write a for loop that would do what I need, and I have the feeling that the 'R way' would avoid for loops.

Bonus: For the plots, I would like to specify limiting values for the axes. Right now I do this in this way:

xmin=min(frame$x[frame$f1=="level1"],frame$x[frame$f1=="level2"])

How can I include this type of calculation in my script?

+2  A: 

I'm assuming your data is in the format (data frame called df)

    f1     f2     f3     fn      value
    A........................... value 1
    A............................value 2
    .............................
    B............................value n-1
    B............................value n

In that cause, lattice (or ggplot2) will be very useful.

library(lattice)

densityplot(~value, groups = f1, data = df, plot.points = FALSE)

This should get you close to what you are looking for, I think.

Greg

Greg
Works great! Thanks so much!
J Miller
you may want to add auto.key = TRUE as an argument to densityplot as well.
Greg
Thanks for that, too. Great pointer!
J Miller
+1  A: 

You could also do:

# create an empty plot. You may want to add xlab, ylab etc
# EDIT: also add some appropriate axis limits with xlim and ylim
plot(0, 0, "n", xlim=c(0, 10), ylim=c(0, 2))
levels <- unique(frame$f1)
for (l in levels)
    {
    lines(density(frame$x[frame$f1==l]))
    }
nico
Thanks, nico! Works great. And I appreciate seeing how to use a for loop in this way.
J Miller
+1  A: 

ggplot2 code

library(ggplot2)
ggplot(data, aes(value, colour = f1)) +
  stat_density(position = "identity")
JoFrhwld
Thanks, JoFrhwld. I've heard about ggplot2 and have been meaning to learn more about this. I like the way the commands also label the plots.
J Miller