Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Below is some code <a href="http://www.mathworks.com/matlabcentral/fileexchange/27205" rel="nofollow noreferrer">ported from Matlab</a> to calculate the intersection between segments. If you apply it between the x axis (fixed) and each pair of successive points, you get a list of new coordinates that indicate the crossing points between your <code>geom_line</code> and the x axis. From this it's an easy step to shade the relevant polygons. Note that I haven't properly tested the ported Matlab code.</p> <p><img src="https://i.stack.imgur.com/PQzNz.png" alt="enter image description here"></p> <pre><code>## Ported from Matlab to R ## Copyright (c) 2010, U. Murat Erdem ## All rights reserved. ## http://www.mathworks.com/matlabcentral/fileexchange/27205 lineSegmentIntersect &lt;- function(XY1, XY2){ n_rows_1 &lt;- nrow(XY1) n_cols_1 &lt;- ncol(XY1) n_rows_2 &lt;- nrow(XY2) n_cols_2 &lt;- ncol(XY2) stopifnot(n_cols_1 == 4 &amp;&amp; n_cols_2 == 4) nc &lt;- n_rows_1 * n_rows_2 X1 &lt;- matrix(XY1[,1], nrow=nc, ncol=1) X2 &lt;- matrix(XY1[,3], nrow=nc, ncol=1) Y1 &lt;- matrix(XY1[,2], nrow=nc, ncol=1) Y2 &lt;- matrix(XY1[,4], nrow=nc, ncol=1) XY2 &lt;- t(XY2) X3 &lt;- matrix(XY2[1,], nrow=nc, ncol=1) X4 &lt;- matrix(XY2[3,], nrow=nc, ncol=1) Y3 &lt;- matrix(XY2[2,], nrow=nc, ncol=1) Y4 &lt;- matrix(XY2[4,], nrow=nc, ncol=1) X4_X3 &lt;- X4-X3 Y1_Y3 &lt;- Y1-Y3 Y4_Y3 &lt;- Y4-Y3 X1_X3 &lt;- X1-X3 X2_X1 &lt;- X2-X1 Y2_Y1 &lt;- Y2-Y1 numerator_a &lt;- X4_X3 * Y1_Y3 - Y4_Y3 * X1_X3 numerator_b &lt;- X2_X1 * Y1_Y3 - Y2_Y1 * X1_X3 denominator &lt;- Y4_Y3 * X2_X1 - X4_X3 * Y2_Y1 u_a &lt;- numerator_a / denominator u_b &lt;- numerator_b / denominator INT_X &lt;- X1 + X2_X1 * u_a INT_Y &lt;- Y1 + Y2_Y1 * u_a INT_B &lt;- (u_a &gt;= 0) &amp; (u_a &lt;= 1) &amp; (u_b &gt;= 0) &amp; (u_b &lt;= 1) PAR_B &lt;- denominator == 0 COINC_B &lt;- (numerator_a == 0 &amp; numerator_b == 0 &amp; PAR_B) data.frame(x=INT_X[INT_B], y=INT_Y[INT_B]) } set.seed(123) x &lt;- sort(runif(50, -10, 10)) y &lt;- jitter(sin(x), a=2) n &lt;- length(x) xy1 &lt;- matrix(c(-10, 0, 10, 0), ncol=4) xy2 &lt;- cbind(x[-n], y[-n], x[-1], y[-1]) test &lt;- lineSegmentIntersect(xy1, xy2) library(ggplot2) d &lt;- data.frame(x=x, y=y) d2 &lt;- rbind(d, test) d2 &lt;- subset(d2[order(d2$x), ], y &lt;=0) p &lt;- qplot(x, y, data=d, geom="path") p + geom_ribbon(data=d2, aes(ymin = 0, ymax = y), fill="red") </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload