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.
我有一个数据框,其中包含一个定量变量x和几个不同的因子f1,f2,...,fn。各个因素的水平数不是一成不变的。
I want to create a (single) plot of densities of x by factor level fi.
我想用因子级别fi创建一个密度为x的(单个)图。
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.
我想知道的是,如果我只将因子指定为输入,我该怎么做呢?我甚至不知道如何编写一个可以满足我需要的for循环,我觉得'R way'会避免循环。
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?
如何在我的脚本中包含这种类型的计算?
3 个解决方案
#1
3
I'm assuming your data is in the format (data frame called df
)
我假设你的数据是格式(数据框称为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.
在那个原因中,lattice(或ggplot2)将非常有用。
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
#2
1
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]))
}
#3
1
ggplot2
code
library(ggplot2)
ggplot(data, aes(value, colour = f1)) +
stat_density(position = "identity")
#1
3
I'm assuming your data is in the format (data frame called df
)
我假设你的数据是格式(数据框称为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.
在那个原因中,lattice(或ggplot2)将非常有用。
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
#2
1
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]))
}
#3
1
ggplot2
code
library(ggplot2)
ggplot(data, aes(value, colour = f1)) +
stat_density(position = "identity")