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