简体   繁体   中英

Plotly R : Order by month name for axis

I have a df which when plotted as a graph using plotly arranges the axis alphabetically than in the correct order of april ,may ,june. How can this be fixed.

   Month_considered   pct ATC_Count
   <chr>            <dbl> <fct>    
 1 Apr-17            1.62 6,004    
 2 May-17            1.60 6,671    
 3 Jun-17            1.56 6,979    
 4 Jul-17            1.56 6,935    
 5 Aug-17            1.55 7,225    
 6 Sep-17            1.50 6,684    
 7 Oct-17            1.45 6,451    
 8 Nov-17            1.41 6,460    
 9 Dec-17            1.39 6,715    
10 Jan-18            1.39 6,427    
11 Feb-18            1.54 6,844.00 

plot_ly(ab, x = ~Month_considered, y = ~pct, type = 'scatter',mode = 'markers',line = list(color = 'rgb(205, 12, 24)', width = 4))

If you convert Month_considered to an R Date type with something like the example below you'll get a proper date axis. (Note that you have to spoof in a day value for as.Date() to parse properly)

plot_ly(ab,
        x = ~as.Date(paste0("01-",Month_considered),format = "%d-%b-%y"),
        y = ~pct,
        type = 'scatter',
        mode = 'markers',
        line = list(color = 'rgb(205, 12, 24)',
                    width = 4))

If for some reason you didn't want a true date axis, then you could convert your Month_considered column to an ordered factor and specify the proper order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM