Frequency distribution in R

2019-04-17 18:02发布

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.

2条回答
Melony?
2楼-- · 2019-04-17 18:27

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:

enter image description here

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:

enter image description here

查看更多
ゆ 、 Hurt°
3楼-- · 2019-04-17 18:37

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")
查看更多
登录 后发表回答