2017-04-02 48 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