我从美国联邦网站获得了一些利率数据,我试图绘制收益率曲线。我打算用它来与其他一些产品进行比较,并且随着时间的推移保持一致,我希望尽可能长时间地在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))
答案 0 :(得分:4)
如果你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_continuous
更改为scale_x_discrete
并删除您放置x$variable
的位置:< / p>
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))
这给出了以下输出:
稍微离题一下,我是否可以指出这似乎是一个非常简单的问题,但由于你很难回答,所以它没有被回答。为什么难以回答?
library(ggplot)
和其他来电。所有这一切都意味着人们正在将代码的副本复制并粘贴到他们的R安装中,并且在他们有机会看到您遇到的问题之前,它已经以多种方式失败。这里的人 >对修复代码问题感兴趣,但他们并不想解决上述问题。
另一方面,如果你让人们很容易得到帮助,他们会帮助你。因此,在发布之前仔细查看您的代码,复制它,打开一个新的R会话,粘贴它,看看它是否能够解决您遇到的错误。如果确实如此,那就好了,继续发一个问题。如果没有,请清理代码并重试。这是另一位初学者的建议,他在SO上发布了一些不太好的问题。你做了试图整理一个可重复性最小的例子这一事实表明你很想做到正确,但可能需要在发布之前多做一些思考。 This是一个重新回顾的好问题。