3

我很难理解轴上日期的绘图之间的区别geom_segment和时间。annotate(segment, ...)x

让我们从一些随机数据开始:

library(data.table)
library(lubridate)
library(ggplot2)

# Prepare some random data
set.seed(1234)
dt <- data.table(x = rnorm(365*5), d = seq(ymd(20130101), ymd(20131231), by = 86400))
dt.m <- dt[, list(total = sum(x)), by = list(month = floor_date(d, "month"))]
# Create a basic scatterplot chart
p <- qplot(month, total, data = dt.m)

以下两项工作都在上面定义的图中添加了一个段p

# Both of these work as expected and produce the same result
p + geom_segment(x = as.numeric(ymd(20130401)), xend = as.numeric(ymd(20130701)), 
  y = -10, yend = 10)
p + geom_segment(aes(x = ymd(20130401), xend = ymd(20130701), 
  y = -10, yend = 10))

带有 geom_segment 的 ggplot2 图表

但是,以下annotate("segment", ...)调用都不起作用 - 它们会产生不同的错误消息,我无法真正解析。

> p + annotate("segment", x = as.numeric(ymd(20130401)), xend = as.numeric(ymd(20130701)), 
  y = -10, yend = 10)
Error: Invalid input: time_trans works with objects of class POSIXct only

> p + annotate("segment", x = ymd(20130401), xend = ymd(20130701), 
  y = -10, yend = 10)
Error in Ops.POSIXt((x - from[1]), diff(from)) : 
  '/' not defined for "POSIXt" objects 

> p + annotate("segment", aes(x = ymd(20130401), xend = ymd(20130701), 
  y = -10, yend = 10))
Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : 
  cannot coerce class ""uneval"" to a data.frame

我在 R Graphics Cookbook 中收到 7.4 后对调用进行了建模annotate("segment", ...),它似乎适用于 x 轴上没有日期的简单图形。

如果有人能解释这里到底发生了什么,我将不胜感激。

4

1 回答 1

1

看起来这是 ggplot2 (ggplot2 google groups Discussion)中的错误。我已经提交了一张新票

于 2013-09-16T20:11:40.303 回答