R Code: Setting X-Axis As The Selected Dates

    Date:

    Originally posted on SHLee AI Financial Model blog.

    Excerpt

    This post shows a simple R code to set x-axis as some selected dates rather than using 1, 2, 3, … on x-axis.

    match() or which( %in% )

    To find positions in one array of the values in the other array, match() or which( %in% ) can be used. The former is applied to a unique set and the latter to a redundant set.

    In other words, match() only returns the first occurrence of a match.

    #-----------------------------------
    # match() or which( %in% )
    #-----------------------------------
    y <- c(10,20,30,40,50,60,70,80,90,100)
    x <- c(10,30,50,70,90)
     
    # for unique dataset
     
    match(x,y)
    # [1] 1 3 5 7 9
     
    y[match(x,y)]
    # [1] 10 30 50 70 90
     
    # for redundant dataset
     
    which(y %in% x)
    # [1] 1 3 5 7 9
     
    y[which(y %in% x)]
    # [1] 10 30 50 70 90

    Unlike match()which( %in% ) returns the multiple encounters of a match. Here 90 appears twice.

    #-----------------------------------
    # match() or which( %in% )
    #-----------------------------------
    y <- c(10,20,30,40,50,60,70,80,90,90,100)
    x <- c(10,30,50,70,90)
     
    # for unique dataset
     
    match(x,y)
    # [1] 1 3 5 7 9
     
    y[match(x,y)]
    # [1] 10 30 50 70 90
     
    # for redundant dataset
     
    which(y %in% x)
    # [1] 1  3  5  7  9 10
     
    y[which(y %in% x)]
    # [1] 10 30 50 70 90 90

    Finding the selected dates from the whole dates

    A set of dates is unique so that match() function is used. In this example, the last dates for each year (vdate_last) are selected from the whole set of dates (vdate). These selected dates are located at an index set (idx_last).

    #-----------------------------------
    # sample input for dates
    #-----------------------------------
    str_vdate <- "
        2001-01-31 
        2001-02-28 2001-03-30 
        2001-04-30 2001-05-31 2001-06-29 
        2001-07-31 2001-08-31 2001-09-28 2001-10-31
        2001-11-30 2001-12-31 2002-01-31 2002-02-28 2002-03-28
        2002-04-30 2002-05-31 2002-06-28 2002-07-31 2002-08-30
        2002-09-30 2002-10-31 2002-11-29 2002-12-31 2003-01-31
        2003-02-28 2003-03-31 2003-04-30 2003-05-30 2003-06-30
        2003-07-31 2003-08-29 2003-09-30 2003-10-31 2003-11-28
        2003-12-31 2004-01-30 2004-02-27 2004-03-31 2004-04-30
        2004-05-28 2004-06-30 2004-07-30 2004-08-31 2004-09-30
        2004-10-29 2004-11-30 2004-12-31 2005-01-31 2005-02-28
        2005-03-31 2005-04-29 2005-05-31 2005-06-30 2005-07-29
        2005-08-31 2005-09-30 2005-10-31 2005-11-30 2005-12-30
        2006-01-31 2006-02-28 2006-03-31 2006-04-28 2006-05-31
        2006-06-30 2006-07-31 2006-08-31 2006-09-29 2006-10-31
        2006-11-30 2006-12-29 2007-01-31 2007-02-28 2007-03-30
        2007-04-30 2007-05-31 2007-06-29 2007-07-31 2007-08-31
        2007-09-28 2007-10-31 2007-11-30 2007-12-31 2008-01-31
        2008-02-29 2008-03-31 2008-04-30 2008-05-30 2008-06-30
        2008-07-31 2008-08-29 2008-09-30 2008-10-31 2008-11-28
        2008-12-31 2009-01-30 2009-02-27 2009-03-31 2009-04-30
        2009-05-29 2009-06-30 2009-07-31 2009-08-31 2009-09-30
        2009-10-30 2009-11-30 2009-12-31 2010-01-29 2010-02-26
        2010-03-31 2010-04-30 2010-05-28 2010-06-30 2010-07-30
        2010-08-31 2010-09-30 2010-10-29 2010-11-30 2010-12-31
        2011-01-31 2011-02-28 2011-03-31 2011-04-29 2011-05-31
        2011-06-30 2011-07-29 2011-08-31 2011-09-30 2011-10-31
        2011-11-30 2011-12-30 2012-01-31 2012-02-29 2012-03-30
        2012-04-30 2012-05-31 2012-06-29 2012-07-31 2012-08-31
        2012-09-28 2012-10-31 2012-11-30 2012-12-31 2013-01-31
        2013-02-28 2013-03-28 2013-04-30 2013-05-31 2013-06-28
        2013-07-31 2013-08-30 2013-09-30 2013-10-31 2013-11-29
        2013-12-31 2014-01-31 2014-02-28 2014-03-31 2014-04-30
        2014-05-30 2014-06-30 2014-07-31 2014-08-29 2014-09-30
        2014-10-31 2014-11-28 2014-12-31 2015-01-30 2015-02-27
        2015-03-31 2015-04-30 2015-05-29 2015-06-30 2015-07-31
        2015-08-31 2015-09-30 2015-10-30 2015-11-30 2015-12-31
        2016-01-29 2016-02-29 2016-03-31 2016-04-29 2016-05-31
        2016-06-30 2016-07-29 2016-08-31 2016-09-30 2016-10-31
        2016-11-30 2016-12-30 2017-01-31 2017-02-28 2017-03-31
        2017-04-28 2017-05-31 2017-06-30 2017-07-31 2017-08-31
        2017-09-29 2017-10-31 2017-11-30 2017-12-29 2018-01-31
        2018-02-28 2018-03-29 2018-04-30 2018-05-31 2018-06-29
        2018-07-31 2018-08-31 2018-09-28 2018-10-31 2018-11-30
        2018-12-31 2019-01-31 2019-02-28 2019-03-29 2019-04-30
        2019-05-31 2019-06-28 2019-07-31 2019-08-30 2019-09-30
        2019-10-31 2019-11-29 2019-12-31 2020-01-31 2020-02-28
        2020-03-31 2020-04-30 2020-05-29 2020-06-30 2020-07-31
        2020-08-31 2020-09-30 2020-10-30 2020-11-30 2020-12-31
        2021-01-29 2021-02-26 2021-03-31 2021-04-30 2021-05-28
        2021-06-30 2021-07-30 2021-08-31 2021-09-30 2021-10-29
        2021-11-30 2021-12-31"
     
    #-----------------------------------
    # read input date string
    #-----------------------------------
     
    # trimws() : Remove Leading/Trailing Whitespace
    # fill = TRUE : If the rows have unequal length, 
    #               blank fields are implicitly added. 
    vdate <- c(t(read.table(text=trimws(str_vdate), 
                            fill = TRUE)))
     
    vdate <- vdate[vdate != ""]
     
    #===============================================
    # select the last date for each year
    #===============================================
    idx_last   <- match(paste0(2001:2021,"-12"), 
                        substr(vdate,1,7))
    vdate_last <- vdate[idx_last]
     
    # > vdate_last
    # [1]  "2001-12-31" "2002-12-31" "2003-12-31" "2004-12-31"
    # [5]  "2005-12-30" "2006-12-29" "2007-12-31" "2008-12-31"
    # [9]  "2009-12-31" "2010-12-31" "2011-12-30" "2012-12-31"
    # [13] "2013-12-31" "2014-12-31" "2015-12-31" "2016-12-30"
    # [17] "2017-12-29" "2018-12-31" "2019-12-31" "2020-12-31"
    # [21] "2021-12-31"

    Setting x-axis as the selected dates

    This is what I wanted to do in this post.

    When plotting a figure for a time series, the x-axis needs to be labeled as selected dates with sufficient spacings not a plain index number such as 1, 2, 3.

    We can use two arguments (at and labels) of axis() function as selected index and the corresponding dates (idx_last and vdate_last).

    #===============================================
    # This is what I wanted to do 
    # (x axis as selected date)
    #===============================================
     
    # sample date
    nt <- length(vdate)
    x = periodic.series(start.period = 50, length = nt)
    y = x + 0.2*rnorm(nt) # add some noise
     
     
    # plot 
    x11(width=7, height=8); par(mfrow = c(2,1))
     
    plot(y, type = "l", lty=1, lwd=3, xaxt="n", 
         main="year-month x-axis", xlab = "date", col = 4)
    axis(1, at = idx_last, labels = vdate_last)
     
    plot(y, type = "l", lty=1, lwd=3, xaxt="n", 
         main="year x-axis", xlab = "date", col = 2)
    axis(1, at = idx_last, labels = substr(vdate_last,1,4))

    Visit SHLee AI Financial Model blog for additional insights on this topic.

    Disclosure: Interactive Brokers

    Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

    This material is from SHLee AI Financial Model and is being posted with its permission. The views expressed in this material are solely those of the author and/or SHLee AI Financial Model and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

    Go Source

    Chart

    SignUp For Breaking Alerts

    New Graphic

    We respect your email privacy

    Share post:

    Popular

    More like this
    Related

    Volatility Slouches

    “Slouch” is very descriptive word.  As a noun it’s...

    PPI Reflects Uptick in Goods Charges: Nov. 14, 2024

    Investors are parsing through employment and inflation data while...

    Market Making for Beginners

    The article “Market Making for Beginners” first appeared on...

    Chart Advisor: One Chart to Rule’m All

    By Tom Bruni, CMT 1/ One Chart to Rule’m All 2/ AMD...