【问题标题】:Construct a specific plot of time series using R使用 R 构建特定的时间序列图
【发布时间】:2013-12-10 18:46:47
【问题描述】:

我的问题是我从正态分布生成了一个时间序列并绘制了我的时间序列,但我想将时间序列和轴 X 之间的正区域涂成红色,轴 X 下方的负区域也是如此和我的时间序列。

这是我使用的代码,但它不起作用:

 x1<-rnorm(250,0.4,0.9)
x <- as.matrix(x1)
t <- ts(x[,1], start=c(1,1), frequency=30)
plot(t,main="Daily closing price of Walterenergie",ylab="Adjusted close Returns",xlab="Times",col="blue")

plot(t,xlim=c(2,4),main="Daily closing price of Walterenergie",ylab="Adjusted close Returns",xlab="Times",col="blue")
abline(0,0)  

z1<-seq(2,4,0.001)
cord.x <- c(2,z1,4) 
cord.y <- c(0,t(z1),0) 
polygon(cord.x,cord.y,col='red')

【问题讨论】:

    标签: r datetime plot


    【解决方案1】:

    编辑:响应 OP 的附加查询。

    library(ggplot2)
    df      <- data.frame(t=1:nrow(x),y=x)
    df$fill <- ifelse(x>0,"Above","Below")
    ggplot(df)+geom_line(aes(t,y),color="grey")+
      geom_ribbon(aes(x=t,ymin=0,ymax=ifelse(y>0,y,0)),fill="red")+
      geom_ribbon(aes(x=t,ymin=0,ymax=ifelse(y<0,y,0)),fill="blue")+
      labs(title="Daily closing price of Walterenergie",
           y="Adjusted close Returns",
           x="Times")
    

    原文回复:

    这是你的想法吗?

    library(ggplot2)
    df <- data.frame(t=1:nrow(x),y=x)
    ggplot(df)+geom_line(aes(t,y),color="grey")+
      geom_ribbon(aes(x=t,ymin=0,ymax=y),fill="red")+
      labs(title="Daily closing price of Walterenergie",
           y="Adjusted close Returns",
           x="Times")
    

    【讨论】:

    • 谢谢你的回答,很完美,但是我怎样才能把斧头X下面的部分涂成蓝色,我想把它分成两个区域,红色的正面部分和蓝色的负面部分。
    • 非常感谢这是完美的,并回答了我的问题。现在,我如何计算绿色部分和红色部分的面积。
    【解决方案2】:

    这是我不久前为某人编写的一些代码。在这种情况下,正面和负面使用两种不同的颜色。虽然这不是你所追求的,但我想我会分享这个。

    # Set a seed to get a reproducible example
    set.seed(12345)
    
    num.points <- 100
    
    # Create some data
    x.vals <- 1:num.points
    values <- rnorm(n=num.points, mean=0, sd=10)
    
    # Plot the graph
    plot(x.vals, values, t="o", pch=20, xlab="", ylab="", las=1)
    abline(h=0, col="darkgray", lwd=2)
    
    # We need to find the intersections of the curve with the x axis
    # Those lie between positive and negative points
    # When the sign changes the product between subsequent elements
    # will be negative
    crossings <- values[-length(values)] * values[-1]
    crossings <- which(crossings < 0)
    
    # You can draw the points to check (uncomment following line)
    # points(x.vals[crossings], values[crossings], col="red", pch="X")
    
    # We now find the exact intersections using a proportion
    # See? Those high school geometry problems finally come in handy
    intersections <- NULL
    for (cr in crossings)
      {
      new.int <- cr + abs(values[cr])/(abs(values[cr])+abs(values[cr+1]))
      intersections <- c(intersections, new.int)
      }
    
    # Again, let's check the intersections
    # points(intersections, rep(0, length(intersections)), pch=20, col="red", cex=0.7)
    
    last.intersection <- 0
    for (i in intersections)
      {
      ids <- which(x.vals<=i & x.vals>last.intersection)
      poly.x <- c(last.intersection, x.vals[ids], i)
      poly.y <- c(0, values[ids], 0)
      if (max(poly.y) > 0)
        {
        col="green"
        }
      else
        {
        col="red"
        }
      polygon(x=poly.x, y=poly.y, col=col)
    
      last.intersection <- i
      }
    

    这里是the result

    【讨论】:

    • 非常感谢这是完美的,并回答了我的问题。现在,我如何计算绿色部分和红色部分的面积。
    • 两个字:梯形规则。
    • 我本来想用这段代码的附加容量巧妙地概括为应该选择它的“按部分区域”问题。
    • 我正在考虑使用梯形:使用以下代码但我需要将我的时间序列转换为函数Pas&lt;- 0.00005 X1&lt;- seq(0,250,Pas) trapezoid &lt;- function(x0,y0) sum(diff(x0)*(y0[-1]+y0[-length(y0)]))/2 int&lt;-trapezoid(x1,FS1)
    【解决方案3】:

    基础绘图解决方案:

    x1<-rnorm(250,0.4,0.9)
    x <- as.matrix(x1)
    # t <- ts(x[,1], start=c(1,1), frequency=30)
    plot(x1,main="Daily closing price of Walterenergie",ylab="Adjusted close Returns",xlab="Times",col="blue", type="l")
    polygon( c(0,1:250,251), c(0, x1, 0) , col="red")
    

    请注意,这不涉及时间序列绘图方法,由于频率值和起始 x 值 1 的缩放比例不同,这种方法很难理解。解决方案如下:

    plot(t,main="Daily closing price of Walterenergie",
             ylab="Adjusted close Returns",xlab="Times",col="blue", type="l")
    polygon( c(1,1+(0:250)/30), c(0, t, 0) , col="red")
    

    【讨论】:

      猜你喜欢
      • 2016-05-11
      • 2012-12-10
      • 2022-08-14
      • 1970-01-01
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      • 2018-10-22
      • 1970-01-01
      相关资源
      最近更新 更多