Question: Plot multiple R empricial distributions with different X axis on the same plot
0
maxime.policarpo90 wrote:

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 ?

plot distribution R • 244 views
modified 11 months ago by RamRS27k • written 11 months ago by maxime.policarpo90

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. 1
Corentin450 wrote:

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.