麻烦用R中的日期绘图

时间:2011-10-28 21:23:25

标签: r graph plot date posixct

我对R来说相对较新,而且无法根据日期绘制分组数据。我有按月分组的数据超过4年。我不希望2008年5月与2009年5月分组,而是指出每年每个月的标准错误。这是我的代码到目前为止,但我得到一个没有点的空白图。我可以摆脱axis.POSIXct线,我得到一个带有点和误差条的图形。问题似乎与绘图与轴的缩放或数据格式有关。谁能在这帮助我?

> r <- as.POSIXct(range(refmCount$mo.yr), "month")
> 
> ############# can get plot and points to line up on the x-axis##########################
> plot(refmCount$mo.yr, refmCount$count, type = "n", xaxt = "n",
+      xlab = "Date",
+      ylab = "Mean number of salamanders per night",
+      xlim = c(r[1], r[2]))
> axis.POSIXct(1, at = seq(r[1], r[2], by = "month"), format = "%b")
> points(refmCount$mo.yr, refmCount$count, type = "p", pch = 19)
points(depmCount$mo.yr, depmCount$count, type = "p", pch = 24)
> arrows(refmCount$mo.yr, refmCount$count+mCount$se, refmCount$mo.yr, refmCount$count-  refmCount$se, angle=90, code=3, length=0)
 > 
> str(refmCount)
'data.frame':   19 obs. of  7 variables:
$ mo.yr:Class 'Date'  num [1:19] 14000 14031 14061 14092 14123 ...
$ trt  : Factor w/ 2 levels "Depletion","Reference": 2 2 2 2 2 2 2 2 2 2 ...
$ N    : num  75 110 15 10 34 20 20 10 40 15 ...
$ count: num  3.6 5.95 3.47 6.7 11.12 ...
$ sd   : num  8.58 8.4 4.42 3.47 11.88 ...
$ se   : num  0.99 0.801 1.142 1.096 2.037 ...
$ ci   : num  1.97 1.59 2.45 2.48 4.14 ...
> r
[1] "2008-04-30 20:00:00 EDT" "2011-05-31 20:00:00 EDT"
>

1 个答案:

答案 0 :(得分:1)

你有两个选择。安装包“zoo”并使用yearmon类,或计算数字月份,以便2005年5月为2005.4167。您可以使用paste(month.abb[month], year)创建更漂亮的标签。