条形图:更改 x ax 并添加线 [英] Barplot: changing x axe and adding line

查看:41
本文介绍了条形图:更改 x ax 并添加线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个每日数据如下所示的动物园:<代码>>头(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 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="年度总数 - Tagus (Almorol)",ylab="Q (m3/s)")
行(rollapply(y, 10, mean, na.rm=TRUE), col="red", lwd=2)

哪个工作正常,但由于数据不连续,折线图不正确.如果我用点来做就很难理解,所以我想做一个条形图.但是,当我执行 barplot(y) 时,x ax 以 yy-mm-dd 格式出现,而不仅仅是年份,而且我无法添加带有移动平均线的线(它没有给出错误它只是没有出现).

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).

感谢您的帮助!

<代码>>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

推荐答案

条形图中的 x 坐标与数据的原始比例无关.您可以查看 barplot 函数的返回值以了解 x 轴的范围是多少(看不到该线的可能原因是它完全脱离屏幕绘制).

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).

TeachingDemos 包中的 updateusr 函数可用于更改坐标比例以匹配您要添加的数据.

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

另一种选择是将 plot 函数与 type='h' 一起使用(并查看 中的 lwdlend 选项?par) 使用感兴趣的坐标创建自己的条形图,然后添加参考线.

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.

这篇关于条形图:更改 x ax 并添加线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆