[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
Thu Jun 20 17:04:27 CEST 2019
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
More information about the R-help
mailing list