使用 ggplot 的 geom_boxplot 在 POSIX 日期绘制预先计算的统计数据
Plotting pre-computed statistics on POSIX dates with ggplot's geom_boxplot
我正在尝试创建一系列显示日期值分布的箱线图。我使用 data.table
计算分位数,然后将它们提供给 ggplot
进行绘制。然而,当我尝试绘制它们时,我收到一条错误消息 "Error: '/' 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
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
知道为什么我会收到此错误,或者如何修复它?我尝试将日期转换为数值,并且绘制正确,但是轴只显示数值而不是日期。
看起来 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()
我正在尝试创建一系列显示日期值分布的箱线图。我使用 data.table
计算分位数,然后将它们提供给 ggplot
进行绘制。然而,当我尝试绘制它们时,我收到一条错误消息 "Error: '/' 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
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
知道为什么我会收到此错误,或者如何修复它?我尝试将日期转换为数值,并且绘制正确,但是轴只显示数值而不是日期。
看起来 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()