Frequency distribution in R

2019-04-17 17:48发布

问题:

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.

回答1:

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:



回答2:

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