2
I have a graph where the x-axis are the dates, but when ordering chronologically, because before it was in alphabetical order, the information of the graph is wrong because the data follows the alphabetical order, in this case starting in Apr/19 and not in Sep/18 and not going from Sep/18 to Sep/19.
I used that code to sort the months:
base_fox2$mês<-factor(base_fox2$mês, levels = c("set/18","out/18","nov/18",
"dez/18","jan/19","fev/19","mar/19","abr/19","mai/19","jun/19","jul/19",
"ago/19","set/19"))
And this to create the chart with the five variables
library(plotly)
plot_ly(data=base_fox2,name='Fox',x=~base_fox2$mês,y=~base_fox2$quantidade,
type='scatter',mode='lines')%>%
add_trace(y=~base_hb202$quantidade,name='HB20',type='scatter',mode='lines')%>%
add_trace(y=~base_gol2$quantidade,name='Gol',type='scatter',mode='lines')%>% add_trace(y=~base_palio2$quantidade,name='Palio',type='scatter',mode='lines')%>%
add_trace(y=~base_punto2$quantidade,name='Punto',type='scatter',mode='lines')
Try to put
ordered = TRUE
infactor
– Jorge Mendes
If you are going to do operations over the months (e.g. calculating the time between lines), it is best to use the Dates class. See help for
as.Dates
. If this is not the case, sorting the factors (comment above) is a better option.– Carlos Eduardo Lagosta