R中的频率分布 [英] Frequency distribution in R
问题描述
我有五列数字.我想在一张图表中用R中的不同颜色绘制五列的频率分布.有人可以帮我举例说明一下.我是R的新手.
I have five columns with numbers. I want to plot the frequency distribution of five columns in one graph with different colors in R. Can some one help me out how i can do this with an example. I am very new to R.
推荐答案
使用来自@eddi的示例数据,您还可以考虑使用"lattice"软件包:
Using the sample data from @eddi, you can also consider the "lattice" package:
set.seed(1)
d <- data.frame(a = rnorm(100), b = rnorm(100, 1), c = rnorm(100, 2),
d = rnorm(100, 3), e = rnorm(100, 4))
library(lattice)
densityplot(~ a + b + c + d + e, data = d)
这将产生:
如果列数很多,也可以通过先创建formula
来创建图形:
If you have many columns, you can also create your plot by first creating a formula
:
myFormula <- as.formula(paste("~ ", paste(names(d), collapse = "+")))
densityplot(myFormula, data = d)
您还应该探索densityplot
可用的各种选项,例如plot.points
(如果您不希望将点放在密度图的底部,可以将其设置为FALSE
)和auto.key
添加图例.
You should also explore the various options available for densityplot
, such as plot.points
(which can be set to FALSE
if you don't want the points at the bottom of the density plots) and auto.key
to add a legend.
另一个明显的选择是使用"ggplot2",但是为此,您需要首先将数据转换为长"格式:
Another obvious option is to use "ggplot2", but for this, you need to first convert your data into a "long" format:
d2 <- stack(d)
library(ggplot2)
qplot(values, colour=factor(ind), data=d2, geom="density")
结果:
这篇关于R中的频率分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!