问题:您对使用ggplot2
处理持续时间的建议(作者:Hadley Wickham)。具体来说:复制以下带有自定义间隔和合适标签的图。优先使用自定义功能和/或数据重构的方式最少。欢迎提供我未引用的软件包的建议。
数据以秒为单位存储(请参阅下面的df
)。我想显示人眼可读的中断和标签,例如天而不是数千秒,而休息时间是在0、1、2 ...天,而不是尴尬的分数。
工作量证明:下面的第一个示例将持续时间作为整数处理,并通过逐个案例适当地除以60/24/365的倍数等来实现目标。第二个示例使用基数R
difftime
个对象。为了在这种情况下正确处理,我必须使用strptime
函数并减去1
。我错过了什么吗?第三个示例使用duration
包中的lubridate
类。虽然使用day()
和seconds_to_period()
函数很容易指定标签,但是在设置自定义间隔时我做得不好。第四个示例使用hms
类。我设法指定了休息时间,但没有指定标签。也欢迎就以下每个示例提供有关如何编写较短代码行的任何建议。
# Data
df = data.frame(x = 1:6,
num = c(374400, 343500, 174000, 193500, 197700, 270300))
# base/difftime
df$difftime <- as.difftime(df$num, units = "secs")
# lubridate/duration
library("lubridate") # devtools::install_github("tidyverse/lubridate") # the dev version fixes a bug
df$duration <- duration(df$num, units = "seconds")
# hms/hms
library("hms")
df$hms <- as.hms(df$num)
library("ggplot2")
library("scales")
# 1: data is base/numeric
# Pro: no package dependence
# Con: Hard work
breaks = seq(0, 100*60*60, 20*60*60)
labels = function(x) round(x/60/60/24, 0)
ggplot(data = df, aes(x = x, y = num)) +
geom_bar(stat = "identity", fill = "lightblue") +
scale_y_continuous(name = "Duration (Days)",
breaks = breaks,
labels = labels) +
labs(title = "Data stored as numeric (seconds)",
subtitle = "breaks = seq(0, 100*60*60, 20*60*60)\nlabels = function(x) round(x/60/60/24, 0)",
x = NULL)
ggsave("base-num.png")
# 2: data is base/difftime
# Pro: simple once you get over the ``strftime(x, "%d")`` syntax.
# Unresolved: Why do I need to subtract a day?
labels = function(x) as.integer(strftime(x, "%d"))-1
ggplot(data = df, aes(x = x, y = difftime)) +
geom_bar(stat = "identity", fill = "lightblue") +
scale_y_time(name = "Duration (Days)",
labels = labels) +
labs(title = "Data stored as difftime (seconds)",
subtitle = "default breaks\nlabels = function(x) as.integer(strftime(x, '%d'))-1",
x = NULL)
ggsave("base-difftime.png")
# 3: data is lubridate/duration
# Pro: intuitive combination of day() and seconds_to_period() functions
# Unresolved: a better way to make own breaks?
breaks = as.duration(seq(0, 5, 1)*60*60*24)
labels = function(x) day(seconds_to_period(x))
ggplot(data = df, aes(x = x, y = duration)) +
geom_bar(stat = "identity", fill = "lightblue") +
scale_y_continuous(name = "Duration (Days)",
breaks = breaks,
labels = labels) +
labs(title = "Data stored as duration (seconds)",
subtitle = "breaks = as.duration(seq(0, 5, 1)*60*60*24)\nlabels = function(x)lubridate::day(lubridate::seconds_to_period(x))",
x = NULL)
ggsave("lubridate-duration.png")
# 4: data is hms/hms
# Pro: Immediately generates plot with acceptable labels
# Unresolved: how to make own labels: Failed attempts:
labels = 0:(length(breaks)-1)
labels = function(x)lubridate::day(x)
breaks = seq(0, 5, 1)*60*60*24
ggplot(data = df, aes(x = x, y = hms)) +
geom_bar(stat = "identity", fill = "lightblue") +
scale_y_continuous(name = "Duration (Seconds)",
breaks = breaks) +
labs(title = "Data stored as hms (seconds)",
subtitle = "breaks = seq(0, 5, 1)*60*60*24\ndefault labels",
x = NULL)
ggsave("hms-hms.png")
编辑按照Axeman在注释部分的建议,这是将ggplot
与hms
对象结合的方法。在我看来,这似乎是4中最方便的,尽管必须减去1
是出乎意料的。 Axeman,您要发布此答案吗?
breaks = hms::hms(days = 0:4)
labels = function(x) lubridate::day(x)-1
答案 0 :(得分:2)
恕我直言,提出的解决方案在我看来过于复杂。
如果持续时间以整数秒为单位,并且需要以天为单位绘制,则我的方法是在对aes()
的调用中对其进行缩放:
df = data.frame(x = 1:6,
num = c(374400, 343500, 174000, 193500, 197700, 270300))
library("ggplot2")
ggplot(data = df, aes(x = x, y = num / (24*60*60))) +
geom_col(fill = "lightblue") +
labs(title = "Data stored as numeric (seconds)",
y = "Duration (Days)",
x = NULL)
因此,无需摆弄中断和标签。
NB:geom_col()
代替了geom_bar(stat = "identity")
。