0

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?

5
  • I cannot see any 'text' parameter in the signature of the plot_ly function Commented Apr 2, 2017 at 19:45
  • @edouard did you see my page? its plotly website. you will find it "Label Lines with Annotations" Commented Apr 2, 2017 at 19:50
  • more broadly, in the few lines you wrote down above, there is no command to add annotation. thus, it doesn't appear on your graph. Commented Apr 2, 2017 at 19:56
  • @edouard yes! i try but i fall. this is because i open this question... Commented Apr 2, 2017 at 20:06
  • can someone please take a look at my question? stackoverflow.com/questions/65404432/… thanks Commented Dec 22, 2020 at 7:03

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

can you please take a look at my question? stackoverflow.com/questions/65434105/… thanks
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.