使用ggplot的geom_boxplot绘制POSIX日期的预先计算的统计数据

时间:2015-06-16 17:35:39

标签: r ggplot2 data.table lubridate

我正在尝试创建一系列显示日期值分布的箱线图。我使用data.table计算分位数,然后将它们提供给ggplot进行绘制。但是,当我尝试绘制它们时,我收到一条错误,上面写着“错误:'/'未定义为”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

任何想法为什么我会收到此错误,或如何解决它?我尝试将日期转换为数值,并且绘制正确,但是轴只显示数值而不是日期。

1 个答案:

答案 0 :(得分:3)

看起来geom_boxplot的代码会尝试计算框宽度。据我所知,这个分支似乎是不可避免的。一个hack-y解决方法是实际定义日期时间值的划分。

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

在代码之前运行此代码似乎会生成请求的绘图。使用

进行测试
`/.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() 

enter image description here