9059 2011-02-18 2.81
9060 2011-02-21 <NA>
9061 2011-02-22 2.72
9062 2011-02-23 2.75
9063 2011-02-24 2.73
9064 2011-02-25 2.7
9065 2011-02-28 2.73
9066 2011-03-01 2.75
9067 2011-03-02 2.77
9068 2011-03-03 2.79
9069 2011-03-04 2.81
9070 2011-03-07 2.81
9071 2011-03-08 开发者_如何学编程 2.83
9072 2011-03-09 2.78
9073 2011-03-10 2.72
9074 2011-03-11 2.76
9075 2011-03-14 2.75
9076 2011-03-15 2.7
The above is a snippet of a data frame named 'mydata'. The name of the first column is V1 and the second V2. The first column contains dates which was converted using the as.Date() function.
I want to plot this on a x-y grid with column-1 on the x-axis and column-2 on the y-axis. I've tried various combinations for hours without success.
I tried
> plot(mydata[,1], mydata[,2])
This causes the R console to hang as it appears that it is rendering something nonsensical.
This works for me (although I don't know if it produces the desired output?
usr <- textConnection("9059 2011-02-18 2.81
9060 2011-02-21 <NA>
9061 2011-02-22 2.72
9062 2011-02-23 2.75
9063 2011-02-24 2.73
9064 2011-02-25 2.7
9065 2011-02-28 2.73
9066 2011-03-01 2.75
9067 2011-03-02 2.77
9068 2011-03-03 2.79
9069 2011-03-04 2.81
9070 2011-03-07 2.81
9071 2011-03-08 2.83
9072 2011-03-09 2.78
9073 2011-03-10 2.72
9074 2011-03-11 2.76
9075 2011-03-14 2.75
9076 2011-03-15 2.7")
usr <- read.table(usr, header = FALSE)[-1] #remove the first column
with(usr, plot(V3 ~ V2))
I got it to work doing the following:
> x2 <- read.zoo("2_yr_tsy_rate.txt", format="%m/%d/%Y", sep=",", na.strings="ND")
>
> x10 <- read.zoo("10_yr_tsy_rate.txt", format="%m/%d/%Y", sep=",", na.strings="ND")
>
> sprd <- x10 - x2
>
> plot(sprd)
this worked... but if I load the data using read.csv and try to change the data object it is a nightmare.
Now I have another problem. The plot works but the x-axis label reported in years every 10 years. My data is the interest rate spread going back to 1976. I would like the x-axis label to be quarterly. I tried various things including the following but got errors.
> plot(as.yearqtr(sprd), sprd, type='l')
Error in if (del == 0 && to == 0) return(to) :
missing value where TRUE/FALSE needed
This is the output of str(my object)
> str(sprd)
‘zoo’ series from 1976-06-01 to 2011-03-31
Data: num [1:9088] 0.68 0.71 0.7 0.77 0.79 0.79 0.82 0.86 0.83 0.83 ...
Index: Class 'Date' num [1:9088] 2343 2344 2345 2346 2349 ...
>
精彩评论