How to overlay density plots in R?

pasta picture pasta · Aug 4, 2011 · Viewed 171.7k times · Source

I would like to overlay 2 density plots on the same device with R. How can I do that? I searched the web but I didn't find any obvious solution.

My idea would be to read data from a text file (columns) and then use

plot(density(MyData$Column1))
plot(density(MyData$Column2), add=T)

Or something in this spirit.

Answer

cbeleites unhappy with SX picture cbeleites unhappy with SX · Aug 4, 2011

use lines for the second one:

plot(density(MyData$Column1))
lines(density(MyData$Column2))

make sure the limits of the first plot are suitable, though.