Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Superimpose Multiple Density Curves Into One Plot in R

Tags:

plot

r

statistics

I have a data that looks like this.

And I intend to create multiple density curve into one plot, where each curve correspond to the unique ID.

I tried to use "sm" package, with this code, but without success.

library(sm)
dat <- read.table("mydat.txt");
plotfn <- ("~/Desktop/flowgram_superimposed.pdf");
pdf(plotfn);

sm.density.compare(dat$V1,dat$V2, xlab = "Flow Signal")
colfill <- c(2:10);
legend(locator(1), levels(dat$V2), fill=colfill)

dev.off();

Please advice what's the right way to do it or if there is alternative way to do it?

I am trying to get this kind of plot at the end. figure http://img524.imageshack.us/img524/2736/testl.png

like image 752
neversaint Avatar asked Sep 02 '09 10:09

neversaint


People also ask

How do you plot a density plot in R?

To create a density plot in R you can plot the object created with the R density function, that will plot a density curve in a new R window. You can also overlay the density curve over an R histogram with the lines function. The result is the empirical density function.

What is stacked density plot?

To plot a stacked graph of estimates, use a shared extent and a fixed number of subdivision steps to ensure that the points for each area align well. Density estimates of measurements for each iris flower feature are plot in a stacked method.

How do I add a density curve to a histogram in R?

In order to add a density curve over a histogram you can use the lines function for plotting the curve and density for calculating the underlying non-parametric (kernel) density of the distribution. The bandwidth selection for adjusting non-parametric densities is an area of intense research.


1 Answers

Try using ggplot2:

dnow <- read.table("http://dpaste.com/88561/plain/")
library(ggplot2)
qplot(V1, colour=factor(V2), data=dnow, geom="density")
like image 72
Eduardo Leoni Avatar answered Nov 10 '22 01:11

Eduardo Leoni