R ggplot2:y axi中以%H%M%S格式打印秒数
我有一列(以秒为单位)需要绘制为“%H%M%S” 我尝试过使用lubridate pkg,但该列的结果是:R ggplot2:y axi中以%H%M%S格式打印秒数,r,ggplot2,R,Ggplot2,我有一列(以秒为单位)需要绘制为“%H%M%S” 我尝试过使用lubridate pkg,但该列的结果是: loadtime_dfs$avgPageLoadTime <- seconds_to_period(loadtime_df$avgPageLoadTime) Formal class 'Period' [package "lubridate"] 但是,如果Y轴带有断点:“00:01:00”、“00:02:00”、“00:03:00”、“00:04:00”、“00:05:00”
loadtime_dfs$avgPageLoadTime <- seconds_to_period(loadtime_df$avgPageLoadTime)
Formal class 'Period' [package "lubridate"]
但是,如果Y轴带有断点:“00:01:00”、“00:02:00”、“00:03:00”、“00:04:00”、“00:05:00”。您必须为
ggplot
提供字符串以指定为标签
如果您阅读?连续缩放(标签=…)
,您将看到标签=
采用豁免()
、字符或函数。如果要指定特定的位置和表示,则需要同时指定中断=
和标签=
。但是,通常需要ggplot2
来确定轴标签的放置位置,因此我们将提供一个函数,该函数接受一个值并返回一个字符串
我猜在某个地方有一个助手函数可以实现这一点,但这里有一个base-R版本。(函数的起源并不重要,因为我们可以用可能具有相同结果的函数替换我们的函数。)
此格式化函数通过将avgPageLoadTime
的秒数临时转换为POSIXct
然后转换为字符串来进行一些欺骗。这样做意味着如果设置了选项(“digits.secs”)
fmt_hms <- function(x, digits.secs=NULL) {
if (!is.null(digits.secs)) {
oopts <- options(digits.secs = digits.secs)
on.exit(options(oopts), add=TRUE)
}
format(as.POSIXct(x, origin="1970-01-01 00:00:00"), format="%H:%M:%OS", tz="UTC")
}
您必须为ggplot
提供字符串以指定为标签
如果您阅读?连续缩放(标签=…)
,您将看到标签=
采用豁免()
、字符或函数。如果要指定特定的位置和表示,则需要同时指定中断=
和标签=
。但是,通常需要ggplot2
来确定轴标签的放置位置,因此我们将提供一个函数,该函数接受一个值并返回一个字符串
我猜在某个地方有一个助手函数可以实现这一点,但这里有一个base-R版本。(函数的起源并不重要,因为我们可以用可能具有相同结果的函数替换我们的函数。)
此格式化函数通过将avgPageLoadTime
的秒数临时转换为POSIXct
然后转换为字符串来进行一些欺骗。这样做意味着如果设置了选项(“digits.secs”)
fmt_hms <- function(x, digits.secs=NULL) {
if (!is.null(digits.secs)) {
oopts <- options(digits.secs = digits.secs)
on.exit(options(oopts), add=TRUE)
}
format(as.POSIXct(x, origin="1970-01-01 00:00:00"), format="%H:%M:%OS", tz="UTC")
}
我认为您需要将以秒为单位的日期值转换为%H%m%s格式,然后尝试打印。我认为您需要以下方法之一-
library(ggplot2)
library(lubridate)
# convert seconds to periods
td <- seconds_to_period(loadtime_df$avgPageLoadTime)
# then apply the required format
avgPageLoadTime_vector <- sprintf('%02d:%02d:%02d', td@hour, minute(td),
second(td))
# plotting using %H%m%s we can use them as y-axis ticks
# this will give you the same plot as above but Y-axis is fuzzy
ggplot(loadtime_df, aes(date,avgPageLoadTime)) +
geom_point() +
geom_smooth() +
scale_y_continuous(breaks = loadtime_df$avgPageLoadTime,
labels = avgPageLoadTime_vector)
库(ggplot2)
图书馆(lubridate)
#将秒转换为句点
td我认为您需要将以秒为单位的日期值转换为%H%m%s格式,然后尝试打印。我认为您需要以下方法之一-
library(ggplot2)
library(lubridate)
# convert seconds to periods
td <- seconds_to_period(loadtime_df$avgPageLoadTime)
# then apply the required format
avgPageLoadTime_vector <- sprintf('%02d:%02d:%02d', td@hour, minute(td),
second(td))
# plotting using %H%m%s we can use them as y-axis ticks
# this will give you the same plot as above but Y-axis is fuzzy
ggplot(loadtime_df, aes(date,avgPageLoadTime)) +
geom_point() +
geom_smooth() +
scale_y_continuous(breaks = loadtime_df$avgPageLoadTime,
labels = avgPageLoadTime_vector)
库(ggplot2)
图书馆(lubridate)
#将秒转换为句点
td您的Date
列没有小时、分钟或秒。你能仔细检查一下吗?@Tung avgPageLoadTime的单位是秒,但不是日期,有必要吗?奥马尔,如果其中一个答案足以回答你的问题,你能接受它并结束这个问题吗?你的date
列没有小时、分钟或秒。你能再检查一遍吗?@Tung avgPageLoadTime是秒,但不是日期,有必要吗?奥马尔,如果其中一个答案足以回答你的问题,你能接受它并结束问题吗?
library(ggplot2)
ggplot(loadtime_df, aes(date,avgPageLoadTime)) +
geom_point() +
geom_smooth() +
scale_y_continuous(labels=fmt_hms)
library(ggplot2)
library(lubridate)
# convert seconds to periods
td <- seconds_to_period(loadtime_df$avgPageLoadTime)
# then apply the required format
avgPageLoadTime_vector <- sprintf('%02d:%02d:%02d', td@hour, minute(td),
second(td))
# plotting using %H%m%s we can use them as y-axis ticks
# this will give you the same plot as above but Y-axis is fuzzy
ggplot(loadtime_df, aes(date,avgPageLoadTime)) +
geom_point() +
geom_smooth() +
scale_y_continuous(breaks = loadtime_df$avgPageLoadTime,
labels = avgPageLoadTime_vector)
# if you just want to plot with points and not use geom_smooth
# convert the column avgPageLoadTime into %H%m%s date-time format
loadtime_df$avgPageLoadTime <- avgPageLoadTime_vector
# this gives you the right Y-axis values but no smoothing
ggplot(loadtime_df, aes(date,avgPageLoadTime)) +
geom_point()