【问题标题】:R - shade area between two crossing lines with different colorsR - 两条不同颜色的交叉线之间的阴影区域
【发布时间】:2015-08-19 10:06:05
【问题描述】:

我有一个包含 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')

【问题讨论】:

  • 你看过quantmod包吗?
  • @Pascal 不幸的是,我认为quantmod 不包含 Ichimoku 图表。但是this 博客可能会很有趣。
  • 一目指标上还有一个github post
  • @RHertel 是的,我与布林带似乎混为一谈。很抱歉造成混乱。
  • 我必须重新编程 Ichimoku 策略,因此无法使用现有的界面。但是,感谢 github 链接,这将有所帮助。

标签: r plot polygon


【解决方案1】:

如果找到曲线之间的交点,则可以在交点之间绘制多边形。这是对以前的 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)

【讨论】:

  • 我工作得很好,除了一些问题:我在 ints df 中的第一个 pindex 等于 63(然后是 98 和 105),所以第一个 ystart 不起作用,因为 dat 中只有两列,不是 63。我用 2 替换了 ints$pindex[1] 并且它对除最后一个以外的多边形进行了 polts。谢谢你的帖子!
【解决方案2】:

这里有一些代码适用于您的问题的简单版本,其中的行只交叉一次。不过,我还没有测试过它是否可以重复穿越。

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

这是我的结果:

【讨论】:

  • 感谢您展示如何做到这一点,我会继续寻找重复的交叉口
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-23
  • 2013-10-10
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
相关资源
最近更新 更多