2013-12-10 32 views
3

我的问题是我从正态分布产生一个时间序列,并绘制了我的时间序列,但我想用红色将时间序列和ax X之间的正面区域斧头X下面的负面区域和我的时间序列也一样。使用R构造一个具体的时间序列图R

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

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

enter image description here

回答

4

编辑:针对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") 

原始响应:

这是你脑子里有什么?

enter image description here

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") 
+1

谢谢你的回答,这是完美的,但是我怎样才能将斧头X下方的部分着色为蓝色,我想分享它的两个区域,正面部分是红色,负面是蓝色。 – Lea

+0

请参阅上面的修改。很高兴它对你有效。 – jlhoward

+0

非常感谢这是完美的,并回答我的问题。现在,我如何计算绿色部分和红色部分的面积。 – Lea

3

这是一些代码,我写了前一阵子的人。在这种情况下,两种不同的颜色用于正面和负面。虽然这不完全是你想要的,但我想我会分享这一点。

# 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

enter image description here

+0

非常感谢这是完美的,并回答我的问题。现在,我如何计算绿色部分和红色部分的面积。 – Lea

+0

两个字:梯形法则。 –

+0

我想这个代码的附加能力可以整齐地概括为“应该选择的区域”部分的问题。 –

1

基地绘图解决方案:

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

注意这不会处理时间序列绘制方法,是因为频率值和结垢的差异而很难理解起始的1溶液以使x值低于:

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

enter image description here