本文介绍了如何在x轴中使用ggplot2的注释和日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我在理解 geom_segment 和批注(segment,...)之间的区别时遇到问题在 x 轴上绘制日期。 让我们从一些随机数据开始: library(data.table) library(lubridate) library(ggplot2) #准备一些随机数据 set.seed(1234) dt dt.m #散点图p 以下两种方法都可以工作,并为以上定义的小区 p 添加细分: p + geom_segment(x = as.numeric(ymd(20130401)),xend = as.numeric(ymd(20130701)),y = -10,yend = 10)p + geom_segme nt(aes(x = ymd(20130401),xend = ymd(20130701),y = -10,yend = 10)) 然而,下面的 annotate(segment,...)调用都不起作用 - 并且它们会产生不同的错误消息,这是我无法真正解析的。 > $ p $注释(segment,x = as.numeric(ymd(20130401)),xend = as.numeric(ymd(20130701)),y = -10,yend = 10)错误:输入无效:time_trans只能用于POSIXct类的对象 > p +注释(segment,x = ymd(20130401),xend = ymd(20130701),y = -10,yend = 10) Ops.POSIXt((x - from [1 ]),diff(from)):'/'没有为POSIXt对象定义 > p + annotate(segment,aes(x = ymd(20130401),xend = ymd(20130701),y = -10,yend = 10)) as.data.frame.default中的错误(x [[i]],optional = TRUE,stringsAsFactors = stringsAsFactors):不能强制classuneval到data.frame 我在R Graphics Cookbook收到7.4后调用了批注(segment,...)调用,并且它对于在x轴上没有日期的简单图形似乎工作正常。 如果有人能够解释这里发生了什么,我将不胜感激。解决方案看起来像是ggplot2中的一个错误(ggplot2谷歌小组讨论)。我已经提交了新票。 I have a problem understanding the difference between geom_segment and annotate(segment, ...) when it comes to plots with dates on the x axis.Let's start with some random data:library(data.table)library(lubridate)library(ggplot2)# Prepare some random dataset.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 chartp <- qplot(month, total, data = dt.m)Both of the following work and add a segment to the plot p defined above:# Both of these work as expected and produce the same resultp + 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))However, neither of the following annotate("segment", ...) calls work - and they produce different error messages, which I cannot really parse.> 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.frameI modelled the annotate("segment", ...) calls after receipt 7.4 in R Graphics Cookbook, and it seems to work fine with simple graphs that have no dates in x-axis.I would appreciate if someone can explain what's really going on here. 解决方案 Looks like it's a bug in ggplot2 (ggplot2 google groups discussion). I have submitted a new ticket. 这篇关于如何在x轴中使用ggplot2的注释和日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 05-30 20:19