R 使用ggplot'绘制POSIX日期的预计算统计数据;s geom_箱线图

R 使用ggplot'绘制POSIX日期的预计算统计数据;s geom_箱线图,r,ggplot2,data.table,lubridate,R,Ggplot2,Data.table,Lubridate,我试图创建一系列箱线图,显示日期值的分布。我使用data.table计算分位数,然后将它们输入要绘制的ggplot。然而,当我试图绘制它们时,我得到一个错误,上面写着“error:'/”不是为“POSIXt”对象定义的 下面是一个可重复的示例,使用的数据来自于lubridate: library(data.table) library(ggplot2) library(lubridate) # Load data from the lubridate library data(lakers)

我试图创建一系列箱线图,显示日期值的分布。我使用
data.table
计算分位数,然后将它们输入要绘制的
ggplot
。然而,当我试图绘制它们时,我得到一个错误,上面写着“error:'/”不是为“POSIXt”对象定义的

下面是一个可重复的示例,使用的数据来自于lubridate:

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

# Load data from the lubridate library
data(lakers)

# create POSIX date variable
lakers <- within(lakers, posix.date <- ymd(date))
lakers <- data.table(lakers, key = "player")

# Calculate quantiles of dates by player
# follows post at http://stackoverflow.com/questions/14758566/how-can-i-use-functions-returning-vectors-like-fivenum-with-ddply-or-aggregate
Tukeys.five <- c("Min","Q1","Med","Q3","Max") 
plot.stats <- lakers[
    ,
    {quant <- as.list(quantile(posix.date, prob = seq(0,1, by = 0.25),
                               names = F))
    setattr(quant, 'names', Tukeys.five)
    quant},
    by = player
    ]

# Now attempt to plot this with ggplot
ggplot(plot.stats, aes(x = player, ymin = Min, lower = Q1, middle = Med, 
                       upper = Q3, max = Max, group = player)) +
  geom_boxplot(stat = "identity") + coord_flip() 
# Error: '/' not defined for "POSIXt" objects
# In addition: Warning message:
# In loop_apply(n, do.ply) :
#   position_dodge requires constant width: output may be incorrect
库(data.table)
图书馆(GG2)
图书馆(lubridate)
#从lubridate库加载数据
数据(湖人队)
#创建POSIX日期变量

湖人看起来像是does division的代码试图计算方块宽度。据我所知,那根树枝似乎是不可避免的。hack-y的一个解决方法是实际定义日期时间值的划分

`/.POSIXt`<-function(e1,e2) as.numeric(e1)/as.numeric(e2)

`/.POSIXt`哇,谢谢!令我印象深刻的是,你发现了这一点并解决了这一问题。
`/.POSIXt`<-function(e1,e2) as.numeric(e1)/as.numeric(e2)
ggplot(plot.stats[1:10,], aes(x = player, ymin = Min, lower = Q1, middle = Med, 
                       upper = Q3, max = Max, group = player)) +
  geom_boxplot(stat = "identity") + coord_flip()