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))

You should convert the Month_considered column to a date, e.g. using lubridate.

Then you can arrange the data in order of month.

3 Likes

I always find dealing with dates tricky, so I thought you might like:


library(dplyr)
library(readr)
library(lubridate)

dat_df <- read_delim("
row_number Month_considered pct ATC_Count
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",
                     skip = 1,
   delim = " ")

# with lubridate
dat_df %>% 
  mutate(
    Month_considered = lubridate::dmy(paste(01, Month_considered))
  )
#> # A tibble: 11 x 4
#>    row_number Month_considered   pct ATC_Count
#>         <int> <date>           <dbl>     <dbl>
#>  1          1 2017-04-01        1.62      6004
#>  2          2 2017-05-01        1.6       6671
#>  3          3 2017-06-01        1.56      6979
#>  4          4 2017-07-01        1.56      6935
#>  5          5 2017-08-01        1.55      7225
#>  6          6 2017-09-01        1.5       6684
#>  7          7 2017-10-01        1.45      6451
#>  8          8 2017-11-01        1.41      6460
#>  9          9 2017-12-01        1.39      6715
#> 10         10 2018-01-01        1.39      6427
#> 11         11 2018-02-01        1.54      6844
# base-r
dat_df %>% 
  mutate(
    Month_considered = as.Date(paste(01, Month_considered), format = "%d %b-%y")
  )
#> # A tibble: 11 x 4
#>    row_number Month_considered   pct ATC_Count
#>         <int> <date>           <dbl>     <dbl>
#>  1          1 2017-04-01        1.62      6004
#>  2          2 2017-05-01        1.6       6671
#>  3          3 2017-06-01        1.56      6979
#>  4          4 2017-07-01        1.56      6935
#>  5          5 2017-08-01        1.55      7225
#>  6          6 2017-09-01        1.5       6684
#>  7          7 2017-10-01        1.45      6451
#>  8          8 2017-11-01        1.41      6460
#>  9          9 2017-12-01        1.39      6715
#> 10         10 2018-01-01        1.39      6427
#> 11         11 2018-02-01        1.54      6844

Created on 2018-07-22 by the reprex package (v0.2.0.9000).

4 Likes