1

I've got a data set as

prices<- c(100,101,102,103,104,105,108,107,106,105,104,100,98,97,94,90,88,89,89,90,91,90,92,90,94,95,90,89,84,89,80,91,94,94,95,98,103,110,112,70,65)
date<- Sys.Date() -41:1
data<- xts(prices,date)
lineChart(data)

following the above code i'll be able to get my desired line chart. Now i need my chart to show be data lebels in the chart itself. For this i've tried

plot(date,prices,type="l")
textxy(date, prices, prices)

Now i get the chart which is a line chart and with a data lebel on it. Now what i now need is a chart which shows only a few data points which are high and low during a period. The chart which i need should look like http://img9.imageshack.us/img9/2573/aid8.png

4

1 回答 1

1

首先让我们准备数据

prices<- c(100,101,102,103,104,105,108,107,106,105,104,100,98,97,94,90,88,89,89,90,91,90,92,90,94,95,90,89,84,89,80,91,94,94,95,98,103,110,112,70,65)
date<- Sys.Date() -41:1
library(quantmod)
data<- xts(prices,date)
colnames(data) <- "price"

现在我们要找到局部最大值最小值。我们将使用 ZigZag 叠加来平滑图形

chart_Series(data)
data$n <- 1:nrow(data)
data$z <- ZigZag(data$price , change = 2 , percent = T)
add_TA(data$z , on = 1 ,col = 'red' , lty = 3 , type = 'l')
ex <- data[c(findPeaks(data$z) , findValleys(data$z)) - 1 , ]
add_TA(ex$z , on = 1 , col = 'red' , cex = 2 , type = 'p')

现在我们剩下的就是添加标签

text(x = ex$n , y = (ex$z) * 0.99 , label = ex$price)

结果

在此处输入图像描述

于 2013-08-09T07:49:18.973 回答