[R] as.Date and ylim in empty plot: RESOLVED

David Winsemius dw|n@em|u@ @end|ng |rom comc@@t@net
Sat Sep 22 18:51:25 CEST 2018


> On Sep 19, 2018, at 10:48 PM, Ogbos Okike <giftedlife2014 using gmail.com> wrote:
> 
> Hi David,
> That's it!!! The outcome is attached.

The explanation for this is that columns that have digits separated by dashes will not be interpreted by R's read.table() as numeric or dates, but rather as the default for text entries:  as R factors. Factors are stored as numbers with an associated attribute that gets used to display the meaning of those numbers. Since the numbers used are integers from 1 to the count of unique items, using the xlim = as.numeric(as.Date(.)) creates values are far outside the range of the factor integers and nothing gets displayed as a result. If you had used range(as.numeric(<factor-variable-name>)) you might have seen something. Whether it was what you wanted to see .... well, that's another matter!

Good luck understanding R factors. Some research centers have adopted a policy of setting the option used by read.table for that behavior with:

options(stringsAsFactors= FALSE)  # can be set in .Rprofile or other "startup" file.

That produces text rather than factors by default and might result in less confusion.

-- 
David.
> 
> Many thanks please.
> 
> Best
> Ogbos
> 
> On Wed, Sep 19, 2018 at 11:34 PM David Winsemius <dwinsemius using comcast.net> wrote:
> 
> > On Sep 19, 2018, at 7:55 AM, Ogbos Okike <giftedlife2014 using gmail.com> wrote:
> > 
> > Dear Experts,
> > I generated the plot attached. Every other thing is OK except the black
> > horizontal lines which should appear like points or dots as the coloured
> > ones. I can't understand why.
> > 
> > I tried to change it to look like dots by calling empty plots so that I
> > will add them as points.
> > 
> > Since I have a range of date that can fall any where within 2005, I tried:
> > 
> > plot(1, type="n", xlab="", ylab="",
> > xlim=c(as.Date("2005-01-01"),as.Date("2005-12-31")), ylim=c(-.5, -10))
> > 
> > ylim worked fine but xlim instead of appearing like date as indicated on
> > the x-axes of the attached plot, translated to ordinary numbers (12800,
> > 12900,13000, 13100).
> > 
> > All the data is of the same format:
> > 2005-01-04 -2.76105935648091
> > 2005-01-19 -9.60813496025994
> > 2005-01-22 -7.92101965866777
> > 2005-02-19 -1.61308152604905
> > 2005-02-24 -1.51497015807712
> > 2005-05-09 -2.06465797304654
> > 2005-05-11 -1.14840389007051
> > 2005-05-16 -3.85281900888504
> > 2005-06-13 -1.18659683796617
> > 2005-06-17 -3.48787712566258
> > 2005-06-22 -1.14223758296308
> > 2005-07-18 -4.96013018907366
> > 2005-08-03 -1.24313324914368
> > 2005-08-07 -2.96672894841722
> > 2005-08-10 -1.11868063781156
> > 2005-08-25 -1.46453734930983
> > 2005-09-13 -8.00895215754776
> > 2005-09-15 -6.63439065989452
> > 2005-10-13 -2.25054996925846
> > 2005-12-15 -1.08933890547705
> 
> You did not succeed in creating a plot that the rhelp mail server would accept. My guess is that the first column is a factor variable and that you did not use colClasses when doing your data input.
> 
> dd <- read.table(text="2005-01-04 -2.76105935648091
> 2005-01-19 -9.60813496025994
> 2005-01-22 -7.92101965866777
> 2005-02-19 -1.61308152604905
> 2005-02-24 -1.51497015807712
> 2005-05-09 -2.06465797304654
> 2005-05-11 -1.14840389007051
> 2005-05-16 -3.85281900888504
> 2005-06-13 -1.18659683796617
> 2005-06-17 -3.48787712566258
> 2005-06-22 -1.14223758296308
> 2005-07-18 -4.96013018907366
> 2005-08-03 -1.24313324914368
> 2005-08-07 -2.96672894841722
> 2005-08-10 -1.11868063781156
> 2005-08-25 -1.46453734930983
> 2005-09-13 -8.00895215754776
> 2005-09-15 -6.63439065989452
> 2005-10-13 -2.25054996925846
> 2005-12-15 -1.08933890547705", colClasses=c("Date","numeric")
> )
> 
> 
> No problems with:
> 
>  plot(dd[[1]], dd[[2]], xlim=c(as.Date("2005-01-01"),as.Date("2005-12-31")))
> 
> 
> (Not a particularly good test of the use of an xlim argument since nothing was excluded.)
> 
> PDF's are accepted. PNGs are not.
> 
> -- 
> David.
> > 
> > Thank you so much for your input.
> > 
> > Best regards
> > Ogbos
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
> 
> David Winsemius
> Alameda, CA, USA
> 
> 'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law
> 
> 
> 
> 
> 
> <Ogbos.pdf>

David Winsemius
Alameda, CA, USA

'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law




More information about the R-help mailing list