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

Sven hohenstein
source share