[R] How to change x axes labels in plot_ly?

@steii m@iii@g oii gm@ii@com @steii m@iii@g oii gm@ii@com
Wed Jun 26 20:11:49 CEST 2019


Thanks a lot John.

> I have never heard of ploty. Is that a typo?
Plotly is a library and the function for visualizing the chart is actually called "plot_ly". I learned about this in an Udemy course, I thought is the best function to create line charts, bar charts, etc. What function do you use for that?

I have read the articles, and I created a Gist example on Github:
https://gist.github.com/nstefi/f67f8cbc171a2dc6e4b8cc876dfd0c81

Here is the code:
library(plotly)

chart_data <- structure(list(`Month-Day` = c("05-01", "05-15", "05-31", "06-01", 
"06-15", "07-01", "07-15", "08-01", "08-15", "09-01", "09-15", 
"10-01", "10-15", "11-01", "11-15", "12-01", "12-15", "01-01", 
"01-15", "02-01", "02-15", "03-01", "03-15", "04-01", "04-15", 
"05-01", "05-15", "06-01", "06-15", "07-01", "07-15"), FiscalYear_Days = c(0, 
14, 30, 31, 45, 61, 75, 92, 106, 123, 137, 153, 167, 184, 198, 
214, 228, 245, 259, 276, 290, 304, 318, 335, 349, 365, 379, 396, 
410, 426, 440), Last_Year = c(16, 17, 22, 25, 28, 31, 34, 37, 
40, 44, 47, 50, 53, 56, 60, 63, 66, 69, 72, 76, 79, 82, 85, 88, 
92, 95, 98, 101, 104, 108, 111), This_Year = c(10, 14, 18, 18, 
22, 24, 27, 30, 33, 36, 38, 41, 44, 47, 50, 52, 55, 58, 61, 64, 
66, 69, 72, 75, 78, 80, 83, 86, 89, 92, 94)), row.names = c(NA, 
31L), class = "data.frame")

plot_ly(chart_data, 
        x = ~`Month-Day`,
        y = ~Last_Year,
        type = 'scatter',
        mode = 'lines'
) %>% 
  layout(xaxis = list(categoryarray = chart_data$FiscalYear_Days, categoryorder = "array"))

It would be nice if I could provide some screenshots. How can we do that in the r-project mailing list? I've read that they recommend to send plain text emails. Do you use any site where you can upload screenshots and provide links?
So if you run the code, you would see some zig-zag shape chart instead of continuously growing line. Also for some reason now I see the x axis labels mixed up, up to half way I see values of "FiscalYear_Days" column, and after that continues with values from "Month-Day" column. I don't get it, it should be one or the other.
My goal was to show the chart based on x values from "FiscalYear_Days", but show the x axis labels from "Month-Day". Because if I simply show based on "Month-Day", the period is longer than a year, and some values are repeating, and it ends up with a zig-zag line.

Thanks,
Steven

-----Original Message-----
From: John Kane <jrkrideau using gmail.com> 
Sent: Sunday, June 23, 2019 9:56 AM
To: nstefi using gmail.com
Cc: R. Help Mailing List <r-help using r-project.org>
Subject: Re: [R] How to change x axes labels in plot_ly?

Could you supply some code and sample data. The data should be in
dput() format. The way the data has arrived is very difficult to read.

I have never heard of ploty. Is that a typo?

To answer your direct question, yes you should be able to change the x-axis labels but we need to get some idea of what exactly you are doing before we can easily suggest a solution.

I would recommend reading this two links to get an idea of how to present a question in order to get the besc reply http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

 http://adv-r.had.co.nz/Reproducibility.html

On Thu, 20 Jun 2019 at 11:05, <nstefi using gmail.com> wrote:
>
> Hi everyone,
>
>
>
> I have been struggling with this for a while, and I hope someone can 
> give me some advice.
>
> I have built a line chart that shows a cumulated value of number of 
> transactions, and there are 2 lines on the chart comparing last year 
> with this year.
>
> The way I matched last year's data with this year's data is by the 
> month and day (without the year), so my data looks like this:
>
>
> Month-Day
>
> FiscalYear_Days
>
> Last_year
>
> This_Year
>
>
> 05-01
>
> 0
>
> 16
>
> 10
>
>
> 05-02
>
> 1
>
> 17
>
> 14
>
>
> 05-03
>
> 2
>
> 22
>
> 18
>
>
> 05-04
>
> 3
>
> 25
>
> 18
>
>
>
> chart_data <- matrix(c("05-01", "05-02", "05-03", "05-04",
>
>                 "0", "1", "2", "3",
>
>                 "16", "17", "22", "25",
>
>                 "10", "14", "18", "18"),
>
>                 nrow=4, ncol=4, dimnames=list(c("A","B","C","D"), 
> c("Month-Day","FiscalYear_Days", "Last_year", "This_Year")))
>
>
>
> Since our fiscal year doesn't start on January 1st, I had to introduce 
> this column called "FiscalYear_Days", which is the number of days 
> passed since 1st day of fiscal year, and matched last year and current 
> year by that instead. Otherwise I had a problem with plot_ly() showing 
> the chart starting with January 1st on the left.
>
>
>
> Now I figured out a way to change the order of the x axis labels to 
> make it based on the "FiscalYear_Days" instead. This is a simplified plot:
>
> plot_ly(renewals_chart_data,
>
>         x = ~`Month-Day`,
>
>         y = ~Last_Year,
>
>         type = 'scatter',
>
>         mode = 'lines'
>
> ) %>%
>
>   layout(xaxis = list(categoryarray = 
> renewals_chart_data$FiscalYear_Days,
> categoryorder = "array"))
>
>
>
> My problem now is that if the period is longer than 1 year, under 
> Month-Day column I will have some repeating values starting from 
> following year May 1st (05-01, 05-02, etc.), and the plotly chart 
> shows an increasing line that returns back to the left side when reaches the next year's May 1st.
>
> How can I make the line to continue on the right?
>
> If I change plotly to show the x axis based on "FiscalYear_Days", the 
> line is right, but I want at the bottom to show the month and day as labels:
>
> plot_ly(renewals_chart_data,
>
>         x = ~`FiscalYear_Days`,
>
>         y = ~Last_Year,
>
>         type = 'scatter',
>
>         mode = 'lines'
>
> ) %>%
>
>   layout(xaxis = list(categoryarray = 
> renewals_chart_data$FiscalYear_Days,
> categoryorder = "array"))
>
>
>
> Is it possible to just change the x axis label values?
>
>
>
> Thanks a lot,
>
> Steven
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see 
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



--
John Kane
Kingston ON Canada



More information about the R-help mailing list