R : dynamic plot value and x axis value for line chart

Hi Experts,

Need help on r for forecasted line chart, Please find the attachment for details.

Issue:

  1. As of now, x axis has 2017.0, 2017.5 But I need dynamically plotting for x axis yearmonth like 201701,201702, etc.

  2. Need plotting value i.e population value on chart including the forecasted value.

Below is the code used for chart:

library(forecast)
ord <- order(ds$`Year`,ds$`Month`)
sds <- ds[ord,]
fRec <- sds[1,]
mon <- as.numeric(firstRec$'Month')    
yrs <- as.numeric(as.character(firstRec$'Year'))
tm <- ts(data = sds$Population , start= c(yrs,mon) ,frequency = 12)
plot(tm)
tm[is.na(tm)] <-0
hw <- HoltWinters(tm)
plot(forecast(hw,h=3))

R_screenShot

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

Thanks,
Red

Hi! Welcome!

I'm afraid I don't have an answer for you, but I do have some advice to hopefully help you get an answer faster:

  1. This doesn't look like a question specifically about the RStudio IDE, so it would be a good idea to move it to a different category (you might also want to take a look at our RStudio vs R FAQ).

  2. Thanks for including your code! Around here, it's considered both friendly and good practice to format your code as code — just select it and click the </> button at the top of the posting box (you can edit your post by clicking the little grey pencil at the bottom).

  3. The quickest path to an answer is to post a self-contained reproducible example. You're pretty close, but you're missing a non-screenshot sample of your data. It's easiest for people to help you if they can run your code on their own computer to debug and/or tweak it, and that's quite difficult right now. This thread has several ideas for how to post your data here (if your data are unavoidably large, see the discussion over here, instead).

Hi Jcblum,

Thanks for suggestions.I have updated my post.

Thanks,
Red

1 Like