Note that there are some explanatory texts on larger screens.

plurals
  1. POIs a multiple clip on a plot possible?
    text
    copied!<p>I am editing this to provide a better example of what I need. I will keep the original message at the bottom in case that helps.</p> <p>I have the following data:</p> <pre><code>x=c(1,2,7,3,4,8,9,5,6,7,11,13,15,8,9,10,11,12,13,15) y=c(2:10,9,8,7,6,8,10,11,12,13,14,1) date=strptime(20010101:20010120,'%Y%m%d') z=data.frame(date,x,y) z$diff=z$y-z$x z$min=pmin(x,y) z$max=pmax(x,y) </code></pre> <p>So my data is this:</p> <pre><code> date x y diff min max 1 2001-01-01 1 2 1 1 2 2 2001-01-02 2 3 1 2 3 3 2001-01-03 7 4 -3 4 7 4 2001-01-04 3 5 2 3 5 5 2001-01-05 4 6 2 4 6 6 2001-01-06 8 7 -1 7 8 7 2001-01-07 9 8 -1 8 9 8 2001-01-08 5 9 4 5 9 9 2001-01-09 6 10 4 6 10 10 2001-01-10 7 9 2 7 9 11 2001-01-11 11 8 -3 8 11 12 2001-01-12 13 7 -6 7 13 13 2001-01-13 15 6 -9 6 15 14 2001-01-14 8 8 0 8 8 15 2001-01-15 9 10 1 9 10 16 2001-01-16 10 11 1 10 11 17 2001-01-17 11 12 1 11 12 18 2001-01-18 12 13 1 12 13 19 2001-01-19 13 14 1 13 14 20 2001-01-20 15 1 -14 1 15 </code></pre> <p>I want to create a polygon plot where the color of the polygon changes based on when z$diff is less than zero. So the plot should look like this:</p> <p><img src="https://i.stack.imgur.com/Pkc2y.png" alt="Polygon plot with different color based on condition"></p> <p>I know segments can do this with lines, but unfortunately for me I need to do it with a polygon.</p> <p>Original message:</p> <p>Let's say I have this data:</p> <pre><code>x=rnorm(100) y=rnorm(100) date=strptime(20010101:20010410,'%Y%m%d') date=date[complete.cases(date)] z=data.frame(date,x,y) z$max=apply(z[2:3],1,which.max) z$min=apply(z[2:3],1,which.min) z$v=z$max-z$min w=z[z$v&lt;0,] </code></pre> <p>Then I try to make a polygon consisting of two colors, one for when x>y, and another for when y>x. I do this:</p> <pre><code>plot(z$date,z$x,type='n') polygon(c(z$date,z$date[nrow(z):1]),c(z$x,z$y[nrow(z):1]),col='skyblue',border=NA) polygon(c(w$date,w$date[nrow(w):1]),c(w$x,w$y[nrow(w):1]),col='salmon',border=NA) </code></pre> <p>What happens is that when there are gaps in data frame <code>w</code> the polygon covers those gaps. I know how to use clip to clip one region, but can it be used to clip multiple gaps in a data frame?</p> <p>Ideally the <code>w</code> polygon should overlap on the <code>z</code> polygon whenever y>x.</p>
 

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