如果你做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
:
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是一個很好的問題來重溫。
你使用'month','an'和'melt'包什麼? – BenBarnes
Hi @BenBarnes,道歉。 'an'是我自己的函數,用'as.numeric'來簽約,月份來自'data。表'和融化來自'reshape2' –
如果你的'library()'語句包含在代碼中,你的'x'首先定義而不是最後一個,那麼我們可以複製和粘貼你的代碼。 'sfact'也是你自己的一個功能嗎? – SlowLearner