简体   繁体   English

绘图R:按轴的月份名称排序

[英]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. 我有一个df,当使用图以图形方式绘制该图时,该轴按字母顺序排列,而不是按4月,5月,6月的正确顺序排列。 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. 如果使用类似下面的示例将Month_considered转换为R Date类型,则将获得正确的日期轴。 (Note that you have to spoof in a day value for as.Date() to parse properly) (请注意,您必须欺骗as.Date()的天值才能正确解析)

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. 如果由于某种原因您不希望使用真正的日期轴,则可以将Month_considered列转换为有序因子并指定正确的顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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