This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Plot multiple R empricial distributions with different X axis on the same plot

Hi,

I am facing a problem on which i have to plot three empirical distributions in R with the function "ecdf" but one limitation is that those distributions don't have the same X-axis values.

For example I have three vectors :

vector1 <- c(-5,-10,-5,-9,-7,-9,-6,-4,-1,-3,-6,-8,-7,-5,-5,-4,-6,-8,-9)
vector2 <- c(11,12,45,74,12,122,141,52,54,69,87,54,52,48,96)
vector3 <- c(0.2,0.3,0.3,0.3,0.5,0.9,0.5,0.4,0.6,0.8,0.5,0.6)

I would like to plot the empirical distributions of those three vectors in the same graph (The Y-axis will stay the same, between 0 and 1) but with different X-axis (here, axis x1 will be between -10 and -1 while axis x3 will be between 0 and 1). I have found many questions regarding plotting different curves with different y-axis but never found how to do it with distributions and different X.

Would you have any idea ?

r distribution plot

Please use the formatting bar (especially the code option) to present your post better. You can use backticks for inline code (`text` becomes text), or select a chunk of text and use the highlighted button to format it as a code block. I've done it for you this time.
code_formatting

1 answer

Hi,

You can use the lines() function, this allows you to add lines to an existing plot.

Do not forget to specify the x limits in your call to "plot()", in your example it would look like this:

plot(ecdf(vector1), xlim = c(-10, 141))
lines(ecdf(vector2), col = "blue")
lines(ecdf(vector3), col = "red")

Hi,

Thanks for your answer, but this will create an unique x-axis for all my vectors. I think I will go this way because creating three different X-axis in the same graph dosn't seem very feasible :/

Have a good day,

Max

Another solution would be to use par(mfrow=c(1,3) to create three subplots in the same figure (see https://www.statmethods.net/advgraphs/layout.html)

If I understood correctly, what you want to achieve is to "cut" your x-axis where you do not have data, I would personally advise against it as it would be misleading.

Log in to answer this question.