ggplot2:以y轴以%H%M%S格式绘制秒

时间:2018-09-02 19:10:51

标签: r ggplot2

我有一秒钟的列,需要将其绘制为“%H%M%S”。

我尝试使用lubridate pkg,但该列显示为:

loadtime_dfs$avgPageLoadTime <- seconds_to_period(loadtime_df$avgPageLoadTime)


Formal class 'Period' [package "lubridate"] 
我可以绘制但不显示任何格式的

loadtime_df <- structure(list(date = structure(c(17766, 17767, 17768, 17769, 
17770, 17771), class = "Date"), pagePath = c("/webapp/wcs/stores/servlet/CategoryDisplay?urlRequestType=Base&catalogId=3074457345616676668&categoryId=3074457345616676994&pageView=grid&urlLangId=-24&beginIndex=0&langId=-24&top_category=3074457345616676981&parent_category_rn=3074457345616720192&storeId=10151", 
"/webapp/wcs/stores/servlet/CategoryDisplay?urlRequestType=Base&catalogId=3074457345616676668&categoryId=3074457345616676994&pageView=grid&urlLangId=-24&beginIndex=0&langId=-24&top_category=3074457345616676981&parent_category_rn=3074457345616720192&storeId=10151", 
"/webapp/wcs/stores/servlet/CategoryDisplay?urlRequestType=Base&catalogId=3074457345616676668&categoryId=3074457345616676994&pageView=grid&urlLangId=-24&beginIndex=0&langId=-24&top_category=3074457345616676981&parent_category_rn=3074457345616720192&storeId=10151", 
"/webapp/wcs/stores/servlet/CategoryDisplay?urlRequestType=Base&catalogId=3074457345616676668&categoryId=3074457345616676994&pageView=grid&urlLangId=-24&beginIndex=0&langId=-24&top_category=3074457345616676981&parent_category_rn=3074457345616720192&storeId=10151", 
"/webapp/wcs/stores/servlet/CategoryDisplay?urlRequestType=Base&catalogId=3074457345616676668&categoryId=3074457345616676994&pageView=grid&urlLangId=-24&beginIndex=0&langId=-24&top_category=3074457345616676981&parent_category_rn=3074457345616720192&storeId=10151", 
"/webapp/wcs/stores/servlet/CategoryDisplay?urlRequestType=Base&catalogId=3074457345616676668&categoryId=3074457345616676994&pageView=grid&urlLangId=-24&beginIndex=0&langId=-24&top_category=3074457345616676981&parent_category_rn=3074457345616720192&storeId=10151"
), pageviews = c(245L, 225L, 194L, 214L, 214L, 213L), pageLoadTime = c(18965L, 
185834L, 31115L, 114561L, 88807L, 0L), avgPageLoadTime = c(6, 
27, 16, 138, 144, 0), bouncerate = c(5.63380281690141, 3.48837209302326, 
5.40540540540541, 7.01754385964912, 0, 5), mes = c("agosto", 
"agosto", "agosto", "agosto", "agosto", "agosto")), .Names = c("date", 
"pagePath", "pageviews", "pageLoadTime", "avgPageLoadTime", "bouncerate", 
"mes"), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))

这是我需要绘制的:

ggplot(loadtime_df, aes(date,avgPageLoadTime)) + 
  geom_point() +
  geom_smooth()

但Y轴带有中断:“ 00:01:00”,“ 00:02:00”,“ 00:03:00”,“ 00:04:00”,“ 00:05:00”

2 个答案:

答案 0 :(得分:2)

您将不得不为ggplot提供字符串以分配为标签。

如果您阅读?scale_y_continuous(labels=...),将会看到labels=包含waiver()characterfunction。如果要指定特定的位置和表示形式,则需要同时指定breaks=labels=。但是,通常您希望ggplot2确定放置轴标签的位置,因此我们将提供一个接受值并返回字符串的函数。

我猜想某个地方有一个辅助函数可以做到这一点,但这是base-R版本。 (该函数的来源并不重要,因为我们可以将另一个函数替换为可能具有相同结果的函数。)

此格式设置功能通过将avgPageLoadTime的秒数暂时转换为POSIXct,然后转换为字符串来作弊。以这种方式进行操作意味着它会尊重options("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")
}

为了证明这一点,我将更改您的数据值之一:

loadtime_df$avgPageLoadTime[3] <- loadtime_df$avgPageLoadTime[3] + 0.123456
fmt_hms(loadtime_df$avgPageLoadTime)
# [1] "00:00:06" "00:00:27" "00:00:16" "00:02:18" "00:02:24" "00:00:00"
fmt_hms(loadtime_df$avgPageLoadTime, digits.secs=3)
# [1] "00:00:06.000" "00:00:27.000" "00:00:16.123" "00:02:18.000" "00:02:24.000"
# [6] "00:00:00.000"

所以我们只能提供此功能:

library(ggplot2)
ggplot(loadtime_df, aes(date,avgPageLoadTime)) + 
  geom_point() +
  geom_smooth() +
  scale_y_continuous(labels=fmt_hms)

ggplot2 y-axis updated

答案 1 :(得分:0)

我认为您需要将以秒为单位的日期值转换为%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)

enter image description here

# 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()

enter image description here] 2