Reputation: 520
I imagine this is a very simple question, but I'm trying to plot a time series in Plotly (R) and everytime I try to plot - the lines are automatically assuming the y axis (i.e. facing horizontally).
From what I understand, this is a problem relating to how my variables are being input into the code. But not entirely sure how to solve this issue...
Assuming this is to do with my variables, I've printed the structure of my dataset below:
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 53 obs. of 2 variables:
$ Date.received: Date, format: "2017-06-29" "2017-06-22" "2017-05-16" "2017-06-23" ...
$ n : num 20 17 14 13 12 12 12 11 11 11 ...
My Plotly code is as follows:
plot_ly(Time, x = Date.received, y = n, mode = "line")
The outcome is:
Many thanks in advance, apologies for the rookie question!
Upvotes: 2
Views: 278
Reputation: 25385
Your data is in the wrong order, it is sorted by decreasing value of n. For a time series it needs to be sorted on the date. Try doing:
Time = Time[order(Time$Date.received),]
So your dataframe is properly sorted, and then plotting with:
plot_ly(Time, x = ~Date.received, y = ~n, mode = "line")
Note the ~
before the column names Date.received
and n
, which is necessary to let plot_ly know you are referring to column names of the dataframe Time.
Upvotes: 3