问:您对使用处理ggplot2
的建议(作者: Hadley )。具体而言:复制下面的情节与自定义中断和适当的标签。偏好尽量减少使用自定义函数和/或重构数据。欢迎对我没有列举的一揽子方案提出建议。
数据以秒为单位存储(参见下面的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
是出乎意料的。艾克斯曼,你想发这个作为答案吗?
breaks = hms::hms(days = 0:4)
labels = function(x) lubridate::day(x)-1
发布于 2018-09-28 01:33:23
在我看来,拟议的解决方案过于复杂。
如果以整数秒的形式给出持续时间,并且需要在日尺度上绘制,则我的方法是在对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)
所以,没有必要对断裂和标签胡作非为。
注:geom_col()
是geom_bar(stat = "identity")
的替代品。
https://stackoverflow.com/questions/52550809
复制