#!Rscript a <- c(1,2,3,4,5,4,3,2,1,5,6,-12) b <- c(7,4,2,1,-1,-4,5,3,2,1,0,-11) swappoints <- vector() if (length(a) != length(b)) { print("Horribly sorry, old chap, we need equally sized lists for this to work") q() } a.is.greater <- F if (a[[1]] > b[[1]]) a.is.greater <- T for (i in c(1:length(b))) { if (a[[i]] > b[[i]] & a.is.greater == F) { a.is.greater <- T swappoints <- c(swappoints, a[[i]]) } if (b[[i]] > a[[i]] & a.is.greater == T) { a.is.greater <- F swappoints <- c(swappoints, -1 * a[[i]]) } } # segmented y redToGreen = swappoints[swappoints < 0] greenToRed = swappoints[swappoints > 0] png('SpreadCurves2.png') plot(seq_len(length(b)),b, xpd=NA, main='Contrived setup', type='l') lines(b, col='red') lines(a, col='blue') polygon(seq_len(length(b))[swappoints>0], b[swappoints>0], col='green') polygon(seq_len(length(b))[swappoints<0], b[swappoints<0], col='red') dev.off() print(paste('Output in SpreadCurves2.png in', getwd()))