I have a time-series data frame:
The column "Contas.Resultado" is ordered correctly by ascending order of "Valor" column.
My problem is:
When I plot the chart, the legend doesn't appear in the correct order, consequently, some geom_area plot overlap others.
ggplotly(tbl1 %>% ggplot(aes(Ano, Valor)) + 
  geom_area(aes(fill = Contas.Resultado), 
            position = position_dodge(width = 0), stat = "identity") +    
  scale_x_yearmon() +
  xlab("") +
  ylab("R$ (milhões)")) 
I got the right results using this:
df$Contas.Resultado <- factor((df$Contas.Resultado), 
                              levels = c("Faturamento", "Receita Líquida", 
                                         "Resultado Bruto", "EBTIDA R$", "EBTI",
                                         "Resultado Líquido"))
I would appreciate if someone knows a straightforward way to solve this


 
     
    