Reputation: 1
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!
Upvotes: 0
Views: 174
Reputation: 174813
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):
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"))
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.
Upvotes: 1