[英]plot.ts with years at x-axis
我四處張望,但仍然找不到我的小問題。 我有一個像這樣的數據框:
GerProd_sum_PerYear
1997 369332000
1998 399127000
1999 396103500
2000 506698500
2001 417757000
2002 440025882
2003 499654816
2004 533781000
2005 565508000
2006 600001000
2007 695574663
2008 543780271
2009 496257990
2010 547352965
2011 554533553
2012 532066522
2013 535117263
我想plot.ts(或只是plot),以便年份(1997年至2013年)在x軸上。
到目前為止,我已經做到了:
test<-plot.ts(df, type= "b", main = "Amounts over time", xlab = "Years", las=3, ylab = "Amounts per year")
看起來不錯,但是x軸是1到17,因為有17個值。...我希望將1997到2013替換為1到17。
救命 :)
問題是年份被定義為row.names,因此在plot.ts
沒有考慮。 我會以這種方式去尋找基本情節:
df <-
read.csv(text=
"GerProd_sum_PerYear
1997,369332000
1998,399127000
1999,396103500
2000,506698500
2001,417757000
2002,440025882
2003,499654816
2004,533781000
2005,565508000
2006,600001000
2007,695574663
2008,543780271
2009,496257990
2010,547352965
2011,554533553
2012,532066522
2013,53511726",row.names=1)
years <- as.numeric(row.names(df))
test<-plot(x=years,y=df$GerProd_sum_PerYear, type= "b",
main = "Amounts over time", xlab = "Years", las=3,
ylab = "Amounts per year",xaxt='n')
#xaxt='n' means do not draw the ticks, we will do it manually in the next line
axis(side=1,at=years,las=2) #las=2 means perpendicular labels
如果您真的想使用ts
:
data <- "Date GerProd_sum_PerYear
1997 369332000
1998 399127000
1999 396103500
2000 506698500
2001 417757000
2002 440025882
2003 499654816
2004 533781000
2005 565508000
2006 600001000
2007 695574663
2008 543780271
2009 496257990
2010 547352965
2011 554533553
2012 532066522
2013 535117263"
df <- read.table(text=data, header=T, sep=" ",as.is=T)
timeseries <- ts(df$GerProd_sum_PerYear, start = 1997)
plot.ts(timeseries, type= "b", main = "Amounts over time", xlab = "Years", las=3, ylab = "Amounts per year")
(但考慮使用xts
或zoo
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.