5

i was hoping to plot two time series and shade the space between the series according to which series is larger at that time.

here are the two series-- first in a data frame with an indicator for whichever series is larger at that time

d1 <- read.csv("https://dl.dropbox.com/s/0txm3f70msd3nm6/ribbon%20data.csv?dl=1")

And this is the melted series.

d2 <- read.csv("https://dl.dropbox.com/s/6ohwmtkhpsutpig/melted%20ribbon%20data.csv?dl=1")

which I plot...

ggplot() + geom_line(data = d2,
                 aes(x = time, y = value, group = variable, color = variable)) +
         geom_hline(yintercept = 0, linetype = 2) +
         geom_ribbon(data = d1[d1$big == "B",],
                     aes(x = time, ymin = csa, 
                         ymax =  csb),
                         alpha  = .25,
                         fill = "#9999CC") +
         geom_ribbon(data = d1[d1$big == "A",],
                     aes(x = time, ymin = csb, 
                         ymax =  csa),
                     alpha  = .25,
                     fill = "#CC6666") +
         scale_color_manual(values = c("#CC6666" , "#9999CC"))

which results in...

the resulting plot

why is there a superfluous blue band in the middle of the plot?

Community
  • 1
  • 1
tomw
  • 3,114
  • 4
  • 29
  • 51
  • 1
    It's because you have a gap in the data. To tell `ggplot` about this gap, and to plot nothing, you have to terminate the ribbon by adding a row of NA data at the end of each "section". – Andrie Oct 28 '12 at 05:53
  • i'm actually not sure that's it, because these are just two simulated random walk series. why are there no red bands between their ribbons? – tomw Oct 28 '12 at 06:20
  • 1
    I second the comment of @Andrie. The plot is correct. The ribbon connects the data points. If you want to remove the gap, you have to create separate ribbons. – Sven Hohenstein Oct 28 '12 at 07:42
  • There are no red bands because at the "edges" of the `d1$big == "A"` data the differences between `csa` and `csb` are always zero. This does *not* hold for `d1$big == "B"`. – Sven Hohenstein Oct 28 '12 at 09:54
  • thanks @Andrie and Sven-- the elegance of your criticism alluded me at first. – tomw Oct 28 '12 at 18:22

1 Answers1

9

Here is a solution. I replaced data = d1[d1$big == "B",] in the first geom_ribbon function with:

data = rbind(d1[d1$big == "B",],
             d1[c((which(diff(as.numeric(d1$big)) == -1) + 1),
                  (which(diff(as.numeric(d1$big)) == 1))), ])

This is necessary since the first and last rows of d1$big == "B" sequences often contain different csa and csb values. As a result, there is a visible ribbon connecting the data. The above command uses the last rows before and the first rows after these sequences together with the data for the first ribbon. This problem does not exist for d1$big == "A" (the base for the second ribbon).

The complete code:

ggplot() +
 geom_line(data = d2,
           aes(x = time, y = value, group = variable, color = variable)) +
 geom_hline(yintercept = 0, linetype = 2) +
 geom_ribbon(data = rbind(d1[d1$big == "B",],
                          d1[c((which(diff(as.numeric(d1$big)) == -1) + 1),
                               (which(diff(as.numeric(d1$big)) == 1))), ]),
             aes(x = time, ymin = csa, ymax =  csb),
             alpha  = .25, fill = "#9999CC") +
 geom_ribbon(data = d1[d1$big == "A",],
             aes(x = time, ymin = csb, ymax =  csa),
             alpha  = .25, fill = "#CC6666") +
 scale_color_manual(values = c("#CC6666" , "#9999CC"))

enter image description here

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
  • I had no clue how to include this for my own plot, I ended up making two new columns, 1 above and 1 below the line, filling the spaces with NA's. This worked flawlessly – Mathias711 Nov 16 '16 at 13:16