当涉及到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))
但是,以下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轴没有日期的简单图形一起正常工作。
如果有人能解释这里真正发生的事情,我将不胜感激。