0

I've got some interest rate data from the US fed website and I'm trying to plot a yield curve. I intend to use this for comparison with a number of others and to stay consistent over time, I'd like to keep the same axis range on the y-axis for as long as possible and for as many countries as possible. The following piece of code scale_y_continuous(limits=c(0,7)) or ylim(0,7) both give errors. Does anyone have any thoughts on what I might be doing wrong? Thanks

library(reshape2)
library(data.tool)
library(ggplot2)

    x =    structure(list(Series.Description = c("2012-07-27", "2012-10-26"
        ), `1.month` = c("0.08", "0.12"), `6.month` = c("0.15", "0.15"
        ), `1.year` = c("0.17", "0.19"), `2.year` = c("0.23", "0.30"), 
            `5.year` = c("0.59", "0.78"), `10.year` = c("1.47", "1.81"
            ), `30.year` = c("2.51", "2.94")), .Names = c("Series.Description", 
        "1.month", "6.month", "1.year", "2.year", "5.year", "10.year", 
        "30.year"), row.names = c(1L, 4L), class = "data.frame")


    # dates as # of days
    z=c(30,182,365,730,1825,3650,10950)
    names(x)[1]="date"
    names(x)[-1]=c(30,182,365,730,1825,3650,10950)
    x=melt(x,id.vars=c(1))
    x$variable=levels(x$variable)[x$variable]
       x$variable=as.numeric(x$variable)

    ggplot(data=x,aes(x=variable,y=value,group=date,linetype=date)) + 
      geom_line(colour="red") + geom_point(colour="red") + 
      scale_x_continuous(breaks=z,labels=c("1M","6M","1Y","2Y","3Y","5Y","10Y")) +
      scale_linetype_manual(values=c(2,1)) + 
      scale_y_continuous(limits=c(0,7))
SlowLearner
  • 7,907
  • 11
  • 49
  • 80
Tahnoon Pasha
  • 5,848
  • 14
  • 49
  • 75
  • What package(s) are you using for `month`, `an` and `melt`? – BenBarnes Dec 03 '12 at 16:28
  • Hi @BenBarnes, apologies. `an` is my own function to contract `as.numeric`, month is from `data.table` and melt is from `reshape2` – Tahnoon Pasha Dec 03 '12 at 16:32
  • Would be good to have your `library()` statements included with the code and your `x` defined first instead of last, then we can just copy and paste your code. Is `sfact` one of your own functions as well? – SlowLearner Dec 03 '12 at 16:33
  • hi @SlowLearner embarassingly yes. I've edited the code and included the library statement too. – Tahnoon Pasha Dec 03 '12 at 16:37
  • Sorry Tahnoon, my comment was not clear enough. What I meant was that it helps if you can include the `library(ggplot2)`, `library(reshape)` etc statements at the top of your code. Then all the necessary libraries will be loaded and we don't have to look at the code to figure out which ones are needed. It's pretty obvious for `ggplot` but as you can see from BenBarnes' comment, not so obvious for something like `month`. I assume `lubridate` but... – SlowLearner Dec 03 '12 at 16:43
  • made the edit @SlowLearner. That makes more sense .. :-) also just realised its completely unnecessary for the code – Tahnoon Pasha Dec 03 '12 at 17:18
  • and suffering a brief rtfm moment.. I think `extend_limits` described on [hadleys site](http://docs.ggplot2.org/current/expand_limits.html) should do the trick... – Tahnoon Pasha Dec 03 '12 at 20:01
  • Your issue is that value is a character variable, which will be converted to a factor when you plot it. It looks like it should be numeric and continuous (perhaps you can clarify). If it is correct as a factor / character variable, then you can't use `scale_..._continuous` (as the error clearly states). – mnel Dec 04 '12 at 00:25

1 Answers1

5

If you do str(x) you will see what is going on.

> str(x)
'data.frame':   14 obs. of  3 variables:
 $ date    : chr  "2012-07-27" "2012-10-26" "2012-07-27" "2012-10-26" ...
 $ variable: num  30 30 182 182 365 ...
 $ value   : chr  "0.08" "0.12" "0.15" "0.15" ...

value is a character, not a number as mnel states in his comment. So if you change the value column to a numeric data type it should at least plot. Whether it gives the output you wish is another issue. The code below seems to work for me.

library(reshape2)
library(ggplot2)

x =    structure(list(Series.Description = c("2012-07-27", "2012-10-26"
    ), `1.month` = c("0.08", "0.12"), `6.month` = c("0.15", "0.15"
    ), `1.year` = c("0.17", "0.19"), `2.year` = c("0.23", "0.30"),
        `5.year` = c("0.59", "0.78"), `10.year` = c("1.47", "1.81"
        ), `30.year` = c("2.51", "2.94")), .Names = c("Series.Description",
    "1.month", "6.month", "1.year", "2.year", "5.year", "10.year",
    "30.year"), row.names = c(1L, 4L), class = "data.frame")


# dates as # of days
z=c(30,182,365,730,1825,3650,10950)
names(x)[1]="date"
names(x)[-1]=c(30,182,365,730,1825,3650,10950)
x=melt(x,id.vars=c(1))
x$variable=levels(x$variable)[x$variable]
   x$variable=as.numeric(x$variable)
x$value <- as.numeric(x$value)

ggplot(data=x,aes(x=variable,y=value,group=date,linetype=date)) +
  geom_line(colour="red") + geom_point(colour="red") +
  scale_x_continuous(breaks=z,labels=c("1M","6M","1Y","2Y","3Y","5Y","10Y")) +
  scale_linetype_manual(values=c(2,1)) +
  scale_y_continuous(limits=c(0,7))

If you actually just want labels at regular intervals along the x-axis, you should probably try changing the scale_x_continuous to scale_x_discrete and removing the bit where you fiddle with x$variable:

x <- structure(list(Series.Description = c("2012-07-27", "2012-10-26"
    ), `1.month` = c("0.08", "0.12"), `6.month` = c("0.15", "0.15"
    ), `1.year` = c("0.17", "0.19"), `2.year` = c("0.23", "0.30"),
        `5.year` = c("0.59", "0.78"), `10.year` = c("1.47", "1.81"
        ), `30.year` = c("2.51", "2.94")), .Names = c("Series.Description",
    "1.month", "6.month", "1.year", "2.year", "5.year", "10.year",
    "30.year"), row.names = c(1L, 4L), class = "data.frame")


# dates as # of days
z <- c(30,182,365,730,1825,3650,10950)
names(x)[1] <- "date"
names(x)[-1] <- c(30,182,365,730,1825,3650,10950)
x <- melt(x, id.vars = c(1))
#x$variable=levels(x$variable)[x$variable]
#   x$variable=as.numeric(x$variable)
x$value <- as.numeric(x$value)

ggplot(data = x, aes(x = variable, y = value, group = date, linetype = date)) +
  geom_line(colour = "red") + geom_point(colour = "red") +
  scale_x_discrete(breaks = z, labels = c("1M","6M","1Y","2Y","3Y","5Y","10Y")) +
  scale_linetype_manual(values = c(2,1)) +
  scale_y_continuous(limits = c(0,7))

This gives the following output:

plot

To digress slightly, may I point out that this seems to have been a very simple problem but it hasn't been answered because you made it hard to answer. Why was it hard to answer?

  1. You didn't include the library(ggplot) and other calls at the top.
  2. You included functions that weren't defined in the initial code.
  3. You defined structures after the code instead of before.
  4. You didn't report the error message itself, which would have been useful.

All of this meant that people were copying and pasting into a copy of your code into their R installations and it was failing in multiple ways before they got a chance to see the problem you were experiencing. People here are interested in fixing problems with your code, but they don't want to mess around with issues like the ones outlined above.

On the other hand, if you make it easy for people to help, they will help. So look at your code carefully before posting, copy it, open a new R session, paste it in and see if it works up to the error you have experienced. If it does, great, go ahead and post a question. If not, clean up your code and try again. This is advice from another beginner who has posted a number of not-very-good questions on SO. The fact that you did try to put together a minimal reproducible example suggests that you are keen to get it right, but probably need to do a bit more thinking before posting. This is a great question to revisit.

Community
  • 1
  • 1
SlowLearner
  • 7,907
  • 11
  • 49
  • 80