[英]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.