set x-axis with some selected dates
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #----------------------------------- # 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 | cs |
Unlike match(), which( %in% ) returns the multiple encounters of a match. Here 90 appears twice.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #----------------------------------- # 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 | cs |
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).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | #----------------------------------- # 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" | cs |
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).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #=============================================== # 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)) | cs |
Finally, we can draw two kinds of graph with x-axis being denoted by selected dates.
No comments:
Post a Comment