plot.ts x 轴为年份

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(或者只是绘图),以便年份(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")

(但考虑使用 xtszoo