r - Edit time series plot -


i have dataset sales_history. here dput of first 15 lines

sales_history <- structure(list(month = c("2008/01", "2008/02",      "2008/03", "2008/04", "2008/05", "2008/06", "2008/07",      "2008/08", "2008/09", "2008/10", "2008/11", "2008/12",      "2009/01", "2009/02", "2009/03"),      sales= c(941, 1275, 1908, 2152, 1556,      3052, 2627, 3244, 3817, 3580, 444,      3332, 2823, 3407, 4148 )),      .names = c("month", "sales"),      row.names = c(na, 15l),      class = "data.frame") 

i have months 2008/01 until 2013/10. did auto arima forecast on using:

arimaforecast<-function(df) {     ts1<- ts(df$sales, frequency=12, start=c(2008,1))     fit<-auto.arima(ts1,ic="bic")      plot1=plot(forecast(fit,h=20))     return(plot1) } arimaforecast(sales_history) 

then want plot time series. wrote below.

y <- ts(sales_history$sales,freq=12,start=c(2011,1),end=c(2013,10)) yt <- window(y,end=c(2013,4)) yfit <- auto.arima(yt,ic="bic") yfor <- forecast(yfit,h=10) plot(yfor, main="sales forecasting", sub="optimal arima model",     xlab="month", ylab="sales") lines(fitted(yfor),col="blue") lines(y,col="red") 

then, graph turns out ugly. how produce better graph following?

  1. y-axis not show 1e+06, 3e+06, rather, 1m, 3m, etc. , also,
  2. use green histograms (that is, bars) show history sales data, while still using lines (with connected dots) show fitted history , forecasts?

i'm still not entirely sure mean bars since there no green line in graph (blue , red only), here's stab @ plot combines different features think you're looking for. since plot bit more complex , i'm not familiar plotting functions available forecast, implemented ggplot2 package, makes nice graphs , provides lot of flexibility adjustments (see ggplo2 detailed documentation).

the first part of code takes forecast object yfor code example , turns data frame that's easy use in ggplot (you can improve section using date object instead of numeric timescale if you'd lot more flexibility in x-axis labeling), second part plots (plot rather cut off since subset of data work whole data set).

# convert forecast object data frame ts_values <- data.frame(     time = as.numeric(time(yfor$x)),       sales = as.numeric(yfor$x),      fit = as.numeric(yfor$fitted)) ts_forecast <- data.frame(     time = as.numeric(time(yfor$mean)),     fit = as.numeric(yfor$mean),     upper.80 = as.numeric(yfor$upper[,1]),     upper.95 = as.numeric(yfor$upper[,2]),     lower.80 = as.numeric(yfor$lower[,1]),     lower.95 = as.numeric(yfor$lower[,2]))  # combine fitted data , forecast mean ts_values <- rbind(ts_values, transform(ts_forecast[c("time", "fit")], sales = na))  # plot library(ggplot2) ggplot(null, aes(x = time)) +      geom_bar(data = ts_values, aes(y = sales), stat = "identity",               fill = "dark green", position="dodge") +      geom_line(data = ts_values, aes(y = fit), colour = "red", size = 2) +      geom_ribbon(data = ts_forecast, aes(ymin = lower.95,  ymax = upper.95),                   alpha=.2,  fill="red") +      geom_ribbon(data = ts_forecast, aes(ymin = lower.80,  ymax = upper.80),                   alpha=.2,  fill="red") +     scale_y_continuous(labels = function(x) paste(x/10^6, "m"), expand = c(0,0)) +     theme_bw() 

enter image description here


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -