gpt4 book ai didi

r - 如何使用ggplot2的x轴日期注释?

转载 作者:行者123 更新时间:2023-12-02 02:32:42 27 4
gpt4 key购买 nike

我无法理解 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))

ggplot2 chart with geom_segment

但是,以下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

我建模了annotate("segment", ...)在 R Graphics Cookbook 中收到 7.4 后调用,它似乎可以很好地处理 x 轴上没有日期的简单图表。

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

最佳答案

看起来这是 ggplot2 中的一个错误 (ggplot2 google groups discussion) 。我已经提交了new ticket .

关于r - 如何使用ggplot2的x轴日期注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18796386/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com