Otherwise known as filled curves.
Say you want to, instead of drawing a single line, draw a filled curve. R’s basic plot doesn’t make the especially easy, though it can be made much easier with packages such as ggplot2 as we’ll see in a week.
In any case, the basic trick is to draw polygons on the screen. You have to manually specify the bounds of the polygon, but this isn’t hard: for the first filled curve, the bottom is the X axis, probably 0..0, and the top is is the desired height of your curve. The trick is you specify these points on the polygon as linearly connected segments, so you have to go left to right then right back to left. Let’s do this with the Yahoo data and overlay our moving average as a single line. Load our yahoo2 data as in part 6:
1 2 3 4 5 6 7 8 9 10
Unfortunately, the black border created by polygon almost overshadows the black line of the moving average. It can be removed by setting border=NA when calling polygon:
1 2 3
Now, let’s stack two filled line plots. The trick is for the second series, when we create it’s polygon, to use the height of the first polygon instead of zero for the bottom. This doesn’t work particularly well visually, but I think does demonstrate the technique.
1 2 3 4 5 6 7 8 9 10 11
Finally, let’s tidy up a bit, properly label our axes, and zoom the data in a bit:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19