【发布时间】:2015-06-16 17:35:39
【问题描述】:
我正在尝试创建一系列显示日期值分布的箱线图。我使用data.table 计算分位数,然后将它们提供给ggplot 进行绘制。但是,当我尝试绘制它们时,我收到一条错误消息,提示“错误:'/' not defined for "POSIXt" objects”。
这是一个使用来自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
任何想法为什么我会收到这个错误,或者如何解决它?我尝试将日期转换为数值,并且绘制正确,但是轴只显示数值而不是日期。
【问题讨论】:
标签: r ggplot2 data.table lubridate