0

I have a data frame with two variables. One of them is date (Col1) and the other is numeric population data (sk). I can plot the data like this:

plot(D$sk)

my problem is that (obviously) the plot does not include the dates in the x axis. I have tried to add it by this code:

plot(D$sk, D$Col1, ylim=c(2013-01,2015-05))

But I receive a blank plot and the following error message:

Warning message:
In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion

Based on this answer to a similar question I tried using the xaxt=n argument like this:

with(D, plot(Col1, sk, xaxt="n"))

but it does not work either. I got the following error:

    Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf

Can you please help how I could go forward?

EDIT: Col1 is defined by this code:

Col1=seq(as.Date("2013/1/1"), by="1 month", length.out=41). 

The sk variable is some random number between 100.000 and 200.000. I'm sorry, but I'm new to R and am not sure how to reproce that. I have extracted it from 41 excel sheets and since transformed it a couple of times. But basically these are the two variables in the data frame. I hope this helps somewhat.

EDIT2: Sorry, just noticed there is another code which reduced the date variable:

D$Col1=format(D$Col1, format="%Y-%m")
Community
  • 1
  • 1
babesz
  • 43
  • 1
  • 9
  • can you give an example of your data? Or see first this post: http://stackoverflow.com/questions/4843969/plotting-time-series-with-date-labels-on-x-axis – J_F Sep 25 '16 at 21:58
  • Make sure the dates column is of mode date. If not try `as.Date(D$Col1, format='%Y-%m')` . Format might be different – Daniel Winkler Sep 25 '16 at 22:07
  • `2013-01` will evaluate as 2012! Needs to be in quotes, might need to be a date object with `as.Date` – Richard Telford Sep 25 '16 at 22:22
  • The quotes give this error: `plot(D$sk, D$Col1, ylim=c("2013-01","2015-05")) Error in D$sk : $ operator is invalid for atomic vectors` – babesz Sep 25 '16 at 22:28

1 Answers1

1

I'd recommend using the zoo package which makes it easy to plot the dates on the x-axis. Here's an example

library(zoo)

IBM <- read.csv(paste0("http://real-chart.finance.yahoo.com/table.csv",
                       "?s=IBM&a=07&b=24&c=2010&d=07&e=24&f=2015",
                       "&g=d&ignore=.csv"))
IBM$Date <- as.Date(IBM$Date)
head(IBM)

# Date   Open   High    Low  Close   Volume Adj.Close
# 1 2015-08-24 143.47 147.76 143.00 143.47 10189700  138.1615
# 2 2015-08-21 151.50 153.19 148.70 148.85  7362100  143.3424
# 3 2015-08-20 152.74 153.91 152.50 152.66  4011500  147.0114
# 4 2015-08-19 155.15 155.67 153.41 153.94  4206400  148.2441
# 5 2015-08-18 155.51 156.52 155.25 156.01  2018300  150.2375
# 6 2015-08-17 155.20 156.69 154.70 156.31  2249600  150.5264

OpenPriceTs <- zoo(IBM$Open, IBM$Date)
plot(OpenPriceTs, xaxt="n", xlab="Date", ylab="Open Price")
axis.Date(1, at=IBM$Date, format="%b-%y", tcl=0)

enter image description here

nathanesau
  • 1,681
  • 16
  • 27
  • Hello, sorry for the belated reply. I installed zoo and followed your suggestions with my data, everything goes fine, even plotting, but when I want to add the labels in the last line: `DTs=zoo(D$sk, D$Col1) plot(DTs, xaxt="n", xlab="Date", ylab="pocet poberatelov") axis.Date(1, at=D$Col1, format="%b-%y", tcl=0)` I get the following error: `Error in axis(side, at = z, labels = labels, ...) : plot.new has not been called yet` Any ideas what I'm doing wrong? – babesz Sep 28 '16 at 19:05
  • I got it! After entering the plot(...) command, I should not close the graph. Then the axis.Date (...) simply adds the lables to the x axis. – babesz Sep 28 '16 at 19:36