Barplot: changing x axe and adding line

你离开我真会死。 提交于 2019-12-01 22:53:01

问题


I have a zoo with daily data that looks like this: > head(almorol)
1973-10-02 1973-10-03 1973-10-04 1973-10-05 1973-10-06 1973-10-07
183.9 208.2 153.7 84.8 52.5 35.5

I want to plot annual totals and a moving average so I did:

y<-apply.yearly(almorol, FUN=sum)
plot(y, main="Annual totals - Tagus (Almorol)",ylab="Q (m3/s)")
lines(rollapply(y, 10, mean, na.rm=TRUE), col="red", lwd=2)

Which works fine, but because the data is not continuous a line graph is not correct. If I do it with points is hard to follow so I wanted to do a barplot. However when I do barplot(y) the x axe appears in the format yy-mm-dd instead of just years and I can't add a line with the moving average (it doesn't give an error it just doesn't appear).

Thanks for helping!

edit: > y
1973-12-31 1974-12-31 1975-12-31 1976-12-31
19224.20 103766.30 72180.90 55939.80
1977-12-31 1978-12-31 1979-12-31 1980-12-31
215905.00 231014.21 319481.02 58979.84
1981-12-31 1982-12-31 1983-12-31 1984-12-31
32931.17 67989.06 83920.62 99431.75
1985-12-31 1986-12-31 1987-12-31 1988-12-31
161357.10 82910.87 101154.81 147541.80
1989-12-31 1990-12-31 1991-12-31 1992-12-31
137684.21 134974.39 89039.02 20774.72
1993-12-31 1994-12-31 1995-12-31 1996-12-31
58916.95 55187.38 52361.77 271064.34
1997-12-31 1998-12-31 1999-12-31 2000-12-31
220510.88 125116.62 42170.95 103915.99
2001-12-31 2002-12-31 2003-12-31 2004-12-31
244513.59 66811.15 167458.93 67223.66
2005-12-31 2006-12-31 2007-12-31 2008-12-31
8906.31 116874.33 79105.30 40142.61
2009-12-31 2010-12-31 2011-05-31
43835.34 161491.77 73093.06


回答1:


The x coordinates in a barplot are not related to the original scale of the data. You can look at the return value of the barplot function to see what the range of the x axis is (the probable reason for not seeing the line is it plotted completely off the screen).

The updateusr function in the TeachingDemos package can be used to change the coordinate scale to match the data that you want to add.

Another alternative is to use the plot function with type='h' (and look at lwd and lend options in ?par) to create your own barplot like plot using the coordinates of interest, then add your reference line.



来源:https://stackoverflow.com/questions/6343776/barplot-changing-x-axe-and-adding-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!