开发者

how to script in R over a factor's levels

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?


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


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]))
    }


ggplot2 code

library(ggplot2)
ggplot(data, aes(value, colour = f1)) +
  stat_density(position = "identity")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜