在折线图中绘制数据标签 [英] Plot data labels in a line chart

查看:151
本文介绍了在折线图中绘制数据标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,

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)

现在,我得到的图表是折线图,上面有数据图表。现在,我现在需要的是一个图表,该图表仅显示一段时间内高低两个数据点。
我需要的图表应类似于
http:// img9 .imageshack.us / img9 / 2573 / aid8.png

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

推荐答案

首先让我们准备数据

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叠加层来平滑图形

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)

结果为

这篇关于在折线图中绘制数据标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆