2017-04-02 66 views
0

即时通讯新的情节,我有问题给你。图中密谋R

我有日期和值数据集是这样的:

date    value 
01/01/2001  5 
.... 
01/01/2010  25 

和IM使用此代码绘制它:

plotr<- plot_ly(data,x = ~date, y = ~var,name='X', 
         type="scatter",mode='lines', 
         line = list(color = 'rgb(0, 0, 102)', width = 2)) %>% 
    layout(title = "My first graph", 
     xaxis = list(title = "date"), 
     yaxis = list (title = "number")) 
    plotr 

它工作正常,但我想提出一个音符一个日期例如,在2013年他的价值...我搜查plotly网站,我发现它,但我不能查看我的笔记...

https://plot.ly/r/line-charts/

任何人都可以帮忙?

+0

我看不到任何 '文本' 参数中的签名plot_ly函数 – tagoma

+0

@e douard你看到我的网页了吗?它的剧情网站。你会发现它更“广泛地标注带注释的线条” – RMteam

+0

,在上面写下的几行中,没有添加注释的命令。因此,它不会出现在您的图表上。 – tagoma

回答

0

下面的代码可以帮助您吗?

library(plotly) 

x <- as.Date(c('2011-01-01','2012-01-01', '2013-01-01', '2014-01-01')) 
y <- c(34, 24, 39, 15) 
data <- data.frame(x, y) 

annotation <- list(
x = data$x[2], 
y = data$y[2], 
text = 'My annotation', 
showarrow = TRUE) 

p <- plot_ly(data, x=~x) %>% 
    add_trace(y=y, mode='lines') %>% 
    add_trace(x=~c(x[1], x[4]), y=~c(y[1], y[4]), type='scatter') %>% 
    layout(title='My graph', annotations=annotation) 

enter image description here

0

为您的文档https://plot.ly/r/text-and-annotations/

而且随着你的榜样:

date <- as.Date(c('2014-02-01','2015-01-11', '2016-03-01', '2017-02-01')) 
var<- c(37, 54, 110, 125) 
data <- data.frame(date, var) 

plot_ly(data,x = ~date, y = ~var, name='X', 
    type="scatter",mode='lines', 
    line = list(color = 'rgb(0, 0, 102)', width = 2)) %>% 
add_annotations(x="2015-01-11", 
       y=data$var[data$date=="2015-01-11"], 
       text=data$var[data$date=="2015-01-11"]) 

enter image description here