Plotly Time series forecasting in R - modify default x axis and y axis range


Plotly Time series forecasting in R - modify default x axis and y axis range



I have a query on Time series forecasting with plotly. I have built a forecasting chart however few things needs to be changed but its not working for me. Please find the attachment for details and kindly let me know the correction or suggestion for R code.



Query:



currently X Axis is showing 2,016.5, 2017 , 2017.5 , etc however I want it to show yearmonth like 2016.04,2014,05,etc.
Please be informed yearmonth is a field in data, please refer attached data



currently Y Axis is showing lables with the difference of 50 K , however I want it to show as 5 K, 10K,15K, etc.



Below is R code used :


library(forecast)
library(plotly)
ord <- order(ds$`Calendar Year-DISPLAY_KEY`,ds$`Calendar Month-DISPLAY_KEY`)
sds <- ds[ord,]
firstRec <- sds[1,]
mn <- as.numeric(firstRec$'Calendar Month-DISPLAY_KEY')
yr <- as.numeric(as.character(firstRec$'Calendar Year-DISPLAY_KEY'))
tm <- ts(data = sds$Calc_Best_DSO , start= c(yr,mn) ,frequency = 12)
plot(tm)
tm[is.na(tm)] <-0

fit <- ets(tm)
fore <- forecast(fit, h = 3, level = c(80, 95))

plot_ly() %>%
add_lines(x = time(tm), y = tm,hoverinfo = "text",
color = I("black"), name = "observed",text= paste("Month: ",sds$`Calendar Month-DISPLAY_KEY`,
"<br>","Year: ",sds$`Calendar Year-DISPLAY_KEY`,
"<br>","DSO: ",sds$Calc_Best_DSO)) %>%
add_ribbons(x = time(fore$mean), ymin = fore$lower[, 2], ymax = fore$upper[, 2],
color = I("gray95"), name = "95% confidence") %>%
add_ribbons(x = time(fore$mean), ymin = fore$lower[, 1], ymax = fore$upper[, 1],
color = I("gray80"), name = "80% confidence") %>%
add_lines(x = time(fore$mean), y = fore$mean, color = I("blue"), name = "prediction")



Below is sample data:


Month,Year,YearMonth,Population
1,2017,201701,100
1,2018,201801,300
2,2018,201802,310
3,2018,201803,320
4,2018,201804,330
2,2017,201702,200
3,2017,201703,300
4,2017,201704,400
5,2017,201705,500
6,2017,201706,600
7,2017,201707,700
8,2017,201708,800
9,2017,201709,900
10,2017,201710,1000
11,2017,201711,1100
12,2017,201712,1200



Data



Chart




2 Answers
2



The best way to customize your axis in plotly is to set a variable with the options you want.



For the x-axis, it would look similar to the code below.


a <- list(
autotick = FALSE,
tick0 = 0,
dtick = 1)



This will only show ticks on the x-axis every interval of 1. I realize this was not what you were asking. Using dtick = .01, will make the x-axis hard to read.



For the y-axis, it would look similar to the code below.


b <- list(
autotick = FALSE,
tick0 = 0,
dtick = 5000) #for 5k intervals



Now you must simply input these into your plotly code. Here is an example.


plot_ly()%>%
add_lines(x = time(tm) y = tm, hoverinfo = text, color = I("black"), name = "Observed") %>%
layout(xaxis = a, yaxis = b)



This should work. All you will need to do is define a and b, then simply add the layout to your current plotly code.



Hope this was helpful.





Hi M. Wickers, Thanks for your response. Y axis worked perfectly as expected. However, XAxis is not working. After your code its showing 2017 and 2018. I need to show as per my YearMonth Feild. Please find the attachment from my question. My Data starts from 2017.01 till 2018.04 and I need XAxis to be 2017.01, 2017.02...etc....till 2018.04. Please provide your suggestions.
– red rock
Jul 2 at 16:33






Sorry for the delay. I have not been able to find a clear way to do it. To me, it makes the x-axis unreadable. That is why I suggested only doing 1 year increments.
– M. Wickers
yesterday



You want to format your dates as R dates. That would help with formatting.


R


date


library(plotly)
x <- read.table(
text = 'Month,Year,YearMonth,Population
1,2017,201701,100
1,2018,201801,300
2,2018,201802,310
3,2018,201803,320
4,2018,201804,330
2,2017,201702,200
3,2017,201703,300
4,2017,201704,400
5,2017,201705,500
6,2017,201706,600
7,2017,201707,700
8,2017,201708,800
9,2017,201709,900
10,2017,201710,1000
11,2017,201711,1100
12,2017,201712,1200',
header = TRUE, sep = ','
)
x$YearMonth <- as.Date(paste0(x$YearMonth, '01'), format = '%Y%m%d') # Formatting as dates
x$Population <- x$Population * 100 # Scaling population to show large numbers
x <- x[with(x, order(YearMonth)), ] # Sorting by date
p <- plot_ly(
data = x, x = ~YearMonth, y = ~Population, type = 'scatter', mode = 'lines'
) %>% layout(
xaxis = list(tickformat = '%Y.%m'), # This formatting option should help with your desired format
yaxis = list(tick0 = min(x$Population), dtick = 5000, tickformat = '.2s') # Be sure to include min for tick0
)
p



This results in the following graph -
enter image description here






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to make file upload 'Required' in Contact Form 7?

Rothschild family

amazon EC2 - How to make wp-config.php to writable?