0

Hello I have a data set with about 250 rows I want to be able to limit the dates because they are overlapping and its not clean. I just want the 4 digit years.

Date        Google.Close    Amazon.Close    Google.Return   Amazon.Return
2017-08-22  924.69                966.90             NA               NA
2017-08-23  927.00                958.00    0.002498132     -0.0092046993
2017-08-24  921.28                952.45    -0.006170411    -0.0057933069
2017-08-25  915.89                945.26    -0.005850571    -0.0075489547
2017-08-28  913.81                946.02    -0.002271034     0.0008040222
2017-08-29  921.29                954.06    0.008185487      0.0084987398


date<-gas$Date
amzcl<-gas$Amazon.Close
googcl<-gas$Google.Close
rtn_date <-gas$Date[2:253]
amzR<-gas$Amazon.Return[2:253]
googR<-gas$Google.Return[2:253]





plot(date, amzcl, ylim=c(900, 2000), type="l", pch=16,
 xlab="Amazon Stock Price by Year",
 ylab="Amazon Closing Price")
 axis(1,date,format(date,format="%y"))

Thanks

bryan
  • 307
  • 5
  • 19
  • Is `date` a saved as a `POSIXct` object? – NM_ Mar 17 '19 at 03:32
  • bryan, without providing sample *unambiguous* data (e.g., output from `dput`), you are unlikely to get much help. Looking at a frame on the console does very little for demonstrating what the actual contents are, where timestamps may look like timestamps but are actually strings or factors. Recommendation: https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans Mar 17 '19 at 03:50
  • Yes it is @Nishan – bryan Mar 17 '19 at 03:51
  • '> head(date) [1] "2017-08-22 UTC" "2017-08-23 UTC" "2017-08-24 UTC" "2017-08-25 UTC" "2017-08-28 UTC" "2017-08-29 UTC" ' – bryan Mar 17 '19 at 03:52
  • Yes I just realized that :/ I just updated my post @r2evans – bryan Mar 17 '19 at 04:12
  • bryan, that is not *unambiguous*. For example, tell me if `2019-03-16 21:15:10` is of class `POSIXt`, `character`, or `factor`. Hint: there is no way to know from this alone, but the answer to your question depends heavily on us knowing that. Read my comment above, and read the links, they are not hollow suggestions. (And then there's the realization that you *explicitly* set how many labels there are and where they go, when you say `axis(1, date, ...)`. Consider the arguments and read the man page: https://www.rdocumentation.org/packages/graphics/versions/3.5.3/topics/axis.) – r2evans Mar 17 '19 at 04:24

1 Answers1

0

Use naxt to remove to remove the labels on date and then use axis to add the labels in the format that you like.

Since you would like the last 4 digits of the year, use format="%Y"

plot(date, amzcl, ylim=c(900, 1000), type="l", pch=16,
  xlab="Amazon Stock Price by Year",
  ylab="Amazon Closing Price", 
  xaxt='n')

axis(1,date,format(date,format="%Y"))

You should get a plot such as the one below with the data in your example: 4digitdateforexampledata

Community
  • 1
  • 1
NM_
  • 1,887
  • 3
  • 12
  • 27
  • I think if you manually expand this sample data from 6 rows to 250, you may see that the axis will still be rather cluttered. – r2evans Mar 17 '19 at 13:36