RMteam
RMteam

Reputation: 113

graph in plotly R

Im new with plotly and i have question for you.

I have a dataset with date and value like this:

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

and im plotting it with this code:

   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

It works fine but i want to put a note in one date for example in 2013 with his value... I searched in plotly website and i found it but i cant view my note...

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

anyone could help?

Upvotes: 0

Views: 1011

Answers (2)

shosaco
shosaco

Reputation: 6165

The documentation for you is https://plot.ly/r/text-and-annotations/

And with your example:

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

Upvotes: 0

tags
tags

Reputation: 4059

Does the code below help you?

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

Upvotes: 2

Related Questions