R - 两条交叉线之间的阴影区域,颜色不同

时间:2015-08-19 10:06:06

标签: r plot polygon

我有一个516行和2列的矩阵(名为ichimoku),每个矩阵包含要绘制的值,目标是为Ichimoku strategy重新创建云。 使用matpot,我能够绘制这两条曲线,但我想要的是遮蔽两条曲线之间的区域。我有两个问题:

  • 我尝试使用多边形来遮挡区域,但它不起作用。我怀疑这是因为两个系列(senkouA和senkouB)在情节上交叉了几次而不是总是比另一个更大

  • 如果senkouA> senkouB我希望该区域以绿色阴影,如果是senkouB> senkouA,我希望该区域是阴影,但是从我读到的多边形只能是一种颜色。

是否还有一个多边形功能可以帮助我实现我想要的功能,当senkouA> senkouB时,senkouA和senkouB之间的绿色阴影区域,以及senkouB> senkouA?

ichimoku矩阵看起来像这样(第一列是senkouA,另一列是senkouB)

        [,1]      [,2]
[1,] 23323.62   23320.53
[2,] 23334.67   23328.71
[3,] 23334.11   23323.06
[4,] 23332.94   23323.06
...

这是我的matplot函数(有效):

matplot(ichimoku,lty=1,lwd=1,pch=20,type="l",col=c("red","blue"))

和我的多边形函数(没有):

polygon(c(1:516,516:1),c(senkouA,senkouB),col='green')

2 个答案:

答案 0 :(得分:4)

如果找到曲线之间的交点,则可以在交点之间绘制多边形。以下是对先前post的修改,它们可以找到曲线之间的交点,以及绘制多边形的函数。

## Some sample data
set.seed(0)
dat <- data.frame(x1=3*sin(3*(x=seq(0,10,len=100)))+rnorm(100),
                  x2=2*cos(x)+rnorm(100))

## https://stackoverflow.com/questions/20519431/finding-point-of-intersection-in-r
intersects <- function(x1, x2) {
    seg1 <- which(!!diff(x1 > x2))                            # location of first point in crossing segments
    above <- x2[seg1] > x1[seg1]                              # which curve is above prior to crossing
    slope1 <- x1[seg1+1] - x1[seg1]
    slope2 <- x2[seg1+1] - x2[seg1]
    x <- seg1 + ((x2[seg1] - x1[seg1]) / (slope1 - slope2))
    y <- x1[seg1] + slope1*(x - seg1)
    data.frame(x=x, y=y, pindex=seg1, pabove=(1:2)[above+1L])  # pabove is greater curve prior to crossing
}

ichimoku <- function(data, addLines=TRUE) {
    ## Find points of intersections
    ints <- intersects(data[,1], data[,2])
    intervals <- findInterval(1:nrow(data), c(0, ints$x))

    ## Make plot
    matplot(data, type="n", col=2:3, lty=1, lwd=4)
    legend("topright", c("A", "B"), col=3:2, lty=1, lwd=2)

    ## Draw the polygons
    for (i in seq_along(table(intervals))) {
        xstart <- ifelse(i == 1, 0, ints$x[i-1])
        ystart <- ifelse(i == 1, dat[1,ints$pindex[1]], ints$y[i-1])
        xend <- ints$x[i]
        yend <- ints$y[i]
        x <- seq(nrow(data))[intervals == i]
        polygon(c(xstart, x, xend, rev(x)), c(ystart, data[x,1], yend, rev(data[x,2])),
                col=ints$pabove[i]%%2+2)
    }

    ## Add lines for curves
    if (addLines)
        invisible(lapply(1:2, function(x) lines(seq(nrow(data)), data[,x], col=x%%2+2, lwd=2)))
}

## Plot the data
ichimoku(dat)

enter image description here

答案 1 :(得分:1)

以下是一些适用于问题的简单版本的代码,其中行只交叉一次。然而,我没有对重复的过境进行测试。

# Make toy data
ichimoku <- data.frame(senkouA = rep(10, 10), senkouB = c(3, 5, 4, 7, 10, 11, 15, 12, 13, 14))

# Make indices for the conditions that define the fill colors. They need to intersect for the polygons to connect.
index.green = with(ichimoku, as.logical(senkouA >= senkouB))
index.red = with(ichimoku, as.logical(senkouA <= senkouB))

# Make the line plot
matplot(ichimoku, lty=1, lwd=1, pch=20, type="l", col=c("red","blue"))

# Now add polygons with fill color based on those conditions by subsetting the task using the indices.
with(ichimoku, polygon(x = c(seq(length(senkouA))[index.green], rev(seq(length(senkouA))[index.green])),
    y = c(senkouB[index.green], senkouA[index.green]), col = "green"))
with(ichimoku, polygon(x = c(seq(length(senkouA))[index.red], rev(seq(length(senkouA))[index.red])),
    y = c(senkouB[index.red], senkouA[index.red]), col = "red"))

这是我的结果:

enter image description here