0

I am using R to produce a line graph and have two vectors, one for the x value and another for the y value. More concrete it's accumulated performance over time. I can plot the line using my data (data$AccBH). However, when I try to put the data for y in the code like this:

plot(data$Date, data$AccBH, type='l')

It doesn't work. In the documentation I read this "plot(x, y, ...)" so I thought I could just need to put the vector for y and x in their respective positions. My data for x are 'nondate' dates for example 'Jan-00'.

Basically, all I want is to have some dates from my y data show up on the x axis in stead of the 50, 100, 150.. in the graph below. All I found so far was working with 'real dates' in the x axis.

I just realised I can't post images yet. So I should describe my graph first.

It would be great if somebody could help me out. Also, this is my first question I posted, so if you think I should change the way to post a question, please let me know. Thank you!

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • 2
    Please read [this](http://stackoverflow.com/q/5963269/324364) for some guidance about how to write a question that provides us with enough information to actually answer it. – joran May 13 '13 at 22:01
  • Hi welcome to SO. I'm sure you will find that there are plenty of folks here who would love to help you out. If you could kindly check out the post that @joran linked to and offer a reproducible example, it would help others help you more effecitvely – Ricardo Saporta May 13 '13 at 22:14
  • If you're on a Mac, the following function will quickly add sample data to your clipboard which you can just paste into here: https://raw.github.com/rsaporta/pubR/gitbranch/reproduce.R – Ricardo Saporta May 13 '13 at 22:15
  • Does `plot(data$Date, y = data$AccBH, type='l')` help? – Simon O'Hanlon May 13 '13 at 22:17
  • Nothing will help until the data for the x-axis, which aren't in any standard date representation, are coerced to the appropriate class. – Gavin Simpson May 13 '13 at 22:29

1 Answers1

1

You can't expect R and plot() to make up numeric values from arbitrary character strings that you as a human understand but might as well be gibberish to a computer!

You need to inform R that the vector Date in your data frame represents a date - in R parlance an object of class "Date". Now as you don't have days there, we have to make them up - the first of each month will suffice. Consider this simple example data set

df <- data.frame(Date = paste(month.abb, "13", sep = "-"),
                 AccBH = rnorm(12))

I inform R that Date is a "Date" object through as.Date(). Notice here I am pasting on 01- to each of the month-year combinations already in df. An appropriate format specifier is also provided to tell R how to "read" the elements of the date (read ?strftime for those details)

df <- transform(df,
                Date = as.Date(paste("01", as.character(df$Date), sep = "-"),
                               format = "%d-%b-%y"))

Once we have the data in the correct format, as shown here by the str() function

> str(df)
'data.frame':   12 obs. of  2 variables:
 $ Date : Date, format: "2013-01-01" "2013-02-01" ...
 $ AccBH: num  0.494 -0.759 -2.204 -2.004 2.808 ...

plot() works appropriately:

plot(AccBH ~ Date, data = df, type = "l")

producing something like (you'll have different data):

enter image description here

If you want different labelling, then you'll need to specify it yourself, e.g.

plot(AccBH ~ Date, data = df, type = "l", xaxt = "n")
with(df, axis.Date(side = 1, x = Date, format = "%b-%y"))

enter image description here

See ?axis.Date for more details.

Note there is the zoo package which has a data type specifically for this year-month, the "yearmon" class. You can use this without fiddling with your original data

df2 <- data.frame(Date = paste(month.abb, "13", sep = "-"),
                  AccBH = rnorm(12))

through the use of the as.yearmon() function

require("zoo")
df2 <- transform(df2, Date = as.yearmon(Date, format = "%b-%y"))

Then create a "zoo" object using the variables thus created

zobj <- with(df2, zoo(AccBH, order.by = Date))

This gives

> zobj
    Jan 2013     Feb 2013     Mar 2013     Apr 2013     May 2013     Jun 2013 
-0.607986011  1.741046878 -0.603960213 -1.319397405 -0.355051912  0.296862314 
    Jul 2013     Aug 2013     Sep 2013     Oct 2013     Nov 2013     Dec 2013 
 0.235978782 -0.308123299 -0.200230379 -0.004428621 -0.858600654 -0.241808594 

Which can then be plotted

plot(zobj)

but note it has it's own way of labelling the axes.

enter image description here

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453