简体   繁体   English

在x轴上有年的plot.ts

[英]plot.ts with years at x-axis

I looked everywhere but still could not figure out my little problem. 我四处张望,但仍然找不到我的小问题。 I have a data frame like this: 我有一个像这样的数据框:

     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

I would like to plot.ts (or just plot), so that the years (1997 to 2013) are at the x-axis. 我想plot.ts(或只是plot),以便年份(1997年至2013年)在x轴上。

So far I have done this: 到目前为止,我已经做到了:

test<-plot.ts(df, type= "b", main = "Amounts over time", xlab = "Years", las=3, ylab = "Amounts per year")

It looks fine BUT the x achsis is 1 to 17, as there is 17 values.... I would like the 1 to 17 replaced by 1997 to 2013. 看起来不错,但是x轴是1到17,因为有17个值。...我希望将1997到2013替换为1到17。

Help :) 救命 :)

The problem is the years are defined as row.names, and so not considered in the plot.ts . 问题是年份被定义为row.names,因此在plot.ts没有考虑。 I would go for the base plot in this way : 我会以这种方式去寻找基本情节:

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

在此处输入图片说明

If you really want to use ts : 如果您真的想使用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")

在此处输入图片说明

(but consider using xts or zoo ) (但考虑使用xtszoo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM