简体   繁体   中英

Trouble plotting with dates in R

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM