Search code examples
rplotdensity-plot

How to overlay density plots in R?


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.


Solution

  • 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.