Frequency distribution in R

让人想犯罪 __ 提交于 2019-12-04 22:10:54

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)

This will yield:

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)

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.


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")

The result:

Here's a base R solution:

d = data.frame(a = rnorm(100), b = rnorm(100, 1), c = rnorm(100, 2), d = rnorm(100, 3), e = rnorm(100, 4))

plot(density(d$a), xlim = c(-4, 8))
lines(density(d$b), col = "red")
lines(density(d$c), col = "green")
lines(density(d$d), col = "blue")
lines(density(d$e), col = "yellow")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!