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.
Asked
Active
Viewed 1.3k times
-4
-
2Please do some research before asking a question: http://www.cookbook-r.com/Graphs/Plotting_distributions_(ggplot2) – Roland Apr 22 '13 at 14:52
-
1post a reproducible example with sample input, expected output and the code that you have tried. – Nishanth Apr 22 '13 at 14:55
-
I have simple length distribution of 5 elements in 5 columns 1st 5 7 8 9 10, 2nd 6 47 484 848 ,3rd 38 383 838 and so on. I just want to have the frequency distribution in bins of say 10 for the whole data set in one histogram plot with different colors. – user2252362 Apr 23 '13 at 13:49
2 Answers
7
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:

A5C1D2H2I1M1N2O1R2T1
- 190,393
- 28
- 405
- 485
0
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")

eddi
- 49,088
- 6
- 104
- 155
-
Add a method to automatically calculate the ranges for `xlim` and `ylim`, and I would be happy to up-vote this answer. :) – A5C1D2H2I1M1N2O1R2T1 Apr 22 '13 at 17:26