在 R 中绘制日期时出现问题
我对 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"
>
I am relatively new to R and am having trouble plotting grouped data against date. I have count data grouped by month over 4 years. I don't want May of 2008 grouped with May 2009 but rather points for each month of each year with standard errors. Here is my code so far but I get a blank graph with no points. I can get rid of the axis.POSIXct line and I get a graph with points and error bars. The problem seems to be around the scaling or data format of the plot vs. the axis. Can anyone help me here?
> 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"
>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你有两个选择。安装包“zoo”并使用yearmon 类,或计算数字月份,以便2005 年5 月为2005.4167。您可以使用
paste(month.abb[month],year)
创建更漂亮的标签。You have two choices. Install package "zoo" and use the yearmon class, or calculate numeric months so that May 2005 is 2005.4167. You can create prettier labels with
paste(month.abb[month], year)
.