R-具有不同颜色的两条交叉线之间的阴影区域 [英] R - shade area between two crossing lines with different colors

查看:134
本文介绍了R-具有不同颜色的两条交叉线之间的阴影区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含516行和2列的矩阵(名为ichimoku),每个矩阵都包含要绘制的值,目标是为

I have a matrix (named ichimoku) with 516 rows and 2 columns ,each one of them containing values to be plotted, the goal is to recreate the clouds for the Ichimoku strategy. Using matpot, I am able to plot these two curves but what I want is to shade the area between the two curves. I have two problems :

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

  • I tried using polygon to shade the area but it does not work. I suspect it is because the two series (senkouA and senkouB) cross several times on the plot instead of having one always greater than the other

我希望该区域在senkouA> senkouB时为绿色,而在senkouB> senkouA时为红色,但是从我的理解中,多边形只能是一种颜色.

I would like the area to be shaded in green if senkouA>senkouB and in red if senkouB>senkouA but from what I read the polygon can only be of one color.

是否还有其他功能可以帮助我实现所需的多边形,即当senkouA> senkouB时senkouA和senkouB之间的绿色阴影区域,以及当senkouB> senkouA时红色的阴影区域?

Is there an other function that polygon which might help me achieve what I am looking for, that is a shade area in green between senkouA and senkouB when senkouA>senkouB and a shade area in red when senkouB>senkouA ?

ichimoku矩阵如下所示(第一列是senkouA,另一列是senkouB)

The ichimoku matrix looks like this (the first column is senkouA, the other senkouB)

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

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

here is my matplot function (which works):

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

和我的多边形函数(不是):

and my polygon function (which doesn't):

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

推荐答案

如果找到曲线之间的交点,则可以在交点之间绘制多边形.这是对先前 post 的修改,在该文章中,他们发现了曲线之间的交点,并且绘制多边形的功能.

If you find the intersections between the curves, then you can draw the polygons between the intersections. Here is a modification of a previous post where they find intersections between curves, and a function to draw the polygons.

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

这篇关于R-具有不同颜色的两条交叉线之间的阴影区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆