Possible error in geom_ribbon

I was hoping to build two time series and fill the space between the rows, according to which at that time the series was larger.

here are two series: first in the data frame with an indicator of which of the series is larger at this time

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

And this is a molten series.

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

which I draw ...

 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")) 

that leads to...

the resulting plot

Why is there an extra blue streak in the middle of the plot?

+4
r ggplot2
source share
1 answer

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

 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 lines of the sequences d1$big == "B" often contain different values โ€‹โ€‹of csa and csb . As a result, there is a visible tape connecting the data. The above command uses the last lines before and the first lines after these sequences along with the data for the first tape. This problem does not exist for d1$big == "A" (base for the second tape).

Full 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

+8
source share

All Articles