【问题标题】:Trouble plotting with dates in R在 R 中绘制日期时遇到问题
【发布时间】:2011-12-17 14:47:30
【问题描述】:

我对 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"
>

【问题讨论】:

    标签: r graph plot date posixct


    【解决方案1】:

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

    【讨论】:

    • 我想通了。在代码的早些时候,当我创建变量 mo.yr 时,我使用了 as.Date 而不是 as.POSIXct,所以我的轴是一种形式,而我的变量是另一种形式。之前大约有 150 行代码,所以我想我错过了。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2015-02-07
    • 2017-06-18
    • 2020-09-12
    • 1970-01-01
    • 2021-12-17
    • 2020-05-31
    • 2021-12-03
    • 1970-01-01
    相关资源
    最近更新 更多