2013-12-10 30 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