0

我从美联储网站获得了一些利率数据,我正在尝试绘制收益率曲线。我打算将其用于与其他一些比较,并随着时间的推移保持一致,我希望尽可能长时间地在尽可能多的国家/地区保持 y 轴上的相同轴范围。以下代码scale_y_continuous(limits=c(0,7))ylim(0,7)两者都给出错误。有人对我可能做错了什么有任何想法吗?谢谢

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))
4

1 回答 1

5

如果你这样做str(x),你会看到发生了什么。

> 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是一个字符,而不是 mnel 在他的评论中所说的数字。因此,如果您将value列更改为数字数据类型,它至少应该绘图。它是否提供您想要的输出是另一个问题。下面的代码似乎对我有用。

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))

如果您实际上只想沿 x 轴定期添加标签,您可能应该尝试更改scale_x_continuoustoscale_x_discrete并删除您摆弄的位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))

这给出了以下输出:

阴谋

稍微离题,我可以指出这似乎是一个非常简单的问题,但由于您很难回答,因此尚未得到解答。为什么很难回答?

  1. 您没有library(ggplot)在顶部包括 the 和 other 调用。
  2. 您包含了未在初始代码中定义的函数。
  3. 您在代码之后而不是之前定义了结构。
  4. 您没有报告错误消息本身,这很有用。

所有这一切都意味着人们将您的代码副本复制并粘贴到他们的 R 安装中,并且在他们有机会看到您遇到的问题之前,它以多种方式失败。这里的人们兴趣解决您的代码问题,但他们不想解决上述问题。

另一方面,如果你让人们更容易提供帮助,他们就会提供帮助。因此,在发布之前仔细查看您的代码,复制它,打开一个新的 R 会话,将其粘贴进去,看看它是否能解决您遇到的错误。如果是这样,那太好了,请继续并发布问题。如果没有,请清理您的代码并重试。这是另一个初学者的建议,他在 SO 上发布了一些不太好的问题。您确实尝试将一个最小的可重现示例放在一起这一事实表明您渴望将其做好,但在发布之前可能需要做更多的思考。是一个值得重新审视的好问题。

于 2012-12-04T00:53:22.937 回答