我从美国联邦储备银行网站上获得了一些利率数据,我正在尝试绘制收益率曲线。我打算用它来与其他一些人进行比较并随着时间的推移保持一致,我希望尽可能长时间地在尽可能多的国家/地区保持 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))
Run Code Online (Sandbox Code Playgroud)
如果你这样做,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" ...
Run Code Online (Sandbox Code Playgroud)
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))
Run Code Online (Sandbox Code Playgroud)
如果您实际上只是想要沿 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))
Run Code Online (Sandbox Code Playgroud)
这给出了以下输出:

稍微跑题一下,我想指出这似乎是一个非常简单的问题,但没有得到回答,因为你很难回答。为什么很难回答?
library(ggplot) and other calls at the top.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.