[R] Plotrix and twoord.plot with date on x-axe

Arnaud Michel michel.arnaud at cirad.fr
Sat Jul 26 12:37:01 CEST 2014


Perfect Jim, It's fine !
Thank you
Michel

Le 26/07/2014 12:16, Jim Lemon a écrit :
> On Sat, 26 Jul 2014 09:36:49 AM Arnaud Michel wrote:
>> Hello
>>
>> With package plotrix and  twoord.plot function, I would like to put the
>> labels of the ticks values of x-axe which are date (
>> c("2006 Jan", "2007 Jan", "2008 Jan", "2009 Jan", "2010 Jan", "2011
>> Jan", "2012 Jan") with if possible, the angle of labels and x-axe = 40
>> Any idea ?
>>
>> Thank you for your help
>>
>> My dataframe is
>> TT <-
>> structure(list(x1 = structure(c(1L, 8L, 15L, 22L, 29L, 36L, 43L,
>> 50L, 57L, 64L, 71L, 77L, 2L, 9L, 16L, 23L, 30L, 37L, 44L, 51L,
>> 58L, 65L, 3L, 10L, 17L, 24L, 31L, 38L, 45L, 52L, 59L, 66L, 72L,
>> 78L, 4L, 11L, 18L, 25L, 32L, 39L, 46L, 53L, 60L, 67L, 73L, 79L,
>> 5L, 12L, 19L, 26L, 33L, 40L, 47L, 54L, 61L, 68L, 74L, 80L, 6L,
>> 13L, 20L, 27L, 34L, 41L, 48L, 55L, 62L, 69L, 75L, 81L, 7L, 14L,
>> 21L, 28L, 35L, 42L, 49L, 56L, 63L, 70L, 76L, 82L), .Label =
> c("01/01/2006",
>> "01/01/2007", "01/01/2008", "01/01/2009", "01/01/2010",
> "01/01/2011",
>> "01/01/2012", "01/02/2006", "01/02/2007", "01/02/2008",
> "01/02/2009",
>> "01/02/2010", "01/02/2011", "01/02/2012", "01/03/2006",
> "01/03/2007",
>> "01/03/2008", "01/03/2009", "01/03/2010", "01/03/2011",
> "01/03/2012",
>> "01/04/2006", "01/04/2007", "01/04/2008", "01/04/2009",
> "01/04/2010",
>> "01/04/2011", "01/04/2012", "01/05/2006", "01/05/2007",
> "01/05/2008",
>> "01/05/2009", "01/05/2010", "01/05/2011", "01/05/2012",
> "01/06/2006",
>> "01/06/2007", "01/06/2008", "01/06/2009", "01/06/2010",
> "01/06/2011",
>> "01/06/2012", "01/07/2006", "01/07/2007", "01/07/2008",
> "01/07/2009",
>> "01/07/2010", "01/07/2011", "01/07/2012", "01/08/2006",
> "01/08/2007",
>> "01/08/2008", "01/08/2009", "01/08/2010", "01/08/2011",
> "01/08/2012",
>> "01/09/2006", "01/09/2007", "01/09/2008", "01/09/2009",
> "01/09/2010",
>> "01/09/2011", "01/09/2012", "01/10/2006", "01/10/2007",
> "01/10/2008",
>> "01/10/2009", "01/10/2010", "01/10/2011", "01/10/2012",
> "01/11/2006",
>> "01/11/2008", "01/11/2009", "01/11/2010", "01/11/2011",
> "01/11/2012",
>> "01/12/2006", "01/12/2008", "01/12/2009", "01/12/2010",
> "01/12/2011",
>> "01/12/2012"), class = "factor"), y1 = c(2.592356082, 2.345800476,
>> 0.583585821, 5.475129414, 5.475129414, 3.718656646,
> 3.089374967,
>> 2.301938832, 2.937799758, 2.194943003, 2.54038668, NA,
> 1.644678741,
>> 1.449029225, 0.956848412, 0.859023655, 0.987578146,
> 0.843738536,
>> 1.247265662, 1.284694265, 0.980520409, 0.835670652,
> 0.566612694,
>> 0.401453666, 0.439134806, 1.15846577, 0.687179049,
> 0.494573266,
>> 0.96702963, 0.504182954, 1.03521455, 0.41886541, 0.401638417,
>> 0.143972524, 0.84561927, 0.551221244, 0.916415951,
> 1.455635055,
>> 2.866544524, 1.709780054, 1.846827755, 1.451262182,
> 0.807575275,
>> 1.590211883, 1.542348196, 0.937123964, 1.399639685,
> 1.091996771,
>> 1.404915044, 1.816230935, 1.468899879, 1.34414673,
> 1.480941894,
>> 1.325698257, 1.462174427, 1.294317244, 1.348639226,
> 0.743242205,
>> 1.326535615, 1.288495608, 0.825648931, 0.520199099,
> 0.631660841,
>> 0.4838471, 0.514975576, 0.644832626, 0.789323515,
> 0.380945025,
>> 0.386553999, 0.342014493, 0.351664055, 0.188006956,
> 0.238740258,
>> 0.223667802, 0.063293682, 0.254889318, 0.324071093,
> 0.323446397,
>> 0.291533728, 0.152346111, 0.429059919, 0.237783277), y2 =
> c(3.59979021,
>> 3.760067114, 3.668671329, 4.08590604, 3.772684564,
> 3.833825503,
>> 3.770872483, 3.801879195, 3.776442953, 3.807248322,
> 3.623087248,
>> 3.85, 3.723986014, 3.67041958, 3.594405594, 3.595244755,
> 3.457832168,
>> 3.582684564, 3.580839161, 3.555804196, 3.409060403,
> 3.39885906,
>> 2.707342657, 2.483986014, 2.548881119, 2.797202797,
> 2.66034965,
>> 2.792447552, 2.597482517, 2.243426573, 2.300629371,
> 2.323356643,
>> 2.057832168, 2.142167832, 2.829300699, 2.45013986,
> 2.418531469,
>> 2.77027972, 3.384335664, 3.32048951, 3.297762238,
> 3.008531469,
>> 2.645034965, 2.965104895, 2.826853147, 2.747342657,
> 3.053986014,
>> 3.327132867, 2.845874126, 2.961748252, 3.104335664,
> 3.076153846,
>> 3.259090909, 3.08013986, 3.022727273, 3.016433566,
> 3.261468531,
>> 2.942237762, 3.217972028, 2.904685315, 2.674755245,
> 2.436783217,
>> 2.466853147, 2.557272727, 2.696853147, 3.308111888,
> 3.121468531,
>> 3.144195804, 3.017132867, 3.069160839, 2.985384615,
> 2.661258741,
>> 2.681188811, 2.746433566, 2.797202797, 2.666713287,
> 2.474335664,
>> 2.769230769, 2.623356643, 2.764195804, 2.658391608,
> 2.665594406
>> ), date = structure(1:82, .Label = c("2006-01-01", "2006-02-01",
>> "2006-03-01", "2006-04-01", "2006-05-01", "2006-06-01",
> "2006-07-01",
>> "2006-08-01", "2006-09-01", "2006-10-01", "2006-11-01",
> "2006-12-01",
>> "2007-01-01", "2007-02-01", "2007-03-01", "2007-04-01",
> "2007-05-01",
>> "2007-06-01", "2007-07-01", "2007-08-01", "2007-09-01",
> "2007-10-01",
>> "2008-01-01", "2008-02-01", "2008-03-01", "2008-04-01",
> "2008-05-01",
>> "2008-06-01", "2008-07-01", "2008-08-01", "2008-09-01",
> "2008-10-01",
>> "2008-11-01", "2008-12-01", "2009-01-01", "2009-02-01",
> "2009-03-01",
>> "2009-04-01", "2009-05-01", "2009-06-01", "2009-07-01",
> "2009-08-01",
>> "2009-09-01", "2009-10-01", "2009-11-01", "2009-12-01",
> "2010-01-01",
>> "2010-02-01", "2010-03-01", "2010-04-01", "2010-05-01",
> "2010-06-01",
>> "2010-07-01", "2010-08-01", "2010-09-01", "2010-10-01",
> "2010-11-01",
>> "2010-12-01", "2011-01-01", "2011-02-01", "2011-03-01",
> "2011-04-01",
>> "2011-05-01", "2011-06-01", "2011-07-01", "2011-08-01",
> "2011-09-01",
>> "2011-10-01", "2011-11-01", "2011-12-01", "2012-01-01",
> "2012-02-01",
>> "2012-03-01", "2012-04-01", "2012-05-01", "2012-06-01",
> "2012-07-01",
>> "2012-08-01", "2012-09-01", "2012-10-01", "2012-11-01",
> "2012-12-01"
>> ), class = "factor"), datepos = structure(c(2L, 1L, 1L, 1L, 1L,
>> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>> 1L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 1L, 1L,
>> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 6L, 1L, 1L, 1L, 1L, 1L, 1L,
>> 1L, 1L, 1L, 1L, 1L, 7L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>> 1L, 8L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("",
>> "2006 Jan", "2007 Jan", "2008 Jan", "2009 Jan", "2010 Jan", "2011 Jan",
>> "2012 Jan"), class = "factor")), .Names = c("x1", "y1", "y2",
>> "date", "datepos"), row.names = c(1L, 5L, 6L, 7L, 8L, 9L, 10L,
>> 11L, 12L, 2L, 3L, 4L, 13L, 15L, 16L, 17L, 18L, 19L, 20L, 21L,
>> 22L, 14L, 23L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 24L, 25L,
>> 26L, 35L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 36L, 37L, 38L,
>> 47L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 48L, 49L, 50L, 59L,
>> 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 60L, 61L, 62L, 71L, 75L,
>> 76L, 77L, 78L, 79L, 80L, 81L, 82L, 72L, 73L, 74L), class =
> "data.frame")
>> # My code is :
>> MinMaxr <- c(min(TT$y1, na.rm=TRUE), max(TT$y1, na.rm=TRUE))
>> MinMaxl <- c(min(TT$y2, na.rm=TRUE), max(TT$y2, na.rm=TRUE))
>>
>> library(plotrix)
>> twoord.plot(
>> TT$date, TT$y1, TT$date, TT$y2,
>> lylim = MinMaxr, rylim = MinMaxl,
>> ylab="YLlab", rylab="YRlab", lcol=2,rcol=4,
>> do.first="plot_bg();grid(col=\"white\",lty=1)",
>> lpch=18, rpch=20, axislab.cex=0.8, cex.main=2,
>> xtickpos=TT$date, xticklab=TT$datepos)
> Hi Arnaud,
> Given your data, the easiest way is:
>
> # change TT$date to a date object (it's a factor)
> TT$date<-as.Date(TT$date,"%Y-%m-%d")
> # use only the ticks that you want to appear
> # but don't display the labels yet
> twoord.plot(TT$date, TT$y1, TT$date, TT$y2,
>   lylim = MinMaxr, rylim = MinMaxl,
>   ylab="YLlab", rylab="YRlab", lcol=2,rcol=4,
>   do.first="plot_bg();grid(col=\"white\",lty=1)",
>   lpch=18, rpch=20, axislab.cex=0.8, cex.main=2,
>   xtickpos=TT$date[c(1,13,23,35,47,59,71)],
>   xticklab=rep("",7))
> # display the rotated labels
> staxlab(1,at=TT$date[c(1,13,23,35,47,59,71)],
>   labels=TT$datepos[c(1,13,23,35,47,59,71)],srt=40)
>
> Jim
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>

-- 
Michel ARNAUD



More information about the R-help mailing list