R plotly chart based on multiple columns

plotly

#1

I have a df which can have 2 or more columns with the first one month always fixed.I am trying to plot them using plotly r. As of now it has three columns: month,apple,orange. Based on analysis it can have another column banana. Below is the code I am using right now but it even takes the column month for y-axis. How do I fix this:

> sample_test
    month apple orange
2  Aug-17     2      1
3  Dec-17     2      1
4  Feb-18     2      1
5  Jan-18     2      1
6  Jul-17     2      1
7  Jun-17     2      1
8  May-17     2      1
9  Nov-17     2      1
10 Oct-17     2      1
11 Sep-17     2      1

p<- plot_ly(sample_test, x = sample_test$month,  name = 'alpha', type = 'scatter', mode = 'lines',
            line = list(color = 'rgb(24, 205, 12)', width = 4)) %>% 
  layout(#title = "abbb",
    xaxis = list(title = "Time"),
    yaxis = list (title = "Percentage"))

for(trace in colnames(sample_test)){
  p <- p %>% plotly::add_trace(y = as.formula(paste0("~`", trace, "`")), name = trace)
}
p

The output looks like this :enter image description here


#2

The first problem is that month looks to be a string, so the months are in the wrong order. You should convert that column to the date format first.

As for the content of the plot, it appears to be mixing a scatter plot and line plot. You should check existing examples and adapt the code to get what you are after.