Ashish Jalan
Ashish Jalan

Reputation: 181

Plot data labels in a line chart

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

Upvotes: 1

Views: 2201

Answers (1)

haki
haki

Reputation: 9759

First lets prepare the data

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"

Now we want to find local maxima minima. We'll use the ZigZag overlay to smooth the graph

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

Now all we have left is to add the labels

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

And the result

enter image description here

Upvotes: 1

Related Questions