diff() function returns an empty object - r
I'm trying to differenciate a time serie, which looks like that : time serie to differenciate. But sadly, diff(spread) returns me this. I also tried diff(spread,1)). I nearly copypasted some code of a working example, and I don't find any obvious mistakes. I installed the modules two hours ago, so I've got the last version of all packages used.
# chemin espace de travail
setwd("C:/Users/Simon/Desktop/Projet serie temp")
#### Q1 ####
require(zoo)
require(tseries)
require(fUnitRoots)
data <- read.csv("base_form.csv",sep=",") #import .csv
View(data) #visualisation
indice = data$Index
dates = data$Dates
spread <- zoo(indice, order.by=dates)
View(spread)
plot.window(ylim = c(-20,20))
plot(spread) #représentation graphique
dspread <- diff(spread) #différence première
plot(cbind(spread,dspread))
Here is the error I get :
> plot(dspread)
Error in plot.window(xlim = xlim, ylim = ylim, log = log, yaxs = pars$yaxs) :
valeurs finies requises pour 'ylim'
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf
Here is the output of dput(head(spread))
structure(c(83.87, 86.15, 94.07, 90.02, 92.22, 93.18), index = structure(1:6, .Label = c("1990-01",
"1990-02", "1990-03", "1990-04", "1990-05", "1990-06", "1990-07",
"1990-08", "1990-09", "1990-10", "1990-11", "1990-12", "1991-01",
"1991-02", "1991-03", "1991-04", "1991-05", "1991-06", "1991-07",
"1991-08", "1991-09", "1991-10", "1991-11", "1991-12", "1992-01",
"1992-02", "1992-03", "1992-04", "1992-05", "1992-06", "1992-07",
"1992-08", "1992-09", "1992-10", "1992-11", "1992-12", "1993-01",
"1993-02", "1993-03", "1993-04", "1993-05", "1993-06", "1993-07",
"1993-08", "1993-09", "1993-10", "1993-11", "1993-12", "1994-01",
"1994-02", "1994-03", "1994-04", "1994-05", "1994-06", "1994-07",
"1994-08", "1994-09", "1994-10", "1994-11", "1994-12", "1995-01",
"1995-02", "1995-03", "1995-04", "1995-05", "1995-06", "1995-07",
"1995-08", "1995-09", "1995-10", "1995-11", "1995-12", "1996-01",
"1996-02", "1996-03", "1996-04", "1996-05", "1996-06", "1996-07",
"1996-08", "1996-09", "1996-10", "1996-11", "1996-12", "1997-01",
"1997-02", "1997-03", "1997-04", "1997-05", "1997-06", "1997-07",
"1997-08", "1997-09", "1997-10", "1997-11", "1997-12", "1998-01",
"1998-02", "1998-03", "1998-04", "1998-05", "1998-06", "1998-07",
"1998-08", "1998-09", "1998-10", "1998-11", "1998-12", "1999-01",
"1999-02", "1999-03", "1999-04", "1999-05", "1999-06", "1999-07",
"1999-08", "1999-09", "1999-10", "1999-11", "1999-12", "2000-01",
"2000-02", "2000-03", "2000-04", "2000-05", "2000-06", "2000-07",
"2000-08", "2000-09", "2000-10", "2000-11", "2000-12", "2001-01",
"2001-02", "2001-03", "2001-04", "2001-05", "2001-06", "2001-07",
"2001-08", "2001-09", "2001-10", "2001-11", "2001-12", "2002-01",
"2002-02", "2002-03", "2002-04", "2002-05", "2002-06", "2002-07",
"2002-08", "2002-09", "2002-10", "2002-11", "2002-12", "2003-01",
"2003-02", "2003-03", "2003-04", "2003-05", "2003-06", "2003-07",
"2003-08", "2003-09", "2003-10", "2003-11", "2003-12", "2004-01",
"2004-02", "2004-03", "2004-04", "2004-05", "2004-06", "2004-07",
"2004-08", "2004-09", "2004-10", "2004-11", "2004-12", "2005-01",
"2005-02", "2005-03", "2005-04", "2005-05", "2005-06", "2005-07",
"2005-08", "2005-09", "2005-10", "2005-11", "2005-12", "2006-01",
"2006-02", "2006-03", "2006-04", "2006-05", "2006-06", "2006-07",
"2006-08", "2006-09", "2006-10", "2006-11", "2006-12", "2007-01",
"2007-02", "2007-03", "2007-04", "2007-05", "2007-06", "2007-07",
"2007-08", "2007-09", "2007-10", "2007-11", "2007-12", "2008-01",
"2008-02", "2008-03", "2008-04", "2008-05", "2008-06", "2008-07",
"2008-08", "2008-09", "2008-10", "2008-11", "2008-12", "2009-01",
"2009-02", "2009-03", "2009-04", "2009-05", "2009-06", "2009-07",
"2009-08", "2009-09", "2009-10", "2009-11", "2009-12", "2010-01",
"2010-02", "2010-03", "2010-04", "2010-05", "2010-06", "2010-07",
"2010-08", "2010-09", "2010-10", "2010-11", "2010-12", "2011-01",
"2011-02", "2011-03", "2011-04", "2011-05", "2011-06", "2011-07",
"2011-08", "2011-09", "2011-10", "2011-11", "2011-12", "2012-01",
"2012-02", "2012-03", "2012-04", "2012-05", "2012-06", "2012-07",
"2012-08", "2012-09", "2012-10", "2012-11", "2012-12", "2013-01",
"2013-02", "2013-03", "2013-04", "2013-05", "2013-06", "2013-07",
"2013-08", "2013-09", "2013-10", "2013-11", "2013-12", "2014-01",
"2014-02", "2014-03", "2014-04", "2014-05", "2014-06", "2014-07",
"2014-08", "2014-09", "2014-10", "2014-11", "2014-12", "2015-01",
"2015-02", "2015-03", "2015-04", "2015-05", "2015-06", "2015-07",
"2015-08", "2015-09", "2015-10", "2015-11", "2015-12", "2016-01",
"2016-02", "2016-03", "2016-04", "2016-05", "2016-06", "2016-07",
"2016-08", "2016-09", "2016-10", "2016-11", "2016-12", "2017-01",
"2017-02", "2017-03", "2017-04", "2017-05", "2017-06", "2017-07",
"2017-08", "2017-09", "2017-10", "2017-11", "2017-12", "2018-01",
"2018-02"), class = "factor"), class = "zoo")
I cannot reproduce the problem perfectly, but I have some thoughts.
TL;DR: Edit: don't use factors, use either character or Date objects before zoo-ifying things.
I hunted this down by looking at the source for zoo:::diff.zoo. Namely, it was failing at
x - lag(x, k=-1)
# Data:
# numeric(0)
# Index:
# factor(0)
# 338 Levels: 1990-01 1990-02 1990-03 1990-04 1990-05 1990-06 1990-07 1990-08 1990-09 1990-10 1990-11 1990-12 1991-01 1991-02 1991-03 1991-04 1991-05 1991-06 1991-07 1991-08 1991-09 1991-10 1991-11 1991-12 1992-01 1992-02 1992-03 1992-04 ... 2018-02
I believe that typically zoo objects are indexed based on some form of time-progression. This might be simple integers, as in
str(zoo(2:5))
# 'zoo' series from 1 to 4
# Data: int [1:4] 2 3 4 5
# Index: int [1:4] 1 2 3 4
or something more explicit/intentional, such as a Date or POSIXct timestamp. In your case, it's a factor. I don't know if zoo is trying to treat it like an integer (probably not, otherwise it should have come up with something), or like some categorical character, most likely not what you want in a time-series. (Correction: as 42- pointed out, this is actually quite fine.)
So even if zoo intelligently deals with factors, there is also the problem that the date you have listed is not perfectly unambiguous (is not a time-based object). For instance, by "1990-01" do you mean "1990-01-01"? Though it might seem intuitive and obvious to make that assumption, R typically does not follow you on that leap.
Try this:
(ind <- index(x))
# [1] 1990-01 1990-02 1990-03 1990-04 1990-05 1990-06
# 338 Levels: 1990-01 1990-02 1990-03 1990-04 1990-05 1990-06 1990-07 1990-08 1990-09 1990-10 1990-11 1990-12 ... 2018-02
(ind <- as.Date(paste0(ind, "-01"), format="%Y-%m-%d"))
# [1] "1990-01-01" "1990-02-01" "1990-03-01" "1990-04-01" "1990-05-01" "1990-06-01"
index(x) <- ind
(The surrounding parentheses are merely a shortcut to dump the output post-assignment. They can be safely removed for production.) That now allows
x - lag(x, k=-1)
# 1990-01-01 1990-02-01 1990-03-01 1990-04-01 1990-05-01 1990-06-01
# NA 2.28 7.92 -4.05 2.20 0.96
which means your spread is likely working now:
diff(x)
# 1990-02-01 1990-03-01 1990-04-01 1990-05-01 1990-06-01
# 2.28 7.92 -4.05 2.20 0.96
My guess means that your data import should instead look like:
data <- read.csv("base_form.csv",sep=",") #import .csv
indice = data$Index
dates = as.Date(paste0(data$Dates, "-01"), format="%Y-%m-%d")
spread <- zoo(indice, order.by=dates)
or more simply
data <- read.csv("base_form.csv",sep=",")
dates = as.character(data$Dates)
or even more simply
data <- read.csv("base_form.csv",sep=",", stringsAsFactors=FALSE)
The problem appears to be the dates are encoded as factors. Note the difference if we construct spread manually:
> indice <- c(83.87, 86.15, 94.07, 90.02, 92.22, 93.18)
> dates <- as.factor(c("1990-01", "1990-02", "1990-03", "1990-04", "1990-05", "1990-06"))
> spread <- zoo(indice, order.by = dates)
> diff(spread)
Data:
numeric(0)
Index:
factor(0)
Levels: 1990-01 1990-02 1990-03 1990-04 1990-05 1990-06
> dates <- c("1990-01", "1990-02", "1990-03", "1990-04", "1990-05", "1990-06")
> spread <- zoo(indice, order.by = dates)
> diff(spread)
1990-02 1990-03 1990-04 1990-05 1990-06
2.28 7.92 -4.05 2.20 0.96
To fix it, you can try adding stringsAsFactors = FALSE to your read.csv.
data <- read.csv("base_form.csv", stringsAsFactors = FALSE)
(Note that sep = "," is the default for read.csv, so you don't really need to specify it.)
EDIT: I should add there are many more zoo-like way of reading dates in correctly, see https://cran.r-project.org/web/packages/zoo/vignettes/zoo-read.pdf
I'm posting to correct what I think are some inaccuracies in r2evans analysis of the problem. It is true that the problem stems from using a factor as an index. The factor class in R does not support ordering operations and at least one of the "o"'s in the name "zoo" stands for "ordered". It could have been solved quickly by:
index(spread) <- as.character(index(spread))
Then the diff-operation would have succeeded, and the cbind operation would also have succeeded because there is a cbind.zoo function that recognizes differences in number of rows and automagically pads the shorter columns with NA's at the beginning.
> cbind( diff(spread), spread )
diff(spread) spread
1990-01 NA 83.87
1990-02 2.28 86.15
1990-03 7.92 94.07
1990-04 -4.05 90.02
1990-05 2.20 92.22
1990-06 0.96 93.18
> cbind( diff(diff(spread)), spread )
diff(diff(spread)) spread
1990-01 NA 83.87
1990-02 NA 86.15
1990-03 5.64 94.07
1990-04 -11.97 90.02
1990-05 6.25 92.22
1990-06 -1.24 93.18
Character vectors are perfectly acceptable index classes for zoo. They will be ordered as lexical values. It's perfectly acceptable to make a "<" or ">" operation on two character values, so there is no ambiguity in this case. The zoo-package also has a yearmon class that this index could become if desired.
Related
Object converted to xts but an error appears when I apply dailyReturn
I have already converted my data to an xts object and this error persists. I think it is related to my date format but, I have used the function as.Date to make sure my date format is right. Here is my code so far: library(quantmod) library(ggplot2) library(PerformanceAnalytics) library(GetTDData) library(openair) library(dplyr) library(devtools) ntnb <- download.TD.data('NTN-B') ntnb35 <- read.TD.files(dl.folder = 'TD Files', maturity = '150535') new35<-ntnb35%>%select(ref.date,yield.bid,price.bid,asset.code,matur.date)%>%filter(ref.date>=as.Date("2016-01-01")) new35$ref.date<-as.Date(new35$ref.date,format="%Y-%m-%d") new35_xts<-xts(new35,order.by=new35$ref.date) dailyReturn(new35_xts) Error in to_period(xx, period = on.opts[[period]], ...) : unsupported type A sample of my data which class is data.frame: ref.date yield.bid price.bid 1 2016-01-04 0.0737 2425.21 2 2016-01-05 0.0735 2431.68 3 2016-01-06 0.0727 2453.29 4 2016-01-07 0.0724 2462.39 5 2016-01-08 0.0732 2443.98 When I convert to xts, my index is strange, that is, my first column, which has an X before the date: ref.date yield.bid price.bid X2016.01.04 "2016-01-04" "0.0737" "2425.21" X2016.01.05 "2016-01-05" "0.0735" "2431.68" X2016.01.06 "2016-01-06" "0.0727" "2453.29" X2016.01.07 "2016-01-07" "0.0724" "2462.39" X2016.01.08 "2016-01-08" "0.0732" "2443.98" X2016.01.11 "2016-01-11" "0.0737" "2432.90" X2016.01.12 "2016-01-12" "0.0735" "2439.33" X2016.01.13 "2016-01-13" "0.0734" "2443.28" My current version of R is ‘3.6.3’. Thank you guys!
You need to remove the date from your timeseries when creating an xts object. If you don't the date will appear in the matrix and will turn the matrix into a character matrix. Using the data.frame new35 as a starting point: library(quantmod) # Make sure you don't have the data in the data for the xts matrix. # The index is defined in the order.by new35_xts <- xts(new35[, -1], order.by = new35$ref.date) dailyReturn(new35_xts) daily.returns 2016-01-04 0.000000000 2016-01-05 -0.002713704 2016-01-06 -0.010884354 2016-01-07 -0.004126547 2016-01-08 0.011049724 data: new35 <- structure(list(ref.date = structure(c(16804, 16805, 16806, 16807, 16808), class = "Date"), yield.bid = c(0.0737, 0.0735, 0.0727, 0.0724, 0.0732), price.bid = c(2425.21, 2431.68, 2453.29, 2462.39, 2443.98)), row.names = c("1", "2", "3", "4", "5"), class = "data.frame")
use a XTS inside name of a Variable in r
I have more than 300 stocks downloaded with getsymbols() and I have the name of this stocks in a vector, for example: USA_STOCKS = c("AAL","AAPL","ADBE","ADI","ADP","ADSK","ALGN", "ALXN","AMAT","AMGN","AMZN","ASML","ATVI","AVGO", "BIDU","BIIB") # This is just an extract from 300 getSymbols(AAL) # this is just one of the 300 "getsymbols" With that, I have a XTS object called AAL and a vector USA_TOCKS with all the name of the XTS Objets. I would like to do: AAL = na.omit(AAL) But, instead of use the Object AAL, I want to refer the object using the name inside the vector. Something like this: USA_STOCKS[1] = na.omit(USA_STOCKS[1]) Obviusly if i did this, I will change only the name of "AAL" inside the vector. But what I want is to refer the object AAL.
Hmm, still a bit unclear, but I think you want to do something like this: library(quantmod) USA_STOCKS = c("AAL","AAPL","ADBE") # Put all requested quotes in big list stocks_usa <- lapply(USA_STOCKS, getSymbols, from = "2018-10-01", to = "2018-11-01", auto.assign = F) # set the names of the list names(stocks_usa) <- USA_STOCKS #reference AAL head(stocks_usa$AAL) AAL.Open AAL.High AAL.Low AAL.Close AAL.Volume AAL.Adjusted 2018-10-01 41.41 41.75 39.60 39.61 7210700 39.50097 2018-10-02 39.60 39.60 38.40 38.50 7625000 38.39403 2018-10-03 38.70 39.26 38.42 38.80 6370300 38.69320 2018-10-04 38.80 39.01 37.48 37.92 5916500 37.81562 2018-10-05 37.93 38.13 36.21 36.44 9127000 36.33969 2018-10-08 36.44 36.85 35.60 35.90 7879300 35.80119 # more referencing stocks_usa$AAL <- na.omit(stocks_usa$AAL)
Trouble coercing data frame to look like an object from the package "DOSE"; my as.numeric() object looks different
I'm having some trouble trying to coerce a dataframe into data provided from the R package DOSE so I can run GO over-representation test from the package clusterProfiler The clusterProfiler vignette uses geneList from the DOSE package; it's class is numeric, but it's somehow linked a gene expression count with gene id. You can see the data by doing the following: source("https://bioconductor.org/biocLite.R") biocLite("DOSE") data(geneList, package="DOSE") class(geneList) [1] "numeric" dput(head(geneList)) structure(c(4.57261268231107, 4.51459371540294, 4.41821798112707, 4.14407518193211, 3.87625800905113, 3.67785700608222), .Names = c("4312", "8318", "10874", "55143", "55388", "991")) I have a data.frame with two columns: dput(df) structure(list(Gene_symbol = c(5339L, 1778L, 79026L, 5591L, 23224L, 23195L), HAP1_pc = c(170, 253, 221.5, 231, 163.5, 172)), .Names = c("Gene_symbol", "HAP1_pc"), class = "data.frame", row.names = c(NA, -6L)) When I try to coerce this data frame into a numeric class it gives me an error: df2 <- as.numeric(df) Error: (list) object cannot be coerced to type 'double' I searched Stack Overflow for this error and found a suggestion from this here: as.numeric(df[[1]]) [1] 5339 1778 79026 5591 23224 23195 Of course this only returns column one (gene id). By reason, if I replaced it with 2 it should return column 2 (gene expression) as.numeric(df[[2]]) [1] 170.0 253.0 221.5 231.0 163.5 172.0 Indeed it did. What confuses me is when I try specifying a range it returns the second row of column one: as.numeric(df[[1:2]]) [1] 1778 At the bottom of this post someone said the above solution only works for one column (which appears to be the case); however, their suggestion for multiple columns (below) does not work as it returns class matrix: apply(df, 2 , as.numeric) Gene_symbol HAP1_pc [1,] 5339 170.0 [2,] 1778 253.0 [3,] 79026 221.5 [4,] 5591 231.0 [5,] 23224 163.5 [6,] 23195 172.0 I tried converting this matrix to numeric but this doesn't work either. foo <- apply(df, 2 , as.numeric) bar <- as.numeric(foo) bar [1] 5339.0 1778.0 79026.0 5591.0 23224.0 23195.0 170.0 253.0 221.5 231.0 163.5 172.0 Comparing the dput of geneList and my data the first apparent difference is that mine is list so i try to unlist() before as.numeric() as.numeric(unlist(df)) [1] 5339.0 1778.0 79026.0 5591.0 23224.0 23195.0 170.0 253.0 221.5 231.0 163.5 172.0 Same result as converting the matrix to numeric above. Next apparent thing from the two dput()'s is that while my data has .Name as the headers the geneList .Name is one for every value in column 1. This is likely where the problem lies; however, I'm not sure what to do about it. How can one do this with base R or using `dplyr'? You're help would be greatly appreciated.
txt to XTS format
I have a text file with close data that I am trying to convert to XTS format. I am able to call it into R, but cannot figure out a way to convert this data to XTS format. Below is the sample data I am working with. 05/31/2017,32.78,FCOM 05/30/2017,32.72,FCOM 05/26/2017,32.56,FCOM 05/25/2017,32.57,FCOM 05/24/2017,32.47,FCOM 05/31/2017,35.63,FDIS 05/30/2017,35.71,FDIS 05/26/2017,35.67,FDIS 05/25/2017,35.54,FDIS 05/24/2017,35.23,FDIS 05/31/2017,18.17,FENY 05/30/2017,18.26,FENY 05/26/2017,18.53,FENY 05/25/2017,18.51,FENY 05/24/2017,18.90,FENY 05/31/2017,36.52,FHLC 05/30/2017,36.40,FHLC 05/26/2017,36.50,FHLC 05/25/2017,36.62,FHLC 05/24/2017,36.41,FHLC 05/31/2017,34.28,FIDU 05/30/2017,34.34,FIDU 05/26/2017,34.33,FIDU 05/25/2017,34.31,FIDU 05/24/2017,34.17,FIDU 05/31/2017,30.56,FMAT 05/30/2017,30.66,FMAT 05/26/2017,30.68,FMAT 05/25/2017,30.62,FMAT 05/24/2017,30.70,FMAT 05/31/2017,34.26,FNCL 05/30/2017,34.60,FNCL 05/26/2017,34.86,FNCL 05/25/2017,34.90,FNCL 05/24/2017,34.85,FNCL 05/31/2017,23.96,FREL 05/30/2017,23.96,FREL 05/26/2017,24.02,FREL 05/25/2017,24.21,FREL 05/24/2017,24.16,FREL Thank you in advance for any assistance you can provide me with!
Use the split argument to read.zoo to indicate which column contains the data that should be used to create columns. x <- read.zoo(text = "05/31/2017,32.78,FCOM 05/30/2017,32.72,FCOM 05/26/2017,32.56,FCOM 05/25/2017,32.57,FCOM 05/24/2017,32.47,FCOM 05/31/2017,35.63,FDIS 05/30/2017,35.71,FDIS 05/26/2017,35.67,FDIS 05/25/2017,35.54,FDIS 05/24/2018,35.23,FDIS 05/31/2017,18.17,FENY 05/30/2017,18.26,FENY 05/26/2017,18.53,FENY 05/25/2017,18.51,FENY 05/24/2017,18.90,FENY 05/31/2017,36.52,FHLC 05/30/2017,36.40,FHLC 05/26/2017,36.50,FHLC 05/25/2017,36.62,FHLC 05/24/2017,36.41,FHLC 05/31/2017,34.28,FIDU 05/30/2017,34.34,FIDU 05/26/2017,34.33,FIDU 05/25/2017,34.31,FIDU 05/24/2017,34.17,FIDU 05/31/2017,30.56,FMAT 05/30/2017,30.66,FMAT 05/26/2017,30.68,FMAT 05/25/2017,30.62,FMAT 05/24/2017,30.70,FMAT 05/31/2017,34.26,FNCL 05/30/2017,34.60,FNCL 05/26/2017,34.86,FNCL 05/25/2017,34.90,FNCL 05/24/2017,34.85,FNCL 05/31/2017,23.96,FREL 05/30/2017,23.96,FREL 05/26/2017,24.02,FREL 05/25/2017,24.21,FREL 05/24/2017,24.16,FREL", sep = ",", format = "%m/%d/%Y", split = 3) Setting split = 3 tells read.zoo to use the 3rd column in the file to create columns. Then x is a zoo object: R> x FCOM FDIS FENY FHLC FIDU FMAT FNCL FREL 2017-05-24 32.47 35.23 18.90 36.41 34.17 30.70 34.85 24.16 2017-05-25 32.57 35.54 18.51 36.62 34.31 30.62 34.90 24.21 2017-05-26 32.56 35.67 18.53 36.50 34.33 30.68 34.86 24.02 2017-05-30 32.72 35.71 18.26 36.40 34.34 30.66 34.60 23.96 2017-05-31 32.78 35.63 18.17 36.52 34.28 30.56 34.26 23.96 You can convert x to xts using x <- as.xts(x).
Display dates on axes in R
I have a data frame df contains 2 fields (Number and dates) as the following Number date 1496 Apr-08 3067 May-08 3049 Jun-08 3077 Jul-08 3237 Aug-08 3020 Sep-08 4990 Oct-08 4776 Nov-08 5140 Dec-08 5582 Jan-09 5743 Feb-09 5561 Mar-09 5974 Apr-09 I want to use plot() function in R to plot number vs. date I've tried using axis.Date() function but it didn't work. Nothing displayed on the plotting area and I don't know why. My code was: plot(df$Number) axis.Date(1, at=seq(min(df$date), max(df$date), by="months"), format="%m-%Y") Any help, please?
It seems that your biggest problem is creating an appropriate date structure for your data. It would be good to acquaint yourself to the different ways that R keeps the date structure. ?strptime has a rather good list of syntax commonly used. In your question then, to convert your date to a form which axis.Date can work, you need to add an arbitrary day into your date field and then convert it with as.Date: df$date <- as.Date(paste0("01-", df$date), format="%d-%b-%y") This way, your axis.Date plot would work: plot(df$date, df$Number, xaxt="n") axis.Date(1, at=seq(min(df$date), max(df$date), by="months"), format="%m-%Y") Input data: df <- structure(list(Number = c(1496, 3067, 3049, 3077, 3237, 3020, 4990, 4776, 5140, 5582, 5743, 5561, 5974), date = c("Apr-08", "May-08", "Jun-08", "Jul-08", "Aug-08", "Sep-08", "Oct-08", "Nov-08", "Dec-08", "Jan-09", "Feb-09", "Mar-09", "Apr-09")), .Names = c("Number", "date"), row.names = c(NA, -13L), class = "data.frame")
The likely source of your problem is that df$date is not a date, but e.g. a character. Using str(df) 'data.frame': 13 obs. of 2 variables: $ Number: int 1496 3067 3049 3077 3237 3020 4990 4776 5140 5582 ... $ date : chr "Apr-08" "May-08" "Jun-08" "Jul-08" ... # note that date is a character vector here, as opposed to Date. and plot(df$Number) axis(1, at=1:nrow(df), labels=df$date) I get